File: print_debug_gnu.c

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (525 lines) | stat: -rw-r--r-- 17,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
Copyright (C) 2020 David Anderson. All Rights Reserved.

  Redistribution and use in source and binary forms, with
  or without modification, are permitted provided that the
  following conditions are met:

    Redistributions of source code must retain the above
    copyright notice, this list of conditions and the following
    disclaimer.

    Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials
    provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*  To print .debug_gnu_pubnames, .debug_gnu_typenames */
#include "globals.h"
#ifdef HAVE_STDINT_H
#include <stdint.h> /* For uintptr_t */
#endif /* HAVE_STDINT_H */
#include "naming.h"
#include "esb.h"                /* For flexible string buffer. */
#include "esb_using_functions.h"
#include "sanitized.h"
#include  "print_debug_gnu.h"

#define TRUE 1
#define FALSE 0

char *ikind_types[8] = {
    "none",
    "type",
    "variable",
    "function",
    "other",
    "unknown5",
    "unknown6",
    "unknown7" };


static int
print_block_entries(
    UNUSEDARG Dwarf_Debug dbg,
    UNUSEDARG Dwarf_Bool for_pubnames,
    UNUSEDARG struct esb_s * secname,
    Dwarf_Gnu_Index_Head head,
    Dwarf_Unsigned blocknum,
    Dwarf_Unsigned entrycount,
    Dwarf_Error *error)
{
    Dwarf_Unsigned i = 0;
    int res = 0;


    printf("    [   ] offset     Kind        Name\n");

    for ( ; i < entrycount; ++i) {
        Dwarf_Unsigned offset_in_debug_info = 0;
        const char *name = 0;
        unsigned char flag = 0;
        unsigned char staticorglobal = 0;
        unsigned char typeofentry = 0;
        /*  flag is all 8 bits and staticorglobal
            and typeofentry were extracted from the flag.
            Present here so we can check all 8 bits
            are correct (lowest 4 should be zero).  */

        res = dwarf_get_gnu_index_block_entry(head,
            blocknum,i,&offset_in_debug_info,
            &name,&flag,&staticorglobal,&typeofentry,
            error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
        if (res == DW_DLV_NO_ENTRY) {
            printf("  ERROR: Block %" DW_PR_DUu
                " entry %" DW_PR_DUu
                " does not exist though entry count"
                " is %" DW_PR_DUu
                ", something is wrong\n",
                blocknum,
                i,entrycount);
            glflags.gf_count_major_errors++;
            return res;
        }
        printf("    [%3" DW_PR_DUu "] 0x%" DW_PR_XZEROS DW_PR_DUx,
            i,offset_in_debug_info);
        printf(" %s,%-8s",
            staticorglobal?"s":"g",
            ikind_types[0x7 & typeofentry]);
        printf(" %s",sanitized(name));
        printf("\n");
        if (flag&0xf) {
            printf("  ERROR: Block %" DW_PR_DUu
                " entry %" DW_PR_DUu " flag 0x%x. "
                "The lower bits are non-zero "
                "so there may be a corruption problem.",
                blocknum,i, flag);
            glflags.gf_count_major_errors++;
            printf("\n");
        }
    }
    return DW_DLV_OK;
}

int attrlist[] = {
DW_AT_GNU_dwo_name,
DW_AT_dwo_name,
DW_AT_comp_dir,
DW_AT_GNU_dwo_id,
0 };

static void
error_report(int errcode,
    const char *text,
    Dwarf_Error *error) {
    if (errcode == DW_DLV_ERROR) {
        printf("  ERROR: %s"
            ", ignoring other attributes here: %s\n",
            text,
            dwarf_errmsg(*error));
        glflags.gf_count_major_errors++;
        return;
    } else {
        printf("  ERROR impossible DW_DLV_NO_ENTRY: %s"
            ", ignoringother attributes here. \n",
            text);
    }
    return;
}
static void
print_selected_attributes(Dwarf_Debug dbg,
    Dwarf_Die die,
    Dwarf_Half version,
    Dwarf_Half offset_size,
    Dwarf_Error *error)
{
    int res = 0;
    int i = 0; 

    for ( ; attrlist[i]; ++i) {
        Dwarf_Attribute attr = 0;
        int attrid = 0;
        const char * atname = 0;
        Dwarf_Half form = 0;
        enum Dwarf_Form_Class fclass = 0;
        char *formstring = 0;
        int print_str = FALSE;
        struct esb_s m;
        Dwarf_Sig8 sig;

        attrid = attrlist[i];
        res = dwarf_attr(die,attrid,&attr,error);
        if (res == DW_DLV_ERROR) {
            error_report(res,"dwarf_attr() returned error",error);
            dwarf_dealloc_error(dbg,*error);
            *error = 0;
            return;
        }
        if (res == DW_DLV_NO_ENTRY) {
            continue;
        }
        /* ok, this attribute is present */
        atname = get_AT_name(attrid,FALSE);
        res = dwarf_whatform(attr,&form,error);
        if (res != DW_DLV_OK) {
            error_report(res,"dwarf_whatform() problem: ",error);
            dwarf_dealloc_error(dbg,*error);
            dwarf_dealloc_attribute(attr);
            *error = 0;
            return;
        }
        if (res == DW_DLV_NO_ENTRY) {
            /* impossible, cannot get here */
            dwarf_dealloc_attribute(attr);
            continue;
        }
        esb_constructor(&m);
        fclass = dwarf_get_form_class(version,attrid,
            offset_size,form);
        if (fclass == DW_FORM_CLASS_STRING) {
            res = dwarf_formstring(attr,&formstring,error);
            if (res == DW_DLV_OK) {
                print_str = TRUE; 
                esb_append(&m,formstring);
            } else {
                error_report(res,"dwarf_formstring() returned error",
                    error);
                if (res == DW_DLV_ERROR) {
                    dwarf_dealloc_error(dbg,*error);
                    *error = 0;
               }
               dwarf_dealloc_attribute(attr);
               esb_destructor(&m);
               break;
            }
        } else if (fclass == DW_FORM_CLASS_CONSTANT)  {
            if (form == DW_FORM_data8) {
                res = dwarf_formsig8_const(attr,&sig,error);
                if (res == DW_DLV_OK) {
                    print_str = TRUE; 
                    format_sig8_string(&sig,&m);
                } else {
                    error_report(res,"dwarf_formsig8_const() "
                        "returned error",error);
                    if (res == DW_DLV_ERROR) {
                        dwarf_dealloc_error(dbg,*error);
                        *error = 0;
                    }
                    esb_destructor(&m);
                    dwarf_dealloc_attribute(attr);
                    break;
                }
            }
        } else if (fclass == DW_FORM_CLASS_REFERENCE) {
            /* includes DW_FORM_ref_sig8, DW_FORM_ref* */
            if (form == DW_FORM_ref_sig8) {
                res = dwarf_formsig8(attr,&sig,error);
                if (res == DW_DLV_OK) {
                    print_str = TRUE; 
                    esb_constructor(&m);
                    format_sig8_string(&sig,&m);
                } else {
                    error_report(res,"dwarf_formsig8() "
                        "problem error",error);
                    if (res == DW_DLV_ERROR) {
                        dwarf_dealloc_error(dbg,*error);
                        *error = 0;
                    }
                    esb_destructor(&m);
                    dwarf_dealloc_attribute(attr);
                    break;
                }
            }
        }
        if (print_str) {
            printf("  %-18s                  : %s\n",
                atname,esb_get_string(&m));
        }
        dwarf_dealloc_attribute(attr);
        esb_destructor(&m);
    }
}


static int
print_die_basics(Dwarf_Debug dbg,
    Dwarf_Die die,
    UNUSEDARG Dwarf_Unsigned cudie_goff,
    Dwarf_Error *error)
{
    int res = 0;
    Dwarf_Half tag = 0;
    Dwarf_Half version = 0;
    Dwarf_Bool is_info = 0;
    Dwarf_Bool is_dwo = 0;
    Dwarf_Half offset_size = 0;
    Dwarf_Half address_size = 0;
    Dwarf_Half extension_size = 0;
    Dwarf_Sig8 *signature = 0;
    Dwarf_Off  offset_of_length = 0;
    Dwarf_Unsigned  total_byte_length = 0;

    res = dwarf_cu_header_basics(die, &version,
        &is_info,&is_dwo,
        &offset_size,&address_size,&extension_size,
        &signature,&offset_of_length,&total_byte_length,error);
    if (res != DW_DLV_OK) {
        if (res == DW_DLV_ERROR) {
            printf("ERROR: Cannot access compilation unit data: %s",
               dwarf_errmsg(*error));
            dwarf_dealloc_error(dbg,*error);
            *error = 0;
        } else {
            printf("ERROR:  Cannot access compilation unit data"
               "No such found");
        }
        glflags.gf_count_major_errors++;
        return DW_DLV_OK;
    }
    printf("  Compilation unit data follows\n");
    printf("  CU version                          : %d\n",version); 
    if (!is_info) {
        printf("  CU  section is .debug_types");
    }
    printf("  CU section is dwo?                  : %s\n",
        is_dwo?"yes":"no");
    printf("  CU offset size                      : %u\n",
        offset_size);
    printf("  CU extension size                   : %u\n",
        extension_size);
    printf("  CU address size                     : %u\n",
        address_size);
    printf("  CU beginning offset                 : 0x%"
        DW_PR_XZEROS DW_PR_DUx "\n",
        offset_of_length);
    printf("  CU total length                     : 0x%"
        DW_PR_XZEROS DW_PR_DUx "\n",
        total_byte_length);

    if (signature) {
        struct esb_s m;
        char buf[24];

        esb_constructor_fixed(&m,buf,sizeof(buf));
        printf("  CU signature                        : ");
        format_sig8_string(signature,&m);
        printf("%s\n", esb_get_string(&m));
        esb_destructor(&m);
    }
    res = dwarf_tag(die,&tag,error);
    if (res != DW_DLV_OK) {
        if (res == DW_DLV_ERROR) {
            printf("ERROR: Cannot access DIE tag  ERROR: %s\n",
               dwarf_errmsg(*error));
            dwarf_dealloc_error(dbg,*error);
            *error = 0;
        } else {
            printf("ERROR:  Cannot access DIE tag "
               "No such found\n");
        }
        printf("\n");
        glflags.gf_count_major_errors++;
    } else {
        const char *actual_tag_name = 0;

        actual_tag_name = get_TAG_name(tag,FALSE);
        printf("  CU die TAG                          : "
            "%s\n", actual_tag_name);
    }
    print_selected_attributes(dbg,die,version,offset_size,error);
    return DW_DLV_OK;
}

static int
print_all_blocks(Dwarf_Debug dbg,
    Dwarf_Bool           for_pubnames,
    struct esb_s        *secname,
    Dwarf_Gnu_Index_Head head,
    Dwarf_Unsigned       block_count,
    Dwarf_Error         *error)
{
    Dwarf_Unsigned i = 0;
    int res = 0;

    for ( ; i < block_count; ++i) {
        Dwarf_Unsigned block_length            = 0;
        Dwarf_Half version                     = 0;
        Dwarf_Unsigned offset_into_debug_info  = 0;
        Dwarf_Unsigned size_of_debug_info_area = 0;
        Dwarf_Unsigned entrycount              = 0;

        res = dwarf_get_gnu_index_block(head,i,
            &block_length,&version,
            &offset_into_debug_info,
            &size_of_debug_info_area,
            &entrycount,error);
        if (res == DW_DLV_NO_ENTRY) {
            printf("  ERROR: Block %" DW_PR_DUu
                " does not exist though block count"
                " is %" DW_PR_DUu
                ", something is wrong\n",
                i,block_count);
            glflags.gf_count_major_errors++;
            return res;
        }
        if (res == DW_DLV_ERROR) {
            return res;
        }
        printf("  Blocknumber                         : "
            "%" DW_PR_DUu "\n",i);
        printf("  Block length                        : "
            "%" DW_PR_DUu "\n",block_length);
        printf("  Version                             : "
            "%u\n",version);
        printf("  Offset into .debug_info section     : "
            "0x%" DW_PR_XZEROS DW_PR_DUx "\n",offset_into_debug_info);
        printf("  Size of area in .debug_info section : "
            "%" DW_PR_DUu "\n",size_of_debug_info_area);
        printf("  Number of entries in block          : "
            "%" DW_PR_DUu "\n",entrycount);
     
        /*  The CU offsets appear to be those in
            the executable here. Not in
            any dwo object. The offsets within
            the entries in a block are a different
            story and some of that seems odd,
            the content names many things  in libraries,
            not just the executable or its dwo. ?
            */
        res = dwarf_get_cu_die_offset_given_cu_header_offset_b(
            dbg,offset_into_debug_info,/*is_info = */ TRUE,
            &offset_into_debug_info,error);
        if (res != DW_DLV_OK) {
            printf("  ERROR: Block %" DW_PR_DUu
                " has an invalid .debug_info offset of "
                "0x%" DW_PR_DUx
                ", something is wrong\n",
                i,offset_into_debug_info);
            if (res == DW_DLV_ERROR) {
                dwarf_dealloc_error(dbg,*error);
                *error = 0;
            }
            glflags.gf_count_major_errors++;
        } else {
            Dwarf_Die die = 0;
            Dwarf_Bool is_info = TRUE;
            res = dwarf_offdie_b(dbg,offset_into_debug_info,
                is_info, &die,error);
            if (res != DW_DLV_OK) {
                printf("  ERROR: Block %" DW_PR_DUu
                    " cu DIE offset 0x%" DW_PR_DUx
                    " is not a valid DIE offset in .debug_info\n",
                    i, offset_into_debug_info);
                if (res == DW_DLV_ERROR) {
                    dwarf_dealloc_error(dbg,*error);
                    *error = 0;
                }
                glflags.gf_count_major_errors++;
            } else {
                /* Always returns DW_DLV_OK */
                print_die_basics(dbg, die,
                    offset_into_debug_info, error);
                printf("\n");
                dwarf_dealloc_die(die);
            }
        }
        res = print_block_entries(dbg,for_pubnames,
            secname,head,i,entrycount,error);
        if (res == DW_DLV_ERROR) {
            return res;
        }
    }
    return DW_DLV_OK;
}

/*  November 25,2020: gdb 10.2 binutils source 
    can print these sections
    but gdb does not, AFAICT, use this at all.
    (binutils can print it, as can we).
    The Block offset is part of the skeleton, and refers
    to the skeleton CU DIEs (when
    that is involved) but the individual item offsets
    are referring to I-do-not-know-what.
    Block zero refers to the single CU_DIE in
    the .dwo file. The others....?
    Nothing suggests how things actually connect up. */
int
print_debug_gnu(Dwarf_Debug dbg,
    Dwarf_Error *error)
{
    int res = 0;
    Dwarf_Gnu_Index_Head head = 0;
    Dwarf_Bool for_pubnames = TRUE;
    Dwarf_Unsigned block_count = 0;
    const char *stdname = 0;
    char buf[DWARF_SECNAME_BUFFER_SIZE];
    struct esb_s truename;
    unsigned int i = 0;

    for(i = 0; i < 2; i++) {
        esb_constructor_fixed(&truename,buf,
            DWARF_SECNAME_BUFFER_SIZE);
        if (!i) {
            glflags.current_section_id = DEBUG_GNU_PUBNAMES;
            for_pubnames = TRUE;
            stdname =  ".debug_gnu_pubnames";
        } else {
            for_pubnames = FALSE;
            glflags.current_section_id = DEBUG_GNU_PUBTYPES;
            stdname = ".debug_gnu_pubtypes";
        }
        get_true_section_name(dbg,stdname, &truename,TRUE);
        res = dwarf_get_gnu_index_head(dbg,for_pubnames,
            &head, &block_count,error);
        if (res == DW_DLV_ERROR) {
            glflags.gf_count_major_errors++;
            printf("ERROR: problem reading %s. %s\n",
                sanitized(esb_get_string(&truename)),
                dwarf_errmsg(*error));
            dwarf_dealloc_error(dbg,*error);
            *error = 0;
            continue;
        } else if (res == DW_DLV_NO_ENTRY) {
            continue;
        }
        printf("\n%s with %" DW_PR_DUu
            " blocks of names\n",
            sanitized(esb_get_string(&truename)),
            block_count);
        res = print_all_blocks(dbg,for_pubnames,
            &truename, head,block_count,error);
        if (res == DW_DLV_ERROR) {
            glflags.gf_count_major_errors++;
            printf("ERROR: problem reading %s. %s\n",
                sanitized(esb_get_string(&truename)),
                dwarf_errmsg(*error));
            dwarf_dealloc_error(dbg,*error);
            *error = 0;
        } else if (res == DW_DLV_NO_ENTRY) {
            /* impossible */
        } else {
            /* normal */
        }
        dwarf_gnu_index_dealloc(head);
        esb_destructor(&truename);
    }
    return DW_DLV_OK;
}