Debian Codegraph @

src/bin/dwarfdump/print_macinfo.c

raw
1 /*
2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2009-2018 SN Systems Ltd. All rights reserved.
5 Portions Copyright 2008-2020 David Anderson. All rights reserved.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of version 2 of the GNU General
9 Public License as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write the Free
26 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
27 Boston MA 02110-1301, USA.
28
29 */
30
31 #include <config.h>
32
33 #include <string.h> /* memset() */
34 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
35
36 #include "dwarf.h"
37 #include "libdwarf.h"
38 #include "libdwarf_private.h"
39 #include "dd_defined_types.h"
40 #include "dd_checkutil.h"
41 #include "dd_glflags.h"
42 #include "dd_globals.h"
43 #include "dd_naming.h"
44 #include "dd_macrocheck.h"
45 #include "dd_esb.h"
46 #include "dd_esb_using_functions.h"
47 #include "dd_sanitized.h"
48
49 #include "print_sections.h"
50 #include "print_frames.h"
51
52 struct macro_counts_s {
53 long mc_start_file;
54 long mc_end_file;
55 long mc_define;
56 long mc_undef;
57 long mc_extension;
58 long mc_code_zero;
59 long mc_unknown;
60 };
61
62 static int
63 print_one_macro_entry_detail(long i,
64 char *type,
65 struct Dwarf_Macro_Details_s *mdp)
66 {
67 /* "DW_MACINFO_*: section-offset file-index [line] string\n" */
68 if (glflags.gf_do_print_dwarf) {
69 if (mdp->dmd_macro) {
70 printf("%3ld %s: %6" DW_PR_DUu " %4" DW_PR_DSd " [%4"
71 DW_PR_DSd "] \"%s\" \n",
72 i,
73 type,
74 (Dwarf_Unsigned)mdp->dmd_offset,
75 mdp->dmd_fileindex, mdp->dmd_lineno,
76 sanitized(mdp->dmd_macro));
77 print_split_macro_value(mdp->dmd_macro);
78 } else {
79 printf("%3ld %s: %6" DW_PR_DUu " %4" DW_PR_DSd " [%4"
80 DW_PR_DSd "] 0\n",
81 i,
82 type,
83 (Dwarf_Unsigned)mdp->dmd_offset,
84 mdp->dmd_fileindex, mdp->dmd_lineno);
85 }
86 }
87 return DW_DLV_OK;
88 }
89
90 /* Nothing in here can actually fail.
91 Returns DW_DLV_OK */
92 static int
93 print_one_macro_entry(long i,
94 struct Dwarf_Macro_Details_s *mdp,
95 struct macro_counts_s *counts,
96 char ** srcfiles,
97 Dwarf_Signed srcf_count)
98 {
99 int res = 0;
100
101 switch (mdp->dmd_type) {
102 case 0:
103 counts->mc_code_zero++;
104 res = print_one_macro_entry_detail(i,
105 "DW_MACINFO_type-code-0", mdp);
106 break;
107
108 case DW_MACINFO_start_file: {
109 counts->mc_start_file++;
110 if (mdp->dmd_fileindex == 0) {
111 mdp->dmd_macro = "<zero index, no file specified>";
112 } else if (srcf_count > 0 &&
113 mdp->dmd_fileindex <= srcf_count) {
114 mdp->dmd_macro = srcfiles[mdp->dmd_fileindex-1];
115 } else {
116 if (srcf_count == 0) {
117 mdp->dmd_macro =
118 "<invalid index, no line table file names exist>";
119 } else {
120 mdp->dmd_macro = "<invalid index, corrupt data?>";
121 }
122 }
123 res = print_one_macro_entry_detail(i,
124 "DW_MACINFO_start_file", mdp);
125 break;
126 }
127
128 case DW_MACINFO_end_file:
129 counts->mc_end_file++;
130 res = print_one_macro_entry_detail(i,
131 "DW_MACINFO_end_file ", mdp);
132 break;
133
134 case DW_MACINFO_vendor_ext:
135 counts->mc_extension++;
136 res = print_one_macro_entry_detail(i,
137 "DW_MACINFO_vendor_ext", mdp);
138 break;
139
140 case DW_MACINFO_define:
141 counts->mc_define++;
142 res = print_one_macro_entry_detail(i,
143 "DW_MACINFO_define ", mdp);
144 break;
145
146 case DW_MACINFO_undef:
147 counts->mc_undef++;
148 res = print_one_macro_entry_detail(i,
149 "DW_MACINFO_undef ", mdp);
150 break;
151
152 default:
153 {
154 struct esb_s typeb;
155
156 esb_constructor(&typeb);
157 counts->mc_unknown++;
158 esb_append_printf_u(&typeb,
159 "DW_MACINFO_0x%x, of unknown type", mdp->dmd_type);
160 print_one_macro_entry_detail(i,
161 esb_get_string(&typeb), mdp);
162 esb_destructor(&typeb);
163 }
164 res = DW_DLV_OK;
165 break;
166 }
167 return res;
168 }
169
170 static void
171 mac_dealloc_srcfiles_data(Dwarf_Debug dbg,
172 char **srcfiles,
173 Dwarf_Signed srcf_count)
174 {
175 Dwarf_Signed i = 0;
176 if (!srcfiles) {
177 return;
178 }
179 for ( ; i < srcf_count; ++i) {
180 dwarf_dealloc(dbg,srcfiles[i],DW_DLA_STRING);
181 }
182 dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST);
183 }
184
185 /* print data in .debug_macinfo */
186 /*ARGSUSED*/ int
187 print_macinfo_by_offset(Dwarf_Debug dbg,
188 Dwarf_Die cu_die,
189 Dwarf_Unsigned offset,
190 Dwarf_Error *error)
191 {
192 Dwarf_Unsigned max = 0;
193 Dwarf_Signed count = 0;
194 Dwarf_Macro_Details *maclist = NULL;
195 int lres = 0;
196 long i = 0;
197 struct macro_counts_s counts;
198 Dwarf_Unsigned totallen = 0;
199 Dwarf_Bool is_primary = TRUE;
200 char ** srcfiles = 0;
201 Dwarf_Signed srcf_count = 0;
202
203 glflags.current_section_id = DEBUG_MACINFO;
204
205 /* No real need to get the real section name, this
206 section not used much in modern compilers
207 as this definition of macro data (V2-V4)
208 is obsolete -- it takes too much space to be
209 much used. */
210
211 lres = dwarf_get_macro_details(dbg, offset,
212 max, &count, &maclist, error);
213 if (lres == DW_DLV_ERROR) {
214 struct esb_s m;
215
216 esb_constructor(&m);
217 esb_append_printf_u(&m,
218 "\nERROR: dwarf_get_macro_details() fails on"
219 " offset 0x%x from print_macinfo_by_offset().",offset);
220 simple_err_only_return_action(lres,
221 esb_get_string(&m));
222 esb_destructor(&m);
223 return lres;
224 } else if (lres == DW_DLV_NO_ENTRY) {
225 return lres;
226 }
227 lres = dwarf_srcfiles(cu_die,&srcfiles,&srcf_count,error);
228 if (lres == DW_DLV_ERROR) {
229 /* This error will get found other places. No need
230 to say anything here. */
231 dwarf_dealloc_error(dbg,*error);
232 *error = 0;
233 }
234
235 memset(&counts, 0, sizeof(counts));
236 if (glflags.gf_do_print_dwarf) {
237 struct esb_s truename;
238 char buf[DWARF_SECNAME_BUFFER_SIZE];
239
240 esb_constructor_fixed(&truename,buf,sizeof(buf));
241 get_true_section_name(dbg,".debug_macinfo",
242 &truename,TRUE);
243 printf("\n%s\n",sanitized(esb_get_string(&truename)));
244 esb_destructor(&truename);
245 printf("\n");
246 printf("compilation-unit .debug_macinfo offset "
247 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",offset);
248 printf(" sec file\n");
249 printf("num name offset index [line] "
250 "\"string\"\n");
251 }
252 for (i = 0; i < count; i++) {
253 struct Dwarf_Macro_Details_s *mdp = &maclist[i];
254 print_one_macro_entry(i, mdp, &counts,
255 srcfiles,srcf_count);
256 }
257
258 if (counts.mc_start_file == 0) {
259 printf("ERROR: DW_MACINFO file count of zero is "
260 "invalid DWARF2/3/4\n");
261 glflags.gf_count_major_errors++;
262 }
263 if (counts.mc_start_file != counts.mc_end_file) {
264 glflags.gf_count_major_errors++;
265 printf("ERROR: Counts of DW_MACINFO start_file (%ld)"
266 " end_file (%ld) "
267 "do not match!. Incorrect DWARF2,3,4.\n",
268 counts.mc_start_file, counts.mc_end_file);
269 }
270 if (counts.mc_code_zero < 1) {
271 glflags.gf_count_major_errors++;
272 printf("ERROR: Count of zeros in macro group "
273 "should be non-zero "
274 "(1 preferred), count is %ld\n",
275 counts.mc_code_zero);
276 }
277 /* next byte is maclist[count - 1].dmd_offset + 1; */
278 totallen = (maclist[count - 1].dmd_offset + 1) - offset;
279 add_macro_import(&macinfo_check_tree,is_primary, offset,0,0);
280 add_macro_area_len(&macinfo_check_tree,offset,totallen);
281 if (glflags.gf_do_print_dwarf) {
282 printf("Macro counts: start file %ld, "
283 "end file %ld, "
284 "define %ld, "
285 "undef %ld, "
286 "ext %ld, "
287 "code-zero %ld, "
288 "unknown %ld\n",
289 counts.mc_start_file,
290 counts.mc_end_file,
291 counts.mc_define,
292 counts.mc_undef,
293 counts.mc_extension,
294 counts.mc_code_zero, counts.mc_unknown);
295 }
296
297 /* int type= maclist[count - 1].dmd_type; */
298 /* ASSERT: type is zero */
299 mac_dealloc_srcfiles_data(dbg,srcfiles,srcf_count);
300 dwarf_dealloc(dbg, maclist, DW_DLA_STRING);
301 return DW_DLV_OK;
302 }