File: print_debugfission.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 (327 lines) | stat: -rw-r--r-- 11,242 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
/*
  Copyright 2014-2020 David Anderson. All rights reserved.

  This program is free software; you can redistribute it and/or
  modify it under the terms of version 2 of the GNU General
  Public License as published by the Free Software Foundation.

  This program is distributed in the hope that it would be
  useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  Further, this software is distributed without any warranty
  that it is free of the rightful claim of any third person
  regarding infringement or the like.  Any license provided
  herein, whether implied or otherwise, applies only to this
  software file.  Patent licenses, if any, provided herein
  do not apply to combinations of this program with other
  software, or any other product whatsoever.

  You should have received a copy of the GNU General Public
  License along with this program; if not, write the Free
  Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  Boston MA 02110-1301, USA.

*/

#include "globals.h"
#include "naming.h"
#include "esb.h"
#include "esb_using_functions.h"
#include "sanitized.h"
#include "print_sections.h"

#define TRUE 1
#define FALSE 0

static const char *
dw_dlv_string(int res)
{
    if (res == DW_DLV_ERROR) {
        return "DW_DLV_ERROR";
    }
    if (res == DW_DLV_NO_ENTRY) {
        return "DW_DLV_NO_ENTRY";
    }
    if (res == DW_DLV_OK) {
        return "DW_DLV_OK";
    }
    return "ERROR: Impossible libdwarf DW_DLV code";
}

static int
hashval_zero(Dwarf_Sig8 *val)
{
    unsigned u = 0;

    for(u=0 ; u < sizeof(Dwarf_Sig8);++u) {
        if (val->signature[u]) {
            return FALSE;
        }
    }
    return TRUE;
}

