2 * udebug - debug ring buffer library
4 * Copyright (C) 2023 Felix Fietkau <nbd@nbd.name>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #ifndef __UDEBUG_RINGBUF_H
19 #define __UDEBUG_RINGBUF_H
21 #include <sys/types.h>
29 #define UDEBUG_SOCK_NAME "/var/run/udebug.sock"
34 UDEBUG_FORMAT_BLOBMSG
,
38 UDEBUG_DLT_ETHERNET
= 1,
40 UDEBUG_DLT_RAW_IP
= 101,
41 UDEBUG_DLT_IEEE_802_11
= 105,
42 UDEBUG_DLT_IEEE_802_11_RADIOTAP
= 127,
43 UDEBUG_DLT_NETLINK
= 253,
46 enum udebug_meta_type
{
47 UDEBUG_META_IFACE_NAME
,
48 UDEBUG_META_IFACE_DESC
,
52 #define UDEBUG_TS_MSEC 1000ULL
53 #define UDEBUG_TS_SEC (1000ULL * UDEBUG_TS_MSEC)
58 struct udebug_buf_flag
{
63 struct udebug_buf_meta
{
65 enum udebug_format format
;
66 uint32_t sub_format
; /* linktype for UDEBUG_FORMAT_PACKET */
67 const struct udebug_buf_flag
*flags
;
73 const struct udebug_buf_meta
*meta
;
76 struct list_head list
;
78 struct udebug_hdr
*hdr
;
86 struct udebug_packet_info
{
87 const char *attr
[__UDEBUG_META_MAX
];
90 struct udebug_remote_buf
{
92 struct udebug_buf buf
;
96 /* provided by user */
99 const struct udebug_packet_info
*meta
;
103 struct list_head local_rings
;
104 struct avl_tree remote_rings
;
109 struct uloop_timeout reconnect
;
112 void (*notify_cb
)(struct udebug
*ctx
, struct udebug_remote_buf
*rb
);
121 struct udebug_snapshot
{
122 struct udebug_ptr
*entries
;
123 unsigned int n_entries
;
124 unsigned int dropped
;
130 enum udebug_format format
;
137 struct udebug_snapshot
**list
;
140 struct udebug_snapshot
*s
;
148 uint64_t udebug_timestamp(void);
150 void udebug_entry_init_ts(struct udebug_buf
*buf
, uint64_t timestamp
);
151 static inline void udebug_entry_init(struct udebug_buf
*buf
)
153 udebug_entry_init_ts(buf
, udebug_timestamp());
155 void *udebug_entry_append(struct udebug_buf
*buf
, const void *data
, uint32_t len
);
156 int udebug_entry_printf(struct udebug_buf
*buf
, const char *fmt
, ...)
157 __attribute__ ((format (printf
, 2, 3)));
158 int udebug_entry_vprintf(struct udebug_buf
*buf
, const char *fmt
, va_list ap
)
159 __attribute__ ((format (printf
, 2, 0)));
160 void udebug_entry_add(struct udebug_buf
*buf
);
162 int udebug_buf_init(struct udebug_buf
*buf
, size_t entries
, size_t size
);
163 int udebug_buf_add(struct udebug
*ctx
, struct udebug_buf
*buf
,
164 const struct udebug_buf_meta
*meta
);
165 uint64_t udebug_buf_flags(struct udebug_buf
*buf
);
166 void udebug_buf_free(struct udebug_buf
*buf
);
168 struct udebug_remote_buf
*udebug_remote_buf_get(struct udebug
*ctx
, uint32_t id
);
169 int udebug_remote_buf_map(struct udebug
*ctx
, struct udebug_remote_buf
*rb
, uint32_t id
);
170 void udebug_remote_buf_unmap(struct udebug
*ctx
, struct udebug_remote_buf
*rb
);
171 int udebug_remote_buf_set_poll(struct udebug
*ctx
, struct udebug_remote_buf
*rb
, bool val
);
172 void udebug_remote_buf_set_flags(struct udebug_remote_buf
*rb
, uint64_t mask
, uint64_t set
);
173 struct udebug_snapshot
*udebug_remote_buf_snapshot(struct udebug_remote_buf
*rb
);
174 bool udebug_snapshot_get_entry(struct udebug_snapshot
*s
, struct udebug_iter
*it
, unsigned int entry
);
176 void udebug_remote_buf_set_start_time(struct udebug_remote_buf
*rb
, uint64_t ts
);
177 void udebug_remote_buf_set_start_offset(struct udebug_remote_buf
*rb
, uint32_t idx
);
179 void udebug_iter_start(struct udebug_iter
*it
, struct udebug_snapshot
**s
, size_t n
);
180 bool udebug_iter_next(struct udebug_iter
*it
);
182 void udebug_init(struct udebug
*ctx
);
183 int udebug_connect(struct udebug
*ctx
, const char *path
);
184 void udebug_auto_connect(struct udebug
*ctx
, const char *path
);
185 void udebug_add_uloop(struct udebug
*ctx
);
186 void udebug_poll(struct udebug
*ctx
);
187 void udebug_free(struct udebug
*ctx
);
189 static inline bool udebug_is_connected(struct udebug
*ctx
)
191 return ctx
->fd
.fd
>= 0;
194 int udebug_id_cmp(const void *k1
, const void *k2
, void *ptr
);