ctf-dump.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /* Textual dumping of CTF data.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of libctf.
  4. libctf is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ctf-impl.h>
  16. #include <string.h>
  17. #define str_append(s, a) ctf_str_append_noerr (s, a)
  18. /* One item to be dumped, in string form. */
  19. typedef struct ctf_dump_item
  20. {
  21. ctf_list_t cdi_list;
  22. char *cdi_item;
  23. } ctf_dump_item_t;
  24. /* Cross-call state for dumping. Basically just enough to track the section in
  25. use and a list of return strings. */
  26. struct ctf_dump_state
  27. {
  28. ctf_sect_names_t cds_sect;
  29. ctf_dict_t *cds_fp;
  30. ctf_dump_item_t *cds_current;
  31. ctf_list_t cds_items;
  32. };
  33. /* Cross-call state for ctf_dump_member. */
  34. typedef struct ctf_dump_membstate
  35. {
  36. char **cdm_str;
  37. ctf_dict_t *cdm_fp;
  38. const char *cdm_toplevel_indent;
  39. } ctf_dump_membstate_t;
  40. static int
  41. ctf_dump_append (ctf_dump_state_t *state, char *str)
  42. {
  43. ctf_dump_item_t *cdi;
  44. if ((cdi = malloc (sizeof (struct ctf_dump_item))) == NULL)
  45. return (ctf_set_errno (state->cds_fp, ENOMEM));
  46. cdi->cdi_item = str;
  47. ctf_list_append (&state->cds_items, cdi);
  48. return 0;
  49. }
  50. static void
  51. ctf_dump_free (ctf_dump_state_t *state)
  52. {
  53. ctf_dump_item_t *cdi, *next_cdi;
  54. if (state == NULL)
  55. return;
  56. for (cdi = ctf_list_next (&state->cds_items); cdi != NULL;
  57. cdi = next_cdi)
  58. {
  59. free (cdi->cdi_item);
  60. next_cdi = ctf_list_next (cdi);
  61. free (cdi);
  62. }
  63. }
  64. /* Return a dump for a single type, without member info: but do optionally show
  65. the type's references. */
  66. #define CTF_FT_REFS 0x2 /* Print referenced types. */
  67. #define CTF_FT_BITFIELD 0x4 /* Print :BITS if a bitfield. */
  68. #define CTF_FT_ID 0x8 /* Print "ID: " in front of type IDs. */
  69. static char *
  70. ctf_dump_format_type (ctf_dict_t *fp, ctf_id_t id, int flag)
  71. {
  72. ctf_id_t new_id;
  73. char *str = NULL, *bit = NULL, *buf = NULL;
  74. ctf_set_errno (fp, 0);
  75. new_id = id;
  76. do
  77. {
  78. ctf_encoding_t ep;
  79. ctf_arinfo_t ar;
  80. int kind, unsliced_kind;
  81. ssize_t size, align;
  82. const char *nonroot_leader = "";
  83. const char *nonroot_trailer = "";
  84. const char *idstr = "";
  85. id = new_id;
  86. if (flag == CTF_ADD_NONROOT)
  87. {
  88. nonroot_leader = "{";
  89. nonroot_trailer = "}";
  90. }
  91. buf = ctf_type_aname (fp, id);
  92. if (!buf)
  93. {
  94. if (id == 0 || ctf_errno (fp) == ECTF_NONREPRESENTABLE)
  95. {
  96. ctf_set_errno (fp, ECTF_NONREPRESENTABLE);
  97. str = str_append (str, " (type not represented in CTF)");
  98. return str;
  99. }
  100. goto err;
  101. }
  102. if (flag & CTF_FT_ID)
  103. idstr = "ID ";
  104. if (asprintf (&bit, "%s%s0x%lx: (kind %i) ", nonroot_leader, idstr,
  105. id, ctf_type_kind (fp, id)) < 0)
  106. goto oom;
  107. str = str_append (str, bit);
  108. free (bit);
  109. bit = NULL;
  110. if (buf[0] != '\0')
  111. str = str_append (str, buf);
  112. free (buf);
  113. buf = NULL;
  114. unsliced_kind = ctf_type_kind_unsliced (fp, id);
  115. kind = ctf_type_kind (fp, id);
  116. /* Report encodings of everything with an encoding other than enums:
  117. base-type enums cannot have a nonzero cte_offset or cte_bits value.
  118. (Slices of them can, but they are of kind CTF_K_SLICE.) */
  119. if (unsliced_kind != CTF_K_ENUM && ctf_type_encoding (fp, id, &ep) == 0)
  120. {
  121. if ((ssize_t) ep.cte_bits != ctf_type_size (fp, id) * CHAR_BIT
  122. && flag & CTF_FT_BITFIELD)
  123. {
  124. if (asprintf (&bit, ":%i", ep.cte_bits) < 0)
  125. goto oom;
  126. str = str_append (str, bit);
  127. free (bit);
  128. bit = NULL;
  129. }
  130. if ((ssize_t) ep.cte_bits != ctf_type_size (fp, id) * CHAR_BIT
  131. || ep.cte_offset != 0)
  132. {
  133. const char *slice = "";
  134. if (unsliced_kind == CTF_K_SLICE)
  135. slice = "slice ";
  136. if (asprintf (&bit, " [%s0x%x:0x%x]",
  137. slice, ep.cte_offset, ep.cte_bits) < 0)
  138. goto oom;
  139. str = str_append (str, bit);
  140. free (bit);
  141. bit = NULL;
  142. }
  143. if (asprintf (&bit, " (format 0x%x)", ep.cte_format) < 0)
  144. goto oom;
  145. str = str_append (str, bit);
  146. free (bit);
  147. bit = NULL;
  148. }
  149. size = ctf_type_size (fp, id);
  150. if (kind != CTF_K_FUNCTION && size >= 0)
  151. {
  152. if (asprintf (&bit, " (size 0x%lx)", (unsigned long int) size) < 0)
  153. goto oom;
  154. str = str_append (str, bit);
  155. free (bit);
  156. bit = NULL;
  157. }
  158. align = ctf_type_align (fp, id);
  159. if (align >= 0)
  160. {
  161. if (asprintf (&bit, " (aligned at 0x%lx)",
  162. (unsigned long int) align) < 0)
  163. goto oom;
  164. str = str_append (str, bit);
  165. free (bit);
  166. bit = NULL;
  167. }
  168. if (nonroot_trailer[0] != 0)
  169. str = str_append (str, nonroot_trailer);
  170. /* Just exit after one iteration if we are not showing the types this type
  171. references. */
  172. if (!(flag & CTF_FT_REFS))
  173. return str;
  174. /* Keep going as long as this type references another. We consider arrays
  175. to "reference" their element type. */
  176. if (kind == CTF_K_ARRAY)
  177. {
  178. if (ctf_array_info (fp, id, &ar) < 0)
  179. goto err;
  180. new_id = ar.ctr_contents;
  181. }
  182. else
  183. new_id = ctf_type_reference (fp, id);
  184. if (new_id != CTF_ERR)
  185. str = str_append (str, " -> ");
  186. }
  187. while (new_id != CTF_ERR);
  188. if (ctf_errno (fp) != ECTF_NOTREF)
  189. {
  190. free (str);
  191. return NULL;
  192. }
  193. return str;
  194. oom:
  195. ctf_set_errno (fp, errno);
  196. err:
  197. ctf_err_warn (fp, 1, 0, _("cannot format name dumping type 0x%lx"), id);
  198. free (buf);
  199. free (str);
  200. free (bit);
  201. return NULL;
  202. }
  203. /* Dump one string field from the file header into the cds_items. */
  204. static int
  205. ctf_dump_header_strfield (ctf_dict_t *fp, ctf_dump_state_t *state,
  206. const char *name, uint32_t value)
  207. {
  208. char *str;
  209. if (value)
  210. {
  211. if (asprintf (&str, "%s: %s\n", name, ctf_strptr (fp, value)) < 0)
  212. goto err;
  213. ctf_dump_append (state, str);
  214. }
  215. return 0;
  216. err:
  217. return (ctf_set_errno (fp, errno));
  218. }
  219. /* Dump one section-offset field from the file header into the cds_items. */
  220. static int
  221. ctf_dump_header_sectfield (ctf_dict_t *fp, ctf_dump_state_t *state,
  222. const char *sect, uint32_t off, uint32_t nextoff)
  223. {
  224. char *str;
  225. if (nextoff - off)
  226. {
  227. if (asprintf (&str, "%s:\t0x%lx -- 0x%lx (0x%lx bytes)\n", sect,
  228. (unsigned long) off, (unsigned long) (nextoff - 1),
  229. (unsigned long) (nextoff - off)) < 0)
  230. goto err;
  231. ctf_dump_append (state, str);
  232. }
  233. return 0;
  234. err:
  235. return (ctf_set_errno (fp, errno));
  236. }
  237. /* Dump the file header into the cds_items. */
  238. static int
  239. ctf_dump_header (ctf_dict_t *fp, ctf_dump_state_t *state)
  240. {
  241. char *str;
  242. char *flagstr = NULL;
  243. const ctf_header_t *hp = fp->ctf_header;
  244. const char *vertab[] =
  245. {
  246. NULL, "CTF_VERSION_1",
  247. "CTF_VERSION_1_UPGRADED_3 (latest format, version 1 type "
  248. "boundaries)",
  249. "CTF_VERSION_2",
  250. "CTF_VERSION_3", NULL
  251. };
  252. const char *verstr = NULL;
  253. if (asprintf (&str, "Magic number: 0x%x\n", hp->cth_magic) < 0)
  254. goto err;
  255. ctf_dump_append (state, str);
  256. if (hp->cth_version <= CTF_VERSION)
  257. verstr = vertab[hp->cth_version];
  258. if (verstr == NULL)
  259. verstr = "(not a valid version)";
  260. if (asprintf (&str, "Version: %i (%s)\n", hp->cth_version,
  261. verstr) < 0)
  262. goto err;
  263. ctf_dump_append (state, str);
  264. /* Everything else is only printed if present. */
  265. /* The flags are unusual in that they represent the ctf_dict_t *in memory*:
  266. flags representing compression, etc, are turned off as the file is
  267. decompressed. So we store a copy of the flags before they are changed, for
  268. the dumper. */
  269. if (fp->ctf_openflags > 0)
  270. {
  271. if (asprintf (&flagstr, "%s%s%s%s%s%s%s",
  272. fp->ctf_openflags & CTF_F_COMPRESS
  273. ? "CTF_F_COMPRESS": "",
  274. (fp->ctf_openflags & CTF_F_COMPRESS)
  275. && (fp->ctf_openflags & ~CTF_F_COMPRESS)
  276. ? ", " : "",
  277. fp->ctf_openflags & CTF_F_NEWFUNCINFO
  278. ? "CTF_F_NEWFUNCINFO" : "",
  279. (fp->ctf_openflags & (CTF_F_COMPRESS | CTF_F_NEWFUNCINFO))
  280. && (fp->ctf_openflags & ~(CTF_F_COMPRESS | CTF_F_NEWFUNCINFO))
  281. ? ", " : "",
  282. fp->ctf_openflags & CTF_F_IDXSORTED
  283. ? "CTF_F_IDXSORTED" : "",
  284. fp->ctf_openflags & (CTF_F_COMPRESS | CTF_F_NEWFUNCINFO
  285. | CTF_F_IDXSORTED)
  286. && (fp->ctf_openflags & ~(CTF_F_COMPRESS | CTF_F_NEWFUNCINFO
  287. | CTF_F_IDXSORTED))
  288. ? ", " : "",
  289. fp->ctf_openflags & CTF_F_DYNSTR
  290. ? "CTF_F_DYNSTR" : "") < 0)
  291. goto err;
  292. if (asprintf (&str, "Flags: 0x%x (%s)", fp->ctf_openflags, flagstr) < 0)
  293. goto err;
  294. ctf_dump_append (state, str);
  295. }
  296. if (ctf_dump_header_strfield (fp, state, "Parent label",
  297. hp->cth_parlabel) < 0)
  298. goto err;
  299. if (ctf_dump_header_strfield (fp, state, "Parent name", hp->cth_parname) < 0)
  300. goto err;
  301. if (ctf_dump_header_strfield (fp, state, "Compilation unit name",
  302. hp->cth_cuname) < 0)
  303. goto err;
  304. if (ctf_dump_header_sectfield (fp, state, "Label section", hp->cth_lbloff,
  305. hp->cth_objtoff) < 0)
  306. goto err;
  307. if (ctf_dump_header_sectfield (fp, state, "Data object section",
  308. hp->cth_objtoff, hp->cth_funcoff) < 0)
  309. goto err;
  310. if (ctf_dump_header_sectfield (fp, state, "Function info section",
  311. hp->cth_funcoff, hp->cth_objtidxoff) < 0)
  312. goto err;
  313. if (ctf_dump_header_sectfield (fp, state, "Object index section",
  314. hp->cth_objtidxoff, hp->cth_funcidxoff) < 0)
  315. goto err;
  316. if (ctf_dump_header_sectfield (fp, state, "Function index section",
  317. hp->cth_funcidxoff, hp->cth_varoff) < 0)
  318. goto err;
  319. if (ctf_dump_header_sectfield (fp, state, "Variable section",
  320. hp->cth_varoff, hp->cth_typeoff) < 0)
  321. goto err;
  322. if (ctf_dump_header_sectfield (fp, state, "Type section",
  323. hp->cth_typeoff, hp->cth_stroff) < 0)
  324. goto err;
  325. if (ctf_dump_header_sectfield (fp, state, "String section", hp->cth_stroff,
  326. hp->cth_stroff + hp->cth_strlen + 1) < 0)
  327. goto err;
  328. return 0;
  329. err:
  330. free (flagstr);
  331. return (ctf_set_errno (fp, errno));
  332. }
  333. /* Dump a single label into the cds_items. */
  334. static int
  335. ctf_dump_label (const char *name, const ctf_lblinfo_t *info,
  336. void *arg)
  337. {
  338. char *str;
  339. char *typestr;
  340. ctf_dump_state_t *state = arg;
  341. if (asprintf (&str, "%s -> ", name) < 0)
  342. return (ctf_set_errno (state->cds_fp, errno));
  343. if ((typestr = ctf_dump_format_type (state->cds_fp, info->ctb_type,
  344. CTF_ADD_ROOT | CTF_FT_REFS)) == NULL)
  345. {
  346. free (str);
  347. return 0; /* Swallow the error. */
  348. }
  349. str = str_append (str, typestr);
  350. free (typestr);
  351. ctf_dump_append (state, str);
  352. return 0;
  353. }
  354. /* Dump all the object or function entries into the cds_items. */
  355. static int
  356. ctf_dump_objts (ctf_dict_t *fp, ctf_dump_state_t *state, int functions)
  357. {
  358. const char *name;
  359. ctf_id_t id;
  360. ctf_next_t *i = NULL;
  361. char *str = NULL;
  362. if ((functions && fp->ctf_funcidx_names)
  363. || (!functions && fp->ctf_objtidx_names))
  364. str = str_append (str, _("Section is indexed.\n"));
  365. else if (fp->ctf_symtab.cts_data == NULL)
  366. str = str_append (str, _("No symbol table.\n"));
  367. while ((id = ctf_symbol_next (fp, &i, &name, functions)) != CTF_ERR)
  368. {
  369. char *typestr = NULL;
  370. /* Emit the name, if we know it. No trailing space: ctf_dump_format_type
  371. has a leading one. */
  372. if (name)
  373. {
  374. if (asprintf (&str, "%s -> ", name) < 0)
  375. goto oom;
  376. }
  377. else
  378. str = xstrdup ("");
  379. if ((typestr = ctf_dump_format_type (state->cds_fp, id,
  380. CTF_ADD_ROOT | CTF_FT_REFS)) == NULL)
  381. {
  382. ctf_dump_append (state, str);
  383. continue; /* Swallow the error. */
  384. }
  385. str = str_append (str, typestr);
  386. free (typestr);
  387. ctf_dump_append (state, str);
  388. continue;
  389. oom:
  390. ctf_set_errno (fp, ENOMEM);
  391. ctf_next_destroy (i);
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. /* Dump a single variable into the cds_items. */
  397. static int
  398. ctf_dump_var (const char *name, ctf_id_t type, void *arg)
  399. {
  400. char *str;
  401. char *typestr;
  402. ctf_dump_state_t *state = arg;
  403. if (asprintf (&str, "%s -> ", name) < 0)
  404. return (ctf_set_errno (state->cds_fp, errno));
  405. if ((typestr = ctf_dump_format_type (state->cds_fp, type,
  406. CTF_ADD_ROOT | CTF_FT_REFS)) == NULL)
  407. {
  408. free (str);
  409. return 0; /* Swallow the error. */
  410. }
  411. str = str_append (str, typestr);
  412. free (typestr);
  413. ctf_dump_append (state, str);
  414. return 0;
  415. }
  416. /* Dump a single struct/union member into the string in the membstate. */
  417. static int
  418. ctf_dump_member (const char *name, ctf_id_t id, unsigned long offset,
  419. int depth, void *arg)
  420. {
  421. ctf_dump_membstate_t *state = arg;
  422. char *typestr = NULL;
  423. char *bit = NULL;
  424. /* The struct/union itself has already been printed. */
  425. if (depth == 0)
  426. return 0;
  427. if (asprintf (&bit, "%s%*s", state->cdm_toplevel_indent, (depth-1)*4, "") < 0)
  428. goto oom;
  429. *state->cdm_str = str_append (*state->cdm_str, bit);
  430. free (bit);
  431. if ((typestr = ctf_dump_format_type (state->cdm_fp, id,
  432. CTF_ADD_ROOT | CTF_FT_BITFIELD
  433. | CTF_FT_ID)) == NULL)
  434. return -1; /* errno is set for us. */
  435. if (asprintf (&bit, "[0x%lx] %s: %s\n", offset, name, typestr) < 0)
  436. goto oom;
  437. *state->cdm_str = str_append (*state->cdm_str, bit);
  438. free (typestr);
  439. free (bit);
  440. typestr = NULL;
  441. bit = NULL;
  442. return 0;
  443. oom:
  444. free (typestr);
  445. free (bit);
  446. return (ctf_set_errno (state->cdm_fp, errno));
  447. }
  448. /* Report the number of digits in the hexadecimal representation of a type
  449. ID. */
  450. static int
  451. type_hex_digits (ctf_id_t id)
  452. {
  453. int i = 0;
  454. if (id == 0)
  455. return 1;
  456. for (; id > 0; id >>= 4, i++);
  457. return i;
  458. }
  459. /* Dump a single type into the cds_items. */
  460. static int
  461. ctf_dump_type (ctf_id_t id, int flag, void *arg)
  462. {
  463. char *str;
  464. char *indent;
  465. ctf_dump_state_t *state = arg;
  466. ctf_dump_membstate_t membstate = { &str, state->cds_fp, NULL };
  467. /* Indent neatly. */
  468. if (asprintf (&indent, " %*s", type_hex_digits (id), "") < 0)
  469. return (ctf_set_errno (state->cds_fp, ENOMEM));
  470. /* Dump the type itself. */
  471. if ((str = ctf_dump_format_type (state->cds_fp, id,
  472. flag | CTF_FT_REFS)) == NULL)
  473. goto err;
  474. str = str_append (str, "\n");
  475. membstate.cdm_toplevel_indent = indent;
  476. /* Member dumping for structs, unions... */
  477. if (ctf_type_kind (state->cds_fp, id) == CTF_K_STRUCT
  478. || ctf_type_kind (state->cds_fp, id) == CTF_K_UNION)
  479. {
  480. if ((ctf_type_visit (state->cds_fp, id, ctf_dump_member, &membstate)) < 0)
  481. {
  482. if (id == 0 || ctf_errno (state->cds_fp) == ECTF_NONREPRESENTABLE)
  483. {
  484. ctf_dump_append (state, str);
  485. return 0;
  486. }
  487. ctf_err_warn (state->cds_fp, 1, ctf_errno (state->cds_fp),
  488. _("cannot visit members dumping type 0x%lx"), id);
  489. goto err;
  490. }
  491. }
  492. /* ... and enums, for which we dump the first and last few members and skip
  493. the ones in the middle. */
  494. if (ctf_type_kind (state->cds_fp, id) == CTF_K_ENUM)
  495. {
  496. int enum_count = ctf_member_count (state->cds_fp, id);
  497. ctf_next_t *it = NULL;
  498. int i = 0;
  499. const char *enumerand;
  500. char *bit;
  501. int value;
  502. while ((enumerand = ctf_enum_next (state->cds_fp, id,
  503. &it, &value)) != NULL)
  504. {
  505. i++;
  506. if ((i > 5) && (i < enum_count - 4))
  507. continue;
  508. str = str_append (str, indent);
  509. if (asprintf (&bit, "%s: %i\n", enumerand, value) < 0)
  510. {
  511. ctf_next_destroy (it);
  512. goto oom;
  513. }
  514. str = str_append (str, bit);
  515. free (bit);
  516. if ((i == 5) && (enum_count > 10))
  517. {
  518. str = str_append (str, indent);
  519. str = str_append (str, "...\n");
  520. }
  521. }
  522. if (ctf_errno (state->cds_fp) != ECTF_NEXT_END)
  523. {
  524. ctf_err_warn (state->cds_fp, 1, ctf_errno (state->cds_fp),
  525. _("cannot visit enumerands dumping type 0x%lx"), id);
  526. goto err;
  527. }
  528. }
  529. ctf_dump_append (state, str);
  530. free (indent);
  531. return 0;
  532. err:
  533. free (indent);
  534. free (str);
  535. /* Swallow the error: don't cause an error in one type to abort all
  536. type dumping. */
  537. return 0;
  538. oom:
  539. free (indent);
  540. free (str);
  541. return ctf_set_errno (state->cds_fp, ENOMEM);
  542. }
  543. /* Dump the string table into the cds_items. */
  544. static int
  545. ctf_dump_str (ctf_dict_t *fp, ctf_dump_state_t *state)
  546. {
  547. const char *s = fp->ctf_str[CTF_STRTAB_0].cts_strs;
  548. for (; s < fp->ctf_str[CTF_STRTAB_0].cts_strs +
  549. fp->ctf_str[CTF_STRTAB_0].cts_len;)
  550. {
  551. char *str;
  552. if (asprintf (&str, "0x%lx: %s",
  553. (unsigned long) (s - fp->ctf_str[CTF_STRTAB_0].cts_strs),
  554. s) < 0)
  555. return (ctf_set_errno (fp, errno));
  556. ctf_dump_append (state, str);
  557. s += strlen (s) + 1;
  558. }
  559. return 0;
  560. }
  561. /* Dump a particular section of a CTF file, in textual form. Call with a
  562. pointer to a NULL STATE: each call emits a dynamically allocated string
  563. containing a description of one entity in the specified section, in order.
  564. Only the first call (with a NULL state) may vary SECT. Once the CTF section
  565. has been entirely dumped, the call returns NULL and frees and annuls the
  566. STATE, ready for another section to be dumped. The returned textual content
  567. may span multiple lines: between each call the FUNC is called with one
  568. textual line at a time, and should return a suitably decorated line (it can
  569. allocate a new one and return it if it likes). */
  570. char *
  571. ctf_dump (ctf_dict_t *fp, ctf_dump_state_t **statep, ctf_sect_names_t sect,
  572. ctf_dump_decorate_f *func, void *arg)
  573. {
  574. char *str;
  575. char *line;
  576. ctf_dump_state_t *state = NULL;
  577. if (*statep == NULL)
  578. {
  579. /* Data collection. Transforming a call-at-a-time iterator into a
  580. return-at-a-time iterator in a language without call/cc is annoying. It
  581. is easiest to simply collect everything at once and then return it bit
  582. by bit. The first call will take (much) longer than otherwise, but the
  583. amortized time needed is the same. */
  584. if ((*statep = malloc (sizeof (struct ctf_dump_state))) == NULL)
  585. {
  586. ctf_set_errno (fp, ENOMEM);
  587. goto end;
  588. }
  589. state = *statep;
  590. memset (state, 0, sizeof (struct ctf_dump_state));
  591. state->cds_fp = fp;
  592. state->cds_sect = sect;
  593. switch (sect)
  594. {
  595. case CTF_SECT_HEADER:
  596. ctf_dump_header (fp, state);
  597. break;
  598. case CTF_SECT_LABEL:
  599. if (ctf_label_iter (fp, ctf_dump_label, state) < 0)
  600. {
  601. if (ctf_errno (fp) != ECTF_NOLABELDATA)
  602. goto end; /* errno is set for us. */
  603. ctf_set_errno (fp, 0);
  604. }
  605. break;
  606. case CTF_SECT_OBJT:
  607. if (ctf_dump_objts (fp, state, 0) < 0)
  608. goto end; /* errno is set for us. */
  609. break;
  610. case CTF_SECT_FUNC:
  611. if (ctf_dump_objts (fp, state, 1) < 0)
  612. goto end; /* errno is set for us. */
  613. break;
  614. case CTF_SECT_VAR:
  615. if (ctf_variable_iter (fp, ctf_dump_var, state) < 0)
  616. goto end; /* errno is set for us. */
  617. break;
  618. case CTF_SECT_TYPE:
  619. if (ctf_type_iter_all (fp, ctf_dump_type, state) < 0)
  620. goto end; /* errno is set for us. */
  621. break;
  622. case CTF_SECT_STR:
  623. ctf_dump_str (fp, state);
  624. break;
  625. default:
  626. ctf_set_errno (fp, ECTF_DUMPSECTUNKNOWN);
  627. goto end;
  628. }
  629. }
  630. else
  631. {
  632. state = *statep;
  633. if (state->cds_sect != sect)
  634. {
  635. ctf_set_errno (fp, ECTF_DUMPSECTCHANGED);
  636. goto end;
  637. }
  638. }
  639. if (state->cds_current == NULL)
  640. state->cds_current = ctf_list_next (&state->cds_items);
  641. else
  642. state->cds_current = ctf_list_next (state->cds_current);
  643. if (state->cds_current == NULL)
  644. goto end;
  645. /* Hookery. There is some extra complexity to preserve linefeeds within each
  646. item while removing linefeeds at the end. */
  647. if (func)
  648. {
  649. size_t len;
  650. str = NULL;
  651. for (line = state->cds_current->cdi_item; line && *line; )
  652. {
  653. char *nline = line;
  654. char *ret;
  655. nline = strchr (line, '\n');
  656. if (nline)
  657. nline[0] = '\0';
  658. ret = func (sect, line, arg);
  659. str = str_append (str, ret);
  660. str = str_append (str, "\n");
  661. if (ret != line)
  662. free (ret);
  663. if (nline)
  664. {
  665. nline[0] = '\n';
  666. nline++;
  667. }
  668. line = nline;
  669. }
  670. len = strlen (str);
  671. if (str[len-1] == '\n')
  672. str[len-1] = '\0';
  673. }
  674. else
  675. {
  676. str = strdup (state->cds_current->cdi_item);
  677. if (!str)
  678. {
  679. ctf_set_errno (fp, ENOMEM);
  680. return str;
  681. }
  682. }
  683. ctf_set_errno (fp, 0);
  684. return str;
  685. end:
  686. ctf_dump_free (state);
  687. free (state);
  688. ctf_set_errno (fp, 0);
  689. *statep = NULL;
  690. return NULL;
  691. }