Coverage Report

Created: 2026-03-10 08:46

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