int
print_debugfission_index(Dwarf_Debug dbg,const char *type,
    Dwarf_Error *err)
{
    int res = 0;
    Dwarf_Xu_Index_Header xuhdr = 0;
    Dwarf_Unsigned version_number = 0;
    Dwarf_Unsigned offsets_count = 0;
    Dwarf_Unsigned units_count = 0;
    Dwarf_Unsigned hash_slots_count = 0;
    const char * section_name = 0;
    const char * section_type2 = 0;
    const char * section_name2 = 0;
    int is_cu = !strcmp(type,"cu")?TRUE:FALSE;

    res = dwarf_get_xu_index_header(dbg,
        type,
        &xuhdr,
        &version_number,
        &offsets_count,
        &units_count,
        &hash_slots_count,
        &section_name,
        err);
    if (res == DW_DLV_NO_ENTRY) {
        /* This applies to most object files. */
        return res;
    }
    if (res == DW_DLV_ERROR) {
        simple_err_return_msg_either_action(res,
            "ERROR: Call to dwarf_get_xu_index_header() failed.");
        return res;
    }
    res = dwarf_get_xu_index_section_type(xuhdr,
        &section_type2,
        &section_name2,
        err);
    if (res == DW_DLV_NO_ENTRY) {
        struct esb_s tmsg;

        esb_constructor(&tmsg);
        esb_append(&tmsg,
            "ERROR: dwarf_get_xu_index_section_type() "
            " returned DW_DLV_NO_ENTRY "
            " which should be impossible.");
        esb_append(&tmsg," Something is corrupted.");
        simple_err_return_action(DW_DLV_ERROR,
            esb_get_string(&tmsg));
        dwarf_xu_header_free(xuhdr);
        esb_destructor(&tmsg);
        /* We have no way to return a DW_DLV_ERROR.
            as we cannot manufacture a Dwarf_Error */
        return res;
    }
    if (res == DW_DLV_ERROR) {
        simple_err_return_msg_either_action(res,
            "ERROR: Call to dwarf_get_xu_index_section_type() "
            "failed.");
        dwarf_xu_header_free(xuhdr);
        return res;
    }
    if (strcmp(section_type2,type)) {
        struct esb_s tmsg;

        esb_constructor(&tmsg);
        esb_append_printf_s(&tmsg,
            "ERROR: dwarf_get_xu_index_section_type() "
            " returned section type %s ",
            sanitized(section_type2));
        esb_append_printf_s(&tmsg,
            "whereas the call was for section type %s. ",
            sanitized(type));
        esb_append(&tmsg," Something is corrupted.");
        simple_err_return_action(DW_DLV_ERROR,
            esb_get_string(&tmsg));
        esb_destructor(&tmsg);
        dwarf_xu_header_free(xuhdr);
        /* We have no way to return a DW_DLV_ERROR.
            as we cannot manufacture a Dwarf_Error */
        return DW_DLV_OK;
    }
    if(!section_name || !*section_name) {
        section_name = (is_cu?".debug_cu_index":".debug_tu_index");
    }
    {
        struct esb_s truename;
        char buf[DWARF_SECNAME_BUFFER_SIZE];

        esb_constructor_fixed(&truename,buf,sizeof(buf));
        get_true_section_name(dbg,section_name,
            &truename,TRUE);
        printf("\n%s\n",sanitized(esb_get_string(&truename)));
        esb_destructor(&truename);
    }
    printf("  Version:           : %" DW_PR_DUu "\n",
        version_number);
    printf("  Number of columns N: %" DW_PR_DUu "\n",
        offsets_count);
    printf("  number of entries U: %" DW_PR_DUu  "\n",
        units_count);
    printf("  Number of slots   S: %" DW_PR_DUu "\n",
        hash_slots_count);
    {
        unsigned n = 0;
        Dwarf_Unsigned sect_num;
        const char *name = 0;

        printf("\n");
        printf("Columns index to section id and name \n");
        printf("  [ ] id       name\n");
        for ( ; n < offsets_count; ++n) {
            res = dwarf_get_xu_section_names(xuhdr,
                n,&sect_num,&name,err);
            if (res == DW_DLV_ERROR) {
                return res;
            } if (res == DW_DLV_NO_ENTRY) {
                printf("  [%u] unused\n",n);
            } else {
                printf("  [%u] %" DW_PR_DUu "        %s\n",n,
                    sect_num,name);
            }
        }
    }
    if (hash_slots_count > 0) {
        printf("\n");
        printf("   slot      hash                index\n");
    }
    {
        /*  For h < S */
        Dwarf_Unsigned h = 0;
        for( h = 0; h < hash_slots_count; h++) {
            Dwarf_Sig8 hashval;
            Dwarf_Unsigned index = 0;
            Dwarf_Unsigned col = 0;

            memset(&hashval,0,sizeof(hashval));
            res = dwarf_get_xu_hash_entry(xuhdr,h,
                &hashval,&index,err);
            if (res == DW_DLV_ERROR) {
                struct esb_s hmsg;

                esb_constructor(&hmsg);
                esb_append_printf_u(&hmsg,
                    "ERROR: dwarf_get_xu_hash_entry failed "
                    " on slot number %u ",h);
                esb_append_printf_u(&hmsg," of %u slots.",
                    hash_slots_count);
                simple_err_return_action(res,
                    esb_get_string(&hmsg));
                dwarf_xu_header_free(xuhdr);
                esb_destructor(&hmsg);
                return res;
            } else if (res == DW_DLV_NO_ENTRY) {
                /* Impossible */
                struct esb_s hmsg;

                esb_constructor(&hmsg);
                esb_append_printf_u(&hmsg,
                    "ERROR: dwarf_get_xu_hash_entry got NO_ENTRY "
                    " on slot number %u ",h);
                esb_append_printf_u(&hmsg," of %u slots."
                    " That should be impossible.",
                    hash_slots_count);
                dwarf_xu_header_free(xuhdr);
                esb_destructor(&hmsg);
                return res;
            } else if (!index) {
                if (hashval_zero(&hashval)) {
                    /* An unused hash slot, we do not print them */
                } else {
                    struct esb_s hashhexstring;

                    esb_constructor(&hashhexstring);
                    format_sig8_string(&hashval,&hashhexstring);
                    printf("  [%4" DW_PR_DUu "] %s"
                        " %8" DW_PR_DUu  " %s\n",
                        h,
                        esb_get_string(&hashhexstring),
                        index,
                        "Index 0 means the hash gets ignored");
                    esb_destructor(&hashhexstring);
                }
                continue;
            }
            {
                struct esb_s hashhexstring;

                esb_constructor(&hashhexstring);
                format_sig8_string(&hashval,&hashhexstring);
                printf("  [%4" DW_PR_DUu "] %s"
                    " %8" DW_PR_DUu  "\n",
                    h,
                    esb_get_string(&hashhexstring),
                    index);
                esb_destructor(&hashhexstring);
            }
            printf("    [r,c]              section   "
                "  offset             size\n");
            for (col = 0; col < offsets_count; col++) {
                Dwarf_Unsigned off = 0;
                Dwarf_Unsigned len = 0;
                const char * name = 0;
                Dwarf_Unsigned num = 0;
                res = dwarf_get_xu_section_names(xuhdr,
                    col,&num,&name,err);
                if (res != DW_DLV_OK) {
                    struct esb_s hmsg;
                    const char * et= dw_dlv_string(res);

                    esb_constructor(&hmsg);
                    esb_append_printf_s(&hmsg,
                        "ERROR: dwarf_get_xu_section_names "
                        "got %s ",et);
                    esb_append_printf_u(&hmsg,
                        " on column number %u ",col);
                    esb_append_printf_u(&hmsg," of %u columns.",
                        offsets_count);
                    simple_err_return_action(res,
                        esb_get_string(&hmsg));
                    esb_destructor(&hmsg);
                    dwarf_xu_header_free(xuhdr);
                    return res;
                }
                /*  index is 1-origin. We use it
                    that way.  */
                res = dwarf_get_xu_section_offset(xuhdr,
                    index,col,&off,&len,err);
                if (res != DW_DLV_OK) {
                    struct esb_s hmsg;
                    const char * et= dw_dlv_string(res);

                    esb_constructor(&hmsg);
                    esb_append_printf_s(&hmsg,
                        "ERROR: dwarf_get_xu_section_offset "
                        "got %s ",et);
                    esb_append_printf_u(&hmsg,
                        " on index number %u ",index);
                    esb_append_printf_u(&hmsg,
                        " on column number %u ",col);
                    esb_append_printf_u(&hmsg," of %u columns.",
                        offsets_count);
                    simple_err_return_action(res,
                        esb_get_string(&hmsg));
                    esb_destructor(&hmsg);
                    dwarf_xu_header_free(xuhdr);
                    return res;
                }
                printf("    [%1" DW_PR_DUu ",%1" DW_PR_DUu "] %20s "
                    "0x%" DW_PR_XZEROS DW_PR_DUx
                    " (%8" DW_PR_DUu ") "
                    "0x%" DW_PR_XZEROS DW_PR_DUx
                    " (%8" DW_PR_DUu ")\n",
                    index,
                    col,name,
                    off,off,
                    len,len);
            }
        }
    }
    dwarf_xu_header_free(xuhdr);
    return DW_DLV_OK;
}