udebug-cli: fix error message
[project/udebug.git] / udebug.h
1 #ifndef __UDEBUG_RINGBUF_H
2 #define __UDEBUG_RINGBUF_H
3
4 #include <sys/types.h>
5 #include <stdint.h>
6 #include <stdarg.h>
7
8 #include <libubox/list.h>
9 #include <libubox/uloop.h>
10 #include <libubox/avl.h>
11
12 #define UDEBUG_SOCK_NAME "/var/run/udebug.sock"
13
14 enum udebug_format {
15 UDEBUG_FORMAT_PACKET,
16 UDEBUG_FORMAT_STRING,
17 UDEBUG_FORMAT_BLOBMSG,
18 };
19
20 enum {
21 UDEBUG_DLT_ETHERNET = 1,
22 UDEBUG_DLT_PPP = 50,
23 UDEBUG_DLT_RAW_IP = 101,
24 UDEBUG_DLT_IEEE_802_11 = 105,
25 UDEBUG_DLT_IEEE_802_11_RADIOTAP = 127,
26 UDEBUG_DLT_NETLINK = 253,
27 };
28
29 enum udebug_meta_type {
30 UDEBUG_META_IFACE_NAME,
31 UDEBUG_META_IFACE_DESC,
32 __UDEBUG_META_MAX
33 };
34
35 #define UDEBUG_TS_MSEC 1000ULL
36 #define UDEBUG_TS_SEC (1000ULL * UDEBUG_TS_MSEC)
37
38 struct udebug;
39 struct udebug_hdr;
40
41 struct udebug_buf_flag {
42 const char *name;
43 uint64_t mask;
44 };
45
46 struct udebug_buf_meta {
47 const char *name;
48 enum udebug_format format;
49 uint32_t sub_format; /* linktype for UDEBUG_FORMAT_PACKET */
50 const struct udebug_buf_flag *flags;
51 unsigned int n_flags;
52 };
53
54 struct udebug_buf {
55 struct udebug *ctx;
56 const struct udebug_buf_meta *meta;
57 uint32_t id;
58
59 struct list_head list;
60
61 struct udebug_hdr *hdr;
62 void *data;
63 size_t data_size;
64 size_t head_size;
65 size_t ring_size;
66 int fd;
67 };
68
69 struct udebug_packet_info {
70 const char *attr[__UDEBUG_META_MAX];
71 };
72
73 struct udebug_remote_buf {
74 struct avl_node node;
75 struct udebug_buf buf;
76 bool poll;
77 uint32_t head;
78
79 /* provided by user */
80 uint32_t pcap_iface;
81 void *priv;
82 const struct udebug_packet_info *meta;
83 };
84
85 struct udebug {
86 struct list_head local_rings;
87 struct avl_tree remote_rings;
88 uint32_t next_id;
89 struct uloop_fd fd;
90 int poll_handle;
91 char *socket_path;
92 struct uloop_timeout reconnect;
93
94 /* filled by user */
95 void (*notify_cb)(struct udebug *ctx, struct udebug_remote_buf *rb);
96 };
97
98 struct udebug_ptr {
99 uint32_t start;
100 uint32_t len;
101 uint64_t timestamp;
102 };
103
104 struct udebug_snapshot {
105 struct udebug_ptr *entries;
106 unsigned int n_entries;
107 unsigned int dropped;
108 void *data;
109 size_t data_size;
110
111 uint32_t iter_idx;
112
113 enum udebug_format format;
114 uint32_t sub_format;
115
116 uint32_t rbuf_idx;
117 };
118
119 struct udebug_iter {
120 struct udebug_snapshot **list;
121 size_t n;
122
123 struct udebug_snapshot *s;
124 unsigned int s_idx;
125
126 uint64_t timestamp;
127 void *data;
128 size_t len;
129 };
130
131 uint64_t udebug_timestamp(void);
132
133 void udebug_entry_init_ts(struct udebug_buf *buf, uint64_t timestamp);
134 static inline void udebug_entry_init(struct udebug_buf *buf)
135 {
136 udebug_entry_init_ts(buf, udebug_timestamp());
137 }
138 void *udebug_entry_append(struct udebug_buf *buf, const void *data, uint32_t len);
139 int udebug_entry_printf(struct udebug_buf *buf, const char *fmt, ...);
140 int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap);
141 void udebug_entry_add(struct udebug_buf *buf);
142
143 int udebug_buf_init(struct udebug_buf *buf, size_t entries, size_t size);
144 int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
145 const struct udebug_buf_meta *meta);
146 uint64_t udebug_buf_flags(struct udebug_buf *buf);
147 void udebug_buf_free(struct udebug_buf *buf);
148
149 struct udebug_remote_buf *udebug_remote_buf_get(struct udebug *ctx, uint32_t id);
150 int udebug_remote_buf_map(struct udebug *ctx, struct udebug_remote_buf *rb, uint32_t id);
151 void udebug_remote_buf_unmap(struct udebug *ctx, struct udebug_remote_buf *rb);
152 int udebug_remote_buf_set_poll(struct udebug *ctx, struct udebug_remote_buf *rb, bool val);
153 void udebug_remote_buf_set_flags(struct udebug_remote_buf *rb, uint64_t mask, uint64_t set);
154 struct udebug_snapshot *udebug_remote_buf_snapshot(struct udebug_remote_buf *rb);
155 bool udebug_snapshot_get_entry(struct udebug_snapshot *s, struct udebug_iter *it, unsigned int entry);
156
157 void udebug_remote_buf_set_start_time(struct udebug_remote_buf *rb, uint64_t ts);
158 void udebug_remote_buf_set_start_offset(struct udebug_remote_buf *rb, uint32_t idx);
159
160 void udebug_iter_start(struct udebug_iter *it, struct udebug_snapshot **s, size_t n);
161 bool udebug_iter_next(struct udebug_iter *it);
162
163 void udebug_init(struct udebug *ctx);
164 int udebug_connect(struct udebug *ctx, const char *path);
165 void udebug_auto_connect(struct udebug *ctx, const char *path);
166 void udebug_add_uloop(struct udebug *ctx);
167 void udebug_poll(struct udebug *ctx);
168 void udebug_free(struct udebug *ctx);
169
170 static inline bool udebug_is_connected(struct udebug *ctx)
171 {
172 return ctx->fd.fd >= 0;
173 }
174
175
176 #endif