udebug.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. static void
  247. udebug_send_msg(struct udebug *ctx, struct udebug_client_msg *msg,
  248. struct blob_attr *meta, int fd)
  249. {
  250. struct iovec iov[2] = {
  251. { .iov_base = msg, .iov_len = sizeof(*msg) },
  252. {}
  253. };
  254. if (!meta) {
  255. blob_buf_init(&b, 0);
  256. meta = b.head;
  257. }
  258. iov[1].iov_base = meta;
  259. iov[1].iov_len = blob_pad_len(meta);
  260. writev_retry(ctx->fd.fd, iov, ARRAY_SIZE(iov), fd);
  261. }
  262. static bool
  263. udebug_recv_msg(struct udebug *ctx, struct udebug_client_msg *msg, int *fd,
  264. bool wait)
  265. {
  266. struct iovec iov = {
  267. .iov_base = msg,
  268. .iov_len = sizeof(*msg)
  269. };
  270. int ret;
  271. ret = recv_retry(ctx->fd.fd, &iov, wait, fd);
  272. if (ret == -2)
  273. __udebug_disconnect(ctx, true);
  274. return ret == sizeof(*msg);
  275. }
  276. static struct udebug_client_msg *
  277. __udebug_poll(struct udebug *ctx, int *fd, bool wait)
  278. {
  279. static struct udebug_client_msg msg = {};
  280. while (udebug_recv_msg(ctx, &msg, fd, wait)) {
  281. struct udebug_remote_buf *rb;
  282. void *key;
  283. if (msg.type != CL_MSG_RING_NOTIFY)
  284. return &msg;
  285. if (fd && *fd >= 0)
  286. close(*fd);
  287. if (!ctx->notify_cb)
  288. continue;
  289. key = (void *)(uintptr_t)msg.id;
  290. rb = avl_find_element(&ctx->remote_rings, key, rb, node);
  291. if (!rb || !rb->poll)
  292. continue;
  293. if (ctx->poll_handle >= 0)
  294. __atomic_fetch_or(&rb->buf.hdr->notify,
  295. 1UL << ctx->poll_handle,
  296. __ATOMIC_RELAXED);
  297. ctx->notify_cb(ctx, rb);
  298. }
  299. return NULL;
  300. }
  301. static struct udebug_client_msg *
  302. udebug_wait_for_response(struct udebug *ctx, struct udebug_client_msg *msg, int *rfd)
  303. {
  304. int type = msg->type;
  305. int fd = -1;
  306. do {
  307. if (fd >= 0)
  308. close(fd);
  309. fd = -1;
  310. msg = __udebug_poll(ctx, &fd, true);
  311. } while (msg && msg->type != type);
  312. if (!msg)
  313. return NULL;
  314. if (rfd)
  315. *rfd = fd;
  316. else if (fd >= 0)
  317. close(fd);
  318. return msg;
  319. }
  320. static void
  321. udebug_buf_msg(struct udebug_buf *buf, enum udebug_client_msg_type type)
  322. {
  323. struct udebug_client_msg msg = {
  324. .type = type,
  325. .id = buf->id,
  326. };
  327. udebug_send_msg(buf->ctx, &msg, NULL, -1);
  328. udebug_wait_for_response(buf->ctx, &msg, NULL);
  329. }
  330. static size_t __udebug_headsize(unsigned int ring_size, unsigned int page_size)
  331. {
  332. ring_size *= sizeof(struct udebug_ptr);
  333. return ALIGN(sizeof(struct udebug_hdr) + ring_size, page_size);
  334. }
  335. int udebug_buf_open(struct udebug_buf *buf, int fd, uint32_t ring_size, uint32_t data_size)
  336. {
  337. INIT_LIST_HEAD(&buf->list);
  338. buf->fd = fd;
  339. buf->ring_size = ring_size;
  340. buf->head_size = __udebug_headsize(ring_size, sysconf(_SC_PAGESIZE));
  341. buf->data_size = data_size;
  342. if (buf->ring_size > (1U << 24) || buf->data_size > (1U << 29))
  343. return -1;
  344. if (__udebug_buf_map(buf))
  345. return -1;
  346. if (buf->ring_size != buf->hdr->ring_size ||
  347. buf->data_size != buf->hdr->data_size) {
  348. munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
  349. buf->hdr = NULL;
  350. return -1;
  351. }
  352. return 0;
  353. }
  354. int udebug_buf_init(struct udebug_buf *buf, size_t entries, size_t size)
  355. {
  356. uint32_t pagesz = sysconf(_SC_PAGESIZE);
  357. char filename[] = "/udebug.XXXXXX";
  358. unsigned int order = 12;
  359. uint8_t ring_order = 5;
  360. size_t head_size;
  361. int fd;
  362. INIT_LIST_HEAD(&buf->list);
  363. if (size < pagesz)
  364. size = pagesz;
  365. while(size > 1U << order)
  366. order++;
  367. size = 1 << order;
  368. while (entries > 1U << ring_order)
  369. ring_order++;
  370. entries = 1 << ring_order;
  371. if (size > (1U << 29) || entries > (1U << 24))
  372. return -1;
  373. head_size = __udebug_headsize(entries, pagesz);
  374. while (ALIGN(sizeof(*buf->hdr) + (entries * 2) * sizeof(struct udebug_ptr), pagesz) == head_size)
  375. entries *= 2;
  376. fd = shm_open_anon(filename);
  377. if (fd < 0)
  378. return -1;
  379. if (ftruncate(fd, head_size + size) < 0)
  380. goto err_close;
  381. buf->head_size = head_size;
  382. buf->data_size = size;
  383. buf->ring_size = entries;
  384. buf->fd = fd;
  385. if (__udebug_buf_map(buf))
  386. goto err_close;
  387. buf->hdr->ring_size = entries;
  388. buf->hdr->data_size = size;
  389. /* ensure hdr changes are visible */
  390. __sync_synchronize();
  391. return 0;
  392. err_close:
  393. close(fd);
  394. return -1;
  395. }
  396. static void *udebug_buf_alloc(struct udebug_buf *buf, uint32_t ofs, uint32_t len)
  397. {
  398. struct udebug_hdr *hdr = buf->hdr;
  399. hdr->data_used = u32_max(hdr->data_used, ofs + len + 1);
  400. /* ensure that data_used update is visible before clobbering data */
  401. __sync_synchronize();
  402. return udebug_buf_ptr(buf, ofs);
  403. }
  404. uint64_t udebug_buf_flags(struct udebug_buf *buf)
  405. {
  406. struct udebug_hdr *hdr = buf->hdr;
  407. uint64_t flags;
  408. if (!hdr)
  409. return 0;
  410. flags = hdr->flags[0];
  411. if (sizeof(flags) != sizeof(uintptr_t))
  412. flags |= ((uint64_t)hdr->flags[1]) << 32;
  413. return flags;
  414. }
  415. void udebug_entry_init_ts(struct udebug_buf *buf, uint64_t timestamp)
  416. {
  417. struct udebug_hdr *hdr = buf->hdr;
  418. struct udebug_ptr *ptr;
  419. if (!hdr)
  420. return;
  421. ptr = udebug_ring_ptr(hdr, hdr->head);
  422. ptr->start = hdr->data_head;
  423. ptr->len = 0;
  424. ptr->timestamp = timestamp;
  425. }
  426. void *udebug_entry_append(struct udebug_buf *buf, const void *data, uint32_t len)
  427. {
  428. struct udebug_hdr *hdr = buf->hdr;
  429. struct udebug_ptr *ptr;
  430. uint32_t ofs;
  431. void *ret;
  432. if (!hdr)
  433. return NULL;
  434. ptr = udebug_ring_ptr(hdr, hdr->head);
  435. ofs = ptr->start + ptr->len;
  436. if (ptr->len + len > buf->data_size / 2)
  437. return NULL;
  438. ret = udebug_buf_alloc(buf, ofs, len);
  439. if (data)
  440. memcpy(ret, data, len);
  441. ptr->len += len;
  442. return ret;
  443. }
  444. uint16_t udebug_entry_trim(struct udebug_buf *buf, uint16_t len)
  445. {
  446. struct udebug_hdr *hdr = buf->hdr;
  447. struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
  448. if (len)
  449. ptr->len -= len;
  450. return ptr->len;
  451. }
  452. void udebug_entry_set_length(struct udebug_buf *buf, uint16_t len)
  453. {
  454. struct udebug_hdr *hdr = buf->hdr;
  455. struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
  456. ptr->len = len;
  457. }
  458. int udebug_entry_printf(struct udebug_buf *buf, const char *fmt, ...)
  459. {
  460. va_list ap;
  461. size_t ret;
  462. va_start(ap, fmt);
  463. ret = udebug_entry_vprintf(buf, fmt, ap);
  464. va_end(ap);
  465. return ret;
  466. }
  467. int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
  468. {
  469. struct udebug_hdr *hdr = buf->hdr;
  470. struct udebug_ptr *ptr;
  471. uint32_t ofs;
  472. uint32_t len;
  473. char *str;
  474. if (!hdr)
  475. return -1;
  476. ptr = udebug_ring_ptr(hdr, hdr->head);
  477. ofs = ptr->start + ptr->len;
  478. if (ptr->len > buf->data_size / 2)
  479. return -1;
  480. str = udebug_buf_alloc(buf, ofs, UDEBUG_MIN_ALLOC_LEN);
  481. len = vsnprintf(str, UDEBUG_MIN_ALLOC_LEN, fmt, ap);
  482. if (len <= UDEBUG_MIN_ALLOC_LEN)
  483. goto out;
  484. if (ptr->len + len > buf->data_size / 2)
  485. return -1;
  486. udebug_buf_alloc(buf, ofs, len + 1);
  487. len = vsnprintf(str, len, fmt, ap);
  488. out:
  489. ptr->len += len;
  490. return 0;
  491. }
  492. void udebug_entry_add(struct udebug_buf *buf)
  493. {
  494. struct udebug_hdr *hdr = buf->hdr;
  495. struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
  496. uint32_t notify;
  497. uint8_t *data;
  498. /* ensure strings are always 0-terminated */
  499. data = udebug_buf_ptr(buf, ptr->start + ptr->len);
  500. *data = 0;
  501. hdr->data_head = ptr->start + ptr->len + 1;
  502. /* ensure that all data changes are visible before advancing head */
  503. __sync_synchronize();
  504. u32_set(&hdr->head, u32_get(&hdr->head) + 1);
  505. if (!u32_get(&hdr->head))
  506. u32_set(&hdr->head_hi, u32_get(&hdr->head_hi) + 1);
  507. /* ensure that head change is visible */
  508. __sync_synchronize();
  509. notify = __atomic_exchange_n(&hdr->notify, 0, __ATOMIC_RELAXED);
  510. if (notify) {
  511. struct udebug_client_msg msg = {
  512. .type = CL_MSG_RING_NOTIFY,
  513. .id = buf->id,
  514. .notify_mask = notify,
  515. };
  516. blob_buf_init(&b, 0);
  517. udebug_send_msg(buf->ctx, &msg, b.head, -1);
  518. }
  519. }
  520. void udebug_buf_free(struct udebug_buf *buf)
  521. {
  522. struct udebug *ctx = buf->ctx;
  523. if (!list_empty(&buf->list) && buf->list.prev)
  524. list_del(&buf->list);
  525. if (ctx && ctx->fd.fd >= 0)
  526. udebug_buf_msg(buf, CL_MSG_RING_REMOVE);
  527. munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
  528. close(buf->fd);
  529. memset(buf, 0, sizeof(*buf));
  530. }
  531. static void
  532. __udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf)
  533. {
  534. struct udebug_client_msg msg = {
  535. .type = CL_MSG_RING_ADD,
  536. .id = buf->id,
  537. .ring_size = buf->hdr->ring_size,
  538. .data_size = buf->hdr->data_size,
  539. };
  540. const struct udebug_buf_meta *meta = buf->meta;
  541. void *c;
  542. blob_buf_init(&b, 0);
  543. blobmsg_add_string(&b, "name", meta->name);
  544. c = blobmsg_open_array(&b, "flags");
  545. for (size_t i = 0; i < meta->n_flags; i++) {
  546. const struct udebug_buf_flag *flag = &meta->flags[i];
  547. void *e = blobmsg_open_array(&b, NULL);
  548. blobmsg_add_string(&b, NULL, flag->name);
  549. blobmsg_add_u64(&b, NULL, flag->mask);
  550. blobmsg_close_array(&b, e);
  551. }
  552. blobmsg_close_array(&b, c);
  553. udebug_send_msg(ctx, &msg, b.head, buf->fd);
  554. udebug_wait_for_response(ctx, &msg, NULL);
  555. }
  556. int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
  557. const struct udebug_buf_meta *meta)
  558. {
  559. list_add_tail(&buf->list, &ctx->local_rings);
  560. buf->ctx = ctx;
  561. buf->meta = meta;
  562. buf->id = ctx->next_id++;
  563. buf->hdr->format = meta->format;
  564. buf->hdr->sub_format = meta->sub_format;
  565. if (ctx->fd.fd >= 0)
  566. __udebug_buf_add(ctx, buf);
  567. return 0;
  568. }
  569. void udebug_init(struct udebug *ctx)
  570. {
  571. INIT_LIST_HEAD(&ctx->local_rings);
  572. avl_init(&ctx->remote_rings, udebug_id_cmp, true, NULL);
  573. ctx->fd.fd = -1;
  574. ctx->poll_handle = -1;
  575. }
  576. static void udebug_reconnect_cb(struct uloop_timeout *t)
  577. {
  578. struct udebug *ctx = container_of(t, struct udebug, reconnect);
  579. if (udebug_connect(ctx, ctx->socket_path) < 0) {
  580. uloop_timeout_set(&ctx->reconnect, 1000);
  581. return;
  582. }
  583. udebug_add_uloop(ctx);
  584. }
  585. void udebug_auto_connect(struct udebug *ctx, const char *path)
  586. {
  587. free(ctx->socket_path);
  588. ctx->reconnect.cb = udebug_reconnect_cb;
  589. ctx->socket_path = path ? strdup(path) : NULL;
  590. if (ctx->fd.fd >= 0)
  591. return;
  592. udebug_reconnect_cb(&ctx->reconnect);
  593. }
  594. int udebug_connect(struct udebug *ctx, const char *path)
  595. {
  596. struct udebug_remote_buf *rb;
  597. struct udebug_buf *buf;
  598. if (ctx->fd.fd >= 0)
  599. close(ctx->fd.fd);
  600. ctx->fd.fd = -1;
  601. if (!path)
  602. path = UDEBUG_SOCK_NAME;
  603. ctx->fd.fd = usock(USOCK_UNIX, path, NULL);
  604. if (ctx->fd.fd < 0)
  605. return -1;
  606. list_for_each_entry(buf, &ctx->local_rings, list)
  607. __udebug_buf_add(ctx, buf);
  608. avl_for_each_element(&ctx->remote_rings, rb, node) {
  609. if (!rb->poll)
  610. continue;
  611. rb->poll = false;
  612. udebug_remote_buf_set_poll(ctx, rb, true);
  613. }
  614. return 0;
  615. }
  616. void udebug_poll(struct udebug *ctx)
  617. {
  618. while (__udebug_poll(ctx, NULL, false));
  619. }
  620. struct udebug_client_msg *
  621. udebug_send_and_wait(struct udebug *ctx, struct udebug_client_msg *msg, int *rfd)
  622. {
  623. udebug_send_msg(ctx, msg, NULL, -1);
  624. return udebug_wait_for_response(ctx, msg, rfd);
  625. }
  626. static void udebug_fd_cb(struct uloop_fd *fd, unsigned int events)
  627. {
  628. struct udebug *ctx = container_of(fd, struct udebug, fd);
  629. if (fd->eof)
  630. __udebug_disconnect(ctx, true);
  631. udebug_poll(ctx);
  632. }
  633. void udebug_add_uloop(struct udebug *ctx)
  634. {
  635. if (ctx->fd.registered)
  636. return;
  637. ctx->fd.cb = udebug_fd_cb;
  638. uloop_fd_add(&ctx->fd, ULOOP_READ);
  639. }
  640. void udebug_free(struct udebug *ctx)
  641. {
  642. struct udebug_remote_buf *rb, *tmp;
  643. struct udebug_buf *buf;
  644. free(ctx->socket_path);
  645. ctx->socket_path = NULL;
  646. __udebug_disconnect(ctx, false);
  647. uloop_timeout_cancel(&ctx->reconnect);
  648. while (!list_empty(&ctx->local_rings)) {
  649. buf = list_first_entry(&ctx->local_rings, struct udebug_buf, list);
  650. udebug_buf_free(buf);
  651. }
  652. avl_for_each_element_safe(&ctx->remote_rings, rb, node, tmp)
  653. udebug_remote_buf_unmap(ctx, rb);
  654. }