lib-ucode: truncate pcap files
[project/udebug.git] / server.h
1 #ifndef __UDEBUG_SERVER_H
2 #define __UDEBUG_SERVER_H
3
4 #include <libubox/list.h>
5 #include <libubox/uloop.h>
6 #include <libubox/avl.h>
7 #include <libubox/blobmsg.h>
8 #include <libubox/udebug.h>
9 #include <libubox/udebug-proto.h>
10
11 extern int debug_level;
12
13 #define D(level, format, ...) \
14 do { \
15 if (debug_level >= level) \
16 fprintf(stderr, "DEBUG: %s(%d) " format "\n", \
17 __func__, __LINE__, ##__VA_ARGS__); \
18 } while (0)
19
20 #define DC(level, cl, format, ...) \
21 D(level, "[%s(%d)] " format, cl->proc_name, cl->pid, ##__VA_ARGS__)
22
23 struct client {
24 struct list_head list;
25 struct list_head bufs;
26 struct uloop_fd fd;
27 int notify_id;
28
29 char proc_name[64];
30 int pid;
31 int uid;
32
33 int rx_fd;
34 size_t rx_ofs;
35 struct {
36 struct udebug_client_msg msg;
37 union {
38 struct blob_attr data;
39 uint8_t buf[4096];
40 };
41 } __attribute__((packed,aligned(4))) rx_buf;
42 };
43
44 struct client_ring {
45 struct list_head list;
46 struct avl_node node;
47 struct client *cl;
48
49 int fd;
50 uint32_t id;
51 uint32_t ring_size, data_size;
52 const char *name;
53 struct blob_attr *flags;
54 };
55
56 extern struct avl_tree rings;
57
58 void client_alloc(int fd);
59 struct client_ring *client_ring_alloc(struct client *cl);
60 struct client_ring *client_ring_get_by_id(struct client *cl, uint32_t id);
61 void client_ring_free(struct client_ring *r);
62
63 static inline uint32_t ring_id(struct client_ring *r)
64 {
65 return (uint32_t)(uintptr_t)r->node.key;
66 }
67
68 static inline struct client_ring *ring_get_by_id(uint32_t id)
69 {
70 struct client_ring *r;
71 void *key = (void *)(uintptr_t)id;
72
73 return avl_find_element(&rings, key, r, node);
74 }
75
76 void udebug_ubus_init(void);
77 void udebug_ubus_ring_notify(struct client_ring *r, bool add);
78 void udebug_ubus_free(void);
79
80 #endif