udebug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * udebug - debug ring buffer library
  3. *
  4. * Copyright (C) 2023 Felix Fietkau <nbd@nbd.name>
  5. *
  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.
  9. *
  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.
  17. */
  18. #define _GNU_SOURCE
  19. #include <sys/types.h>
  20. #include <sys/mman.h>
  21. #include <sys/socket.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <poll.h>
  28. #include <time.h>
  29. #include "udebug-priv.h"
  30. #include "usock.h"
  31. #define ALIGN(i, sz) (((i) + (sz) - 1) & ~((sz) - 1))
  32. #ifndef MAP_ANONYMOUS
  33. #define MAP_ANONYMOUS MAP_ANON
  34. #endif
  35. #define UDEBUG_MIN_ALLOC_LEN 128
  36. static struct blob_buf b;
  37. static void __randname(char *template)
  38. {
  39. int i;
  40. struct timespec ts;
  41. unsigned long r;
  42. clock_gettime(CLOCK_REALTIME, &ts);
  43. r = ts.tv_sec + ts.tv_nsec;
  44. for (i=0; i<6; i++, r>>=5)
  45. template[i] = 'A'+(r&15)+(r&16)*2;
  46. }
  47. int udebug_id_cmp(const void *k1, const void *k2, void *ptr)
  48. {
  49. uint32_t id1 = (uint32_t)(uintptr_t)k1, id2 = (uint32_t)(uintptr_t)k2;
  50. if (id1 < id2)
  51. return -1;
  52. else if (id1 > id2)
  53. return 1;
  54. return 0;
  55. }
  56. static inline int
  57. shm_open_anon(char *name)
  58. {
  59. char *template = name + strlen(name) - 6;
  60. int fd;
  61. if (template < name || memcmp(template, "XXXXXX", 6) != 0)
  62. return -1;
  63. for (int i = 0; i < 100; i++) {
  64. __randname(template);
  65. fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
  66. if (fd >= 0) {
  67. if (shm_unlink(name) < 0) {
  68. close(fd);
  69. continue;
  70. }
  71. return fd;
  72. }
  73. if (fd < 0 && errno != EEXIST)
  74. return -1;
  75. }
  76. return -1;
  77. }
  78. static void __udebug_disconnect(struct udebug *ctx, bool reconnect)
  79. {
  80. uloop_fd_delete(&ctx->fd);
  81. close(ctx->fd.fd);
  82. ctx->fd.fd = -1;
  83. ctx->poll_handle = -1;
  84. if (ctx->reconnect.cb && reconnect)
  85. uloop_timeout_set(&ctx->reconnect, 1);
  86. }
  87. uint64_t udebug_timestamp(void)
  88. {
  89. struct timespec ts;
  90. uint64_t val;
  91. clock_gettime(CLOCK_REALTIME, &ts);
  92. val = ts.tv_sec;
  93. val *= UDEBUG_TS_SEC;
  94. val += ts.tv_nsec / 1000;
  95. return val;
  96. }
  97. static int
  98. __udebug_buf_map(struct udebug_buf *buf)
  99. {
  100. void *ptr, *ptr2;
  101. ptr = mmap(NULL, buf->head_size + 2 * buf->data_size, PROT_NONE,
  102. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  103. if (ptr == MAP_FAILED)
  104. return -1;
  105. ptr2 = mmap(ptr, buf->head_size + buf->data_size,
  106. PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, buf->fd, 0);
  107. if (ptr2 != ptr)
  108. goto err_unmap;
  109. ptr2 = mmap(ptr + buf->head_size + buf->data_size, buf->data_size,
  110. PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, buf->fd,
  111. buf->head_size);
  112. if (ptr2 != ptr + buf->head_size + buf->data_size)
  113. goto err_unmap;
  114. buf->hdr = ptr;
  115. buf->data = ptr + buf->head_size;
  116. return 0;
  117. err_unmap:
  118. munmap(ptr, buf->head_size + 2 * buf->data_size);
  119. return -1;
  120. }
  121. static int
  122. writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
  123. {
  124. uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
  125. struct msghdr msghdr = { 0 };
  126. struct cmsghdr *cmsg;
  127. int len = 0;
  128. int *pfd;
  129. msghdr.msg_iov = iov,
  130. msghdr.msg_iovlen = iov_len,
  131. msghdr.msg_control = fd_buf;
  132. msghdr.msg_controllen = sizeof(fd_buf);
  133. cmsg = CMSG_FIRSTHDR(&msghdr);
  134. cmsg->cmsg_type = SCM_RIGHTS;
  135. cmsg->cmsg_level = SOL_SOCKET;
  136. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  137. pfd = (int *) CMSG_DATA(cmsg);
  138. msghdr.msg_controllen = cmsg->cmsg_len;
  139. do {
  140. ssize_t cur_len;
  141. if (sock_fd < 0) {
  142. msghdr.msg_control = NULL;
  143. msghdr.msg_controllen = 0;
  144. } else {
  145. *pfd = sock_fd;
  146. }
  147. cur_len = sendmsg(fd, &msghdr, 0);
  148. if (cur_len < 0) {
  149. struct pollfd pfd = {
  150. .fd = fd,
  151. .events = POLLOUT
  152. };
  153. switch(errno) {
  154. case EAGAIN:
  155. poll(&pfd, 1, -1);
  156. break;
  157. case EINTR:
  158. break;
  159. default:
  160. return -1;
  161. }
  162. continue;
  163. }
  164. if (len > 0)
  165. sock_fd = -1;
  166. len += cur_len;
  167. while (cur_len >= (ssize_t) iov->iov_len) {
  168. cur_len -= iov->iov_len;
  169. iov_len--;
  170. iov++;
  171. if (!iov_len)
  172. return len;
  173. }
  174. iov->iov_base += cur_len;
  175. iov->iov_len -= cur_len;
  176. msghdr.msg_iov = iov;
  177. msghdr.msg_iovlen = iov_len;
  178. } while (1);
  179. /* Should never reach here */
  180. return -1;
  181. }
  182. static int
  183. recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
  184. {
  185. uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
  186. struct msghdr msghdr = { 0 };
  187. struct cmsghdr *cmsg;
  188. int total = 0;
  189. int bytes;
  190. int *pfd;
  191. msghdr.msg_iov = iov,
  192. msghdr.msg_iovlen = 1,
  193. msghdr.msg_control = fd_buf;
  194. msghdr.msg_controllen = sizeof(fd_buf);
  195. cmsg = CMSG_FIRSTHDR(&msghdr);
  196. cmsg->cmsg_type = SCM_RIGHTS;
  197. cmsg->cmsg_level = SOL_SOCKET;
  198. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  199. pfd = (int *) CMSG_DATA(cmsg);
  200. while (iov->iov_len > 0) {
  201. if (recv_fd) {
  202. msghdr.msg_control = fd_buf;
  203. msghdr.msg_controllen = cmsg->cmsg_len;
  204. } else {
  205. msghdr.msg_control = NULL;
  206. msghdr.msg_controllen = 0;
  207. }
  208. *pfd = -1;
  209. bytes = recvmsg(fd, &msghdr, 0);
  210. if (!bytes)
  211. return -2;
  212. if (bytes < 0) {
  213. bytes = 0;
  214. if (errno == EINTR)
  215. continue;
  216. if (errno != EAGAIN)
  217. return -2;
  218. }
  219. if (!wait && !bytes)
  220. return 0;
  221. if (recv_fd)
  222. *recv_fd = *pfd;
  223. else if (*pfd >= 0)
  224. close(*pfd);
  225. if (bytes > 0)
  226. recv_fd = NULL;
  227. wait = true;
  228. iov->iov_len -= bytes;
  229. iov->iov_base += bytes;
  230. total += bytes;
  231. if (iov->iov_len > 0) {
  232. struct pollfd pfd = {
  233. .fd = fd,
  234. .events = POLLIN
  235. };
  236. int ret;
  237. do {
  238. ret = poll(&pfd, 1, UDEBUG_TIMEOUT);
  239. } while (ret < 0 && errno == EINTR);
  240. if (!(pfd.revents & POLLIN))
  241. return -1;
  242. }
  243. }
  244. return total;
  245. }
  246. void udebug_send_msg(struct udebug *ctx, struct udebug_client_msg *msg,
  247. struct blob_attr *meta, int fd)
  248. {
  249. struct iovec iov[2] = {
  250. { .iov_base = msg, .iov_len = sizeof(*msg) },
  251. {}
  252. };
  253. if (!meta) {
  254. blob_buf_init(&b, 0);
  255. meta = b.head;
  256. }
  257. iov[1].iov_base = meta;
  258. iov[1].iov_len = blob_pad_len(meta);
  259. writev_retry(ctx->fd.fd, iov, ARRAY_SIZE(iov), fd);
  260. }
  261. static void
  262. udebug_buf_msg(struct udebug_buf *buf, enum udebug_client_msg_type type)
  263. {
  264. struct udebug_client_msg msg = {
  265. .type = type,
  266. .id = buf->id,
  267. };
  268. udebug_send_msg(buf->ctx, &msg, NULL, -1);
  269. }
  270. static size_t __udebug_headsize(unsigned int ring_size, unsigned int page_size)
  271. {
  272. ring_size *= sizeof(struct udebug_ptr);
  273. return ALIGN(sizeof(struct udebug_hdr) + ring_size, page_size);
  274. }
  275. int udebug_buf_open(struct udebug_buf *buf, int fd, uint32_t ring_size, uint32_t data_size)
  276. {
  277. INIT_LIST_HEAD(&buf->list);
  278. buf->fd = fd;
  279. buf->ring_size = ring_size;
  280. buf->head_size = __udebug_headsize(ring_size, sysconf(_SC_PAGESIZE));
  281. buf->data_size = data_size;
  282. if (buf->ring_size > (1U << 24) || buf->data_size > (1U << 29))
  283. return -1;
  284. if (__udebug_buf_map(buf))
  285. return -1;
  286. if (buf->ring_size != buf->hdr->ring_size ||
  287. buf->data_size != buf->hdr->data_size) {
  288. munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
  289. buf->hdr = NULL;
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. int udebug_buf_init(struct udebug_buf *buf, size_t entries, size_t size)
  295. {
  296. uint32_t pagesz = sysconf(_SC_PAGESIZE);
  297. char filename[] = "/udebug.XXXXXX";
  298. unsigned int order = 12;
  299. uint8_t ring_order = 5;
  300. size_t head_size;
  301. int fd;
  302. INIT_LIST_HEAD(&buf->list);
  303. if (size < pagesz)
  304. size = pagesz;
  305. while(size > 1U << order)
  306. order++;
  307. size = 1 << order;
  308. while (entries > 1U << ring_order)
  309. ring_order++;
  310. entries = 1 << ring_order;
  311. if (size > (1U << 29) || entries > (1U << 24))
  312. return -1;
  313. head_size = __udebug_headsize(entries, pagesz);
  314. while (ALIGN(sizeof(*buf->hdr) + (entries * 2) * sizeof(struct udebug_ptr), pagesz) == head_size)
  315. entries *= 2;
  316. fd = shm_open_anon(filename);
  317. if (fd < 0)
  318. return -1;
  319. if (ftruncate(fd, head_size + size) < 0)
  320. goto err_close;
  321. buf->head_size = head_size;
  322. buf->data_size = size;
  323. buf->ring_size = entries;
  324. buf->fd = fd;
  325. if (__udebug_buf_map(buf))
  326. goto err_close;
  327. buf->hdr->ring_size = entries;
  328. buf->hdr->data_size = size;
  329. /* ensure hdr changes are visible */
  330. __sync_synchronize();
  331. return 0;
  332. err_close:
  333. close(fd);
  334. return -1;
  335. }
  336. static void *udebug_buf_alloc(struct udebug_buf *buf, uint32_t ofs, uint32_t len)
  337. {
  338. struct udebug_hdr *hdr = buf->hdr;
  339. hdr->data_used = u32_max(hdr->data_used, ofs + len + 1);
  340. /* ensure that data_used update is visible before clobbering data */
  341. __sync_synchronize();
  342. return udebug_buf_ptr(buf, ofs);
  343. }
  344. uint64_t udebug_buf_flags(struct udebug_buf *buf)
  345. {
  346. struct udebug_hdr *hdr = buf->hdr;
  347. uint64_t flags;
  348. if (!hdr)
  349. return 0;
  350. flags = hdr->flags[0];
  351. if (sizeof(flags) != sizeof(uintptr_t))
  352. flags |= ((uint64_t)hdr->flags[1]) << 32;
  353. return flags;
  354. }
  355. void udebug_entry_init_ts(struct udebug_buf *buf, uint64_t timestamp)
  356. {
  357. struct udebug_hdr *hdr = buf->hdr;
  358. struct udebug_ptr *ptr;
  359. if (!hdr)
  360. return;
  361. ptr = udebug_ring_ptr(hdr, hdr->head);
  362. ptr->start = hdr->data_head;
  363. ptr->len = 0;
  364. ptr->timestamp = timestamp;
  365. }
  366. void *udebug_entry_append(struct udebug_buf *buf, const void *data, uint32_t len)
  367. {
  368. struct udebug_hdr *hdr = buf->hdr;
  369. struct udebug_ptr *ptr;
  370. uint32_t ofs;
  371. void *ret;
  372. if (!hdr)
  373. return NULL;
  374. ptr = udebug_ring_ptr(hdr, hdr->head);
  375. ofs = ptr->start + ptr->len;
  376. if (ptr->len + len > buf->data_size / 2)
  377. return NULL;
  378. ret = udebug_buf_alloc(buf, ofs, len);
  379. if (data)
  380. memcpy(ret, data, len);
  381. ptr->len += len;
  382. return ret;
  383. }
  384. int udebug_entry_printf(struct udebug_buf *buf, const char *fmt, ...)
  385. {
  386. va_list ap;
  387. size_t ret;
  388. va_start(ap, fmt);
  389. ret = udebug_entry_vprintf(buf, fmt, ap);
  390. va_end(ap);
  391. return ret;
  392. }
  393. int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
  394. {
  395. struct udebug_hdr *hdr = buf->hdr;
  396. struct udebug_ptr *ptr;
  397. uint32_t ofs;
  398. uint32_t len;
  399. char *str;
  400. if (!hdr)
  401. return -1;
  402. ptr = udebug_ring_ptr(hdr, hdr->head);
  403. ofs = ptr->start + ptr->len;
  404. if (ptr->len > buf->data_size / 2)
  405. return -1;
  406. str = udebug_buf_alloc(buf, ofs, UDEBUG_MIN_ALLOC_LEN);
  407. len = vsnprintf(str, UDEBUG_MIN_ALLOC_LEN, fmt, ap);
  408. if (len <= UDEBUG_MIN_ALLOC_LEN)
  409. goto out;
  410. if (ptr->len + len > buf->data_size / 2)
  411. return -1;
  412. udebug_buf_alloc(buf, ofs, len + 1);
  413. len = vsnprintf(str, len, fmt, ap);
  414. out:
  415. ptr->len += len;
  416. return 0;
  417. }
  418. void udebug_entry_add(struct udebug_buf *buf)
  419. {
  420. struct udebug_hdr *hdr = buf->hdr;
  421. struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
  422. uint32_t notify;
  423. uint8_t *data;
  424. /* ensure strings are always 0-terminated */
  425. data = udebug_buf_ptr(buf, ptr->start + ptr->len);
  426. *data = 0;
  427. hdr->data_head = ptr->start + ptr->len + 1;
  428. /* ensure that all data changes are visible before advancing head */
  429. __sync_synchronize();
  430. u32_set(&hdr->head, u32_get(&hdr->head) + 1);
  431. if (!u32_get(&hdr->head))
  432. u32_set(&hdr->head_hi, u32_get(&hdr->head_hi) + 1);
  433. /* ensure that head change is visible */
  434. __sync_synchronize();
  435. notify = __atomic_exchange_n(&hdr->notify, 0, __ATOMIC_RELAXED);
  436. if (notify) {
  437. struct udebug_client_msg msg = {
  438. .type = CL_MSG_RING_NOTIFY,
  439. .id = buf->id,
  440. .notify_mask = notify,
  441. };
  442. blob_buf_init(&b, 0);
  443. udebug_send_msg(buf->ctx, &msg, b.head, -1);
  444. }
  445. }
  446. void udebug_buf_free(struct udebug_buf *buf)
  447. {
  448. struct udebug *ctx = buf->ctx;
  449. if (!list_empty(&buf->list) && buf->list.prev)
  450. list_del(&buf->list);
  451. if (ctx && ctx->fd.fd >= 0)
  452. udebug_buf_msg(buf, CL_MSG_RING_REMOVE);
  453. munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
  454. close(buf->fd);
  455. memset(buf, 0, sizeof(*buf));
  456. }
  457. static void
  458. __udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf)
  459. {
  460. struct udebug_client_msg msg = {
  461. .type = CL_MSG_RING_ADD,
  462. .id = buf->id,
  463. .ring_size = buf->hdr->ring_size,
  464. .data_size = buf->hdr->data_size,
  465. };
  466. const struct udebug_buf_meta *meta = buf->meta;
  467. void *c;
  468. blob_buf_init(&b, 0);
  469. blobmsg_add_string(&b, "name", meta->name);
  470. c = blobmsg_open_array(&b, "flags");
  471. for (size_t i = 0; i < meta->n_flags; i++) {
  472. const struct udebug_buf_flag *flag = &meta->flags[i];
  473. void *e = blobmsg_open_array(&b, NULL);
  474. blobmsg_add_string(&b, NULL, flag->name);
  475. blobmsg_add_u64(&b, NULL, flag->mask);
  476. blobmsg_close_array(&b, e);
  477. }
  478. blobmsg_close_array(&b, c);
  479. udebug_send_msg(ctx, &msg, b.head, buf->fd);
  480. }
  481. int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
  482. const struct udebug_buf_meta *meta)
  483. {
  484. list_add_tail(&buf->list, &ctx->local_rings);
  485. buf->ctx = ctx;
  486. buf->meta = meta;
  487. buf->id = ctx->next_id++;
  488. buf->hdr->format = meta->format;
  489. buf->hdr->sub_format = meta->sub_format;
  490. if (ctx->fd.fd >= 0)
  491. __udebug_buf_add(ctx, buf);
  492. return 0;
  493. }
  494. void udebug_init(struct udebug *ctx)
  495. {
  496. INIT_LIST_HEAD(&ctx->local_rings);
  497. avl_init(&ctx->remote_rings, udebug_id_cmp, true, NULL);
  498. ctx->fd.fd = -1;
  499. ctx->poll_handle = -1;
  500. }
  501. static void udebug_reconnect_cb(struct uloop_timeout *t)
  502. {
  503. struct udebug *ctx = container_of(t, struct udebug, reconnect);
  504. if (udebug_connect(ctx, ctx->socket_path) < 0) {
  505. uloop_timeout_set(&ctx->reconnect, 1000);
  506. return;
  507. }
  508. udebug_add_uloop(ctx);
  509. }
  510. void udebug_auto_connect(struct udebug *ctx, const char *path)
  511. {
  512. free(ctx->socket_path);
  513. ctx->reconnect.cb = udebug_reconnect_cb;
  514. ctx->socket_path = path ? strdup(path) : NULL;
  515. if (ctx->fd.fd >= 0)
  516. return;
  517. udebug_reconnect_cb(&ctx->reconnect);
  518. }
  519. int udebug_connect(struct udebug *ctx, const char *path)
  520. {
  521. struct udebug_remote_buf *rb;
  522. struct udebug_buf *buf;
  523. if (ctx->fd.fd >= 0)
  524. close(ctx->fd.fd);
  525. ctx->fd.fd = -1;
  526. if (!path)
  527. path = UDEBUG_SOCK_NAME;
  528. ctx->fd.fd = usock(USOCK_UNIX, path, NULL);
  529. if (ctx->fd.fd < 0)
  530. return -1;
  531. list_for_each_entry(buf, &ctx->local_rings, list)
  532. __udebug_buf_add(ctx, buf);
  533. avl_for_each_element(&ctx->remote_rings, rb, node) {
  534. if (!rb->poll)
  535. continue;
  536. rb->poll = false;
  537. udebug_remote_buf_set_poll(ctx, rb, true);
  538. }
  539. return 0;
  540. }
  541. static bool
  542. udebug_recv_msg(struct udebug *ctx, struct udebug_client_msg *msg, int *fd,
  543. bool wait)
  544. {
  545. struct iovec iov = {
  546. .iov_base = msg,
  547. .iov_len = sizeof(*msg)
  548. };
  549. int ret;
  550. ret = recv_retry(ctx->fd.fd, &iov, wait, fd);
  551. if (ret == -2)
  552. __udebug_disconnect(ctx, true);
  553. return ret == sizeof(*msg);
  554. }
  555. struct udebug_client_msg *__udebug_poll(struct udebug *ctx, int *fd, bool wait)
  556. {
  557. static struct udebug_client_msg msg = {};
  558. while (udebug_recv_msg(ctx, &msg, fd, wait)) {
  559. struct udebug_remote_buf *rb;
  560. void *key;
  561. if (msg.type != CL_MSG_RING_NOTIFY)
  562. return &msg;
  563. if (fd && *fd >= 0)
  564. close(*fd);
  565. if (!ctx->notify_cb)
  566. continue;
  567. key = (void *)(uintptr_t)msg.id;
  568. rb = avl_find_element(&ctx->remote_rings, key, rb, node);
  569. if (!rb || !rb->poll)
  570. continue;
  571. if (ctx->poll_handle >= 0)
  572. __atomic_fetch_or(&rb->buf.hdr->notify,
  573. 1UL << ctx->poll_handle,
  574. __ATOMIC_RELAXED);
  575. ctx->notify_cb(ctx, rb);
  576. }
  577. return NULL;
  578. }
  579. void udebug_poll(struct udebug *ctx)
  580. {
  581. while (__udebug_poll(ctx, NULL, false));
  582. }
  583. static void udebug_fd_cb(struct uloop_fd *fd, unsigned int events)
  584. {
  585. struct udebug *ctx = container_of(fd, struct udebug, fd);
  586. if (fd->eof)
  587. __udebug_disconnect(ctx, true);
  588. udebug_poll(ctx);
  589. }
  590. void udebug_add_uloop(struct udebug *ctx)
  591. {
  592. if (ctx->fd.registered)
  593. return;
  594. ctx->fd.cb = udebug_fd_cb;
  595. uloop_fd_add(&ctx->fd, ULOOP_READ);
  596. }
  597. void udebug_free(struct udebug *ctx)
  598. {
  599. struct udebug_remote_buf *rb, *tmp;
  600. struct udebug_buf *buf;
  601. free(ctx->socket_path);
  602. ctx->socket_path = NULL;
  603. __udebug_disconnect(ctx, false);
  604. uloop_timeout_cancel(&ctx->reconnect);
  605. while (!list_empty(&ctx->local_rings)) {
  606. buf = list_first_entry(&ctx->local_rings, struct udebug_buf, list);
  607. udebug_buf_free(buf);
  608. }
  609. avl_for_each_element_safe(&ctx->remote_rings, rb, node, tmp)
  610. udebug_remote_buf_unmap(ctx, rb);
  611. }