Debian Codegraph @

src/bin/dwarfdump/dd_compiler_info.c

raw
1 /*
2 Copyright 2010-2020 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25 */
26
27 #include <config.h>
28
29 #include <stddef.h> /* NULL */
30 #include <stdio.h> /* stdout fprintf() printf() */
31 #include <stdlib.h> /* exit() free() malloc() qsort() */
32 #include <string.h> /* memset() strcmp() stricmp()
33 strlen() strncmp() */
34
35 /* Windows specific header files */
36 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
37 #include "stdafx.h"
38 #endif /* HAVE_STDAFX_H */
39
40 #include "dwarf.h"
41 #include "libdwarf.h"
42 #include "dd_defined_types.h"
43 #include "dd_checkutil.h"
44 #include "dd_glflags.h"
45 #include "dd_globals.h"
46 #include "dd_makename.h"
47 #include "dd_sanitized.h"
48 #include "dd_safe_strcpy.h"
49 #include "dd_command_options.h"
50 #include "dd_compiler_info.h"
51 #include "libdwarf_private.h" /* For malloc/calloc debug */
52
53 /* Record compilers whose CU names have been seen.
54 Full CU names recorded here, though only a portion
55 of the name may have been checked to cause the
56 compiler data to be entered here.
57
58 The detected/targeted arrays leave index 0 unused.
59 There is no particularly good reason for this.
60 current_compiler starts at -1, and is legitimately
61 1 through COMPILER_TABLE_MAX-1.
62
63 compilers_targeted_count and compilers_detected_count
64 are the actual count, so loops use things like
65 for (i = 1 ; i <= compilers_detected_count; ++1)
66 for example.
67 */
68 static Compiler compilers_detected[COMPILER_TABLE_MAX];
69 static int compilers_detected_count = 0;
70
71 /* compilers_targeted is a list of indications of compilers
72 on which we wish error checking (and the counts
73 of checks made and errors found). We do substring
74 comparisons, so the compilers_targeted name might be simply a
75 compiler version number or a short substring of a
76 CU producer name.
77 The +1 guarantees we do not overstep the array.
78 */
79 static Compiler compilers_targeted[COMPILER_TABLE_MAX];
80 static int compilers_targeted_count = 0;
81 static int current_compiler = -1;
82
83 /* Indicates if the current CU is a target */
84 static Dwarf_Bool current_cu_is_checked_compiler = TRUE;
85
86 static int
87 has_cu_producer_prefix(const char *prefix)
88 {
89 if (strncmp(glflags.CU_producer,prefix,strlen(prefix)) == 0) {
90 return TRUE;
91 }
92 return FALSE;
93 }
94
95 static void
96 PRINT_CHECK_RESULT(char *str,
97 Compiler *pCompiler, Dwarf_Check_Categories category)
98 {
99 Dwarf_Check_Result result = pCompiler->results[category];
100 printf("%-24s%10d %10d\n", str, result.checks, result.errors);
101 }
102
103 /* Print checks and errors for a specific compiler */
104 static void
105 print_specific_checks_results(Compiler *pCompiler)
106 {
107 printf("\nDWARF CHECK RESULT\n");
108 printf("<item> <checks> <errors>\n");
109 if (glflags.gf_check_pubname_attr) {
110 PRINT_CHECK_RESULT("pubname_attr", pCompiler,
111 pubname_attr_result);
112 }
113 if (glflags.gf_check_tag_attr) {
114 PRINT_CHECK_RESULT("attr_tag", pCompiler, attr_tag_result);
115 PRINT_CHECK_RESULT("attr_formclass", pCompiler,
116 attr_formclass_result);
117 }
118 if (glflags.gf_check_tag_tree) {
119 PRINT_CHECK_RESULT("tag_tree", pCompiler, tag_tree_result);
120 }
121 if (glflags.gf_check_type_offset) {
122 PRINT_CHECK_RESULT("type_offset", pCompiler,
123 type_offset_result);
124 }
125 if (glflags.gf_check_decl_file) {
126 PRINT_CHECK_RESULT("decl_file", pCompiler, decl_file_result);
127 }
128 if (glflags.gf_check_ranges) {
129 PRINT_CHECK_RESULT("ranges", pCompiler, ranges_result);
130 }
131 if (glflags.gf_check_lines) {
132 PRINT_CHECK_RESULT("line_table", pCompiler, lines_result);
133 }
134 if (glflags.gf_check_fdes) {
135 PRINT_CHECK_RESULT("fde_table", pCompiler, fde_duplication);
136 }
137 if (glflags.gf_check_aranges) {
138 PRINT_CHECK_RESULT("aranges", pCompiler, aranges_result);
139 }
140
141 if (glflags.gf_check_names) {
142 PRINT_CHECK_RESULT("names",pCompiler, names_result);
143 }
144 if (glflags.gf_check_frames) {
145 PRINT_CHECK_RESULT("frames",pCompiler, frames_result);
146 }
147 if (glflags.gf_check_locations) {
148 PRINT_CHECK_RESULT("locations",pCompiler,
149 locations_result);
150 }
151
152 if (glflags.gf_check_harmless) {
153 PRINT_CHECK_RESULT("harmless_errors", pCompiler,
154 harmless_result);
155 }
156
157 if (glflags.gf_check_abbreviations) {
158 PRINT_CHECK_RESULT("abbreviations", pCompiler,
159 abbreviations_result);
160 }
161
162 if (glflags.gf_check_dwarf_constants) {
163 PRINT_CHECK_RESULT("dwarf_constants",
164 pCompiler, dwarf_constants_result);
165 }
166
167 if (glflags.gf_check_di_gaps) {
168 PRINT_CHECK_RESULT("debug_info_gaps", pCompiler,
169 di_gaps_result);
170 }
171
172 if (glflags.gf_check_forward_decl) {
173 PRINT_CHECK_RESULT("forward_declarations",
174 pCompiler, forward_decl_result);
175 }
176
177 if (glflags.gf_check_self_references) {
178 PRINT_CHECK_RESULT("self_references",
179 pCompiler, self_references_result);
180 }
181
182 /* Display attributes encoding results */
183 if (glflags.gf_check_attr_encoding) {
184 PRINT_CHECK_RESULT("attr_encoding", pCompiler,
185 attr_encoding_result);
186 }
187
188 /* Duplicated attributes */
189 if (glflags.gf_check_duplicated_attributes) {
190 PRINT_CHECK_RESULT("duplicated_attributes",
191 pCompiler, duplicated_attributes_result);
192 }
193 if (glflags.gf_check_functions) {
194 PRINT_CHECK_RESULT("function_coverage",
195 pCompiler, check_functions_result);
196 }
197
198 PRINT_CHECK_RESULT("** Summarize **",pCompiler,
199 total_check_result);
200 fflush(stdout);
201 }
202
203 /* Add a CU name to the current compiler entry, specified by the
204 'current_compiler'; the name is added to the 'compilers_detected'
205 table and is printed if the '-P' or '--print-producers'
206 option is specified in the
207 command line. */
208 void
209 add_cu_name_compiler_target(char *name)
210 {
211 a_name_chain *cu_last = 0;
212 a_name_chain *nc = 0;
213 Compiler *pCompiler = 0;
214
215 if (current_compiler < 1) {
216 printf("ERROR Current compiler set to %d, cannot add "
217 "Compilation unit name. Giving up.",current_compiler);
218 exit(EXIT_FAILURE);
219 }
220 pCompiler = &compilers_detected[current_compiler];
221 cu_last = pCompiler->cu_last;
222 /* Record current cu name */
223 nc = (a_name_chain *)malloc(sizeof(a_name_chain));
224 if (!nc) {
225 printf("ERROR Out of memory "
226 "allocating compiler target %s "
227 "(not saved)\n",name);
228 glflags.gf_count_major_errors++;
229 return;
230 }
231 nc->item = makename(name);
232 nc->next = NULL;
233 if (cu_last) {
234 cu_last->next = nc;
235 } else {
236 pCompiler->cu_list = nc;
237 }
238 pCompiler->cu_last = nc;
239 }
240
241 /* Reset a compiler entry, so all fields are properly set */
242 void
243 reset_compiler_entry(Compiler *compiler)
244 {
245 memset(compiler,0,sizeof(Compiler));
246 }
247
248 /* Record which compiler was used (or notice we saw
249 it before) and set a couple variables as
250 a side effect (which are used all over
251 in this one source file):
252
253 current_cu_is_checked_compiler
254 (used in checking_this_compiler() )
255
256 current_compiler
257
258 The compiler/producer name is from DW_AT_producer.
259 */
260 void
261 update_compiler_target(const char *producer_name)
262 {
263 Dwarf_Bool cFound = FALSE;
264 int index = 0;
265
266 dd_safe_strcpy(glflags.CU_producer,sizeof(glflags.CU_producer),
267 producer_name,
268 strlen(producer_name));
269 current_cu_is_checked_compiler = FALSE;
270
271 /* This list of compilers is just a start:
272 GCC id : "GNU"
273 SNC id : "SN Systems" */
274
275 /* Find a compiler version to check */
276 if (compilers_targeted_count) {
277 for (index = 1; index <= compilers_targeted_count; ++index) {
278 if (is_strstrnocase(glflags.CU_producer,
279 compilers_targeted[index].name)) {
280 compilers_targeted[index].verified = TRUE;
281 current_cu_is_checked_compiler = TRUE;
282 break;
283 }
284 }
285 } else {
286 /* Internally the strings do not include quotes */
287 Dwarf_Bool snc_compiler = has_cu_producer_prefix("SN");
288 Dwarf_Bool gcc_compiler = has_cu_producer_prefix("GNU");
289 current_cu_is_checked_compiler =
290 glflags.gf_check_all_compilers ||
291 (snc_compiler && glflags.gf_check_snc_compiler) ||
292 (gcc_compiler && glflags.gf_check_gcc_compiler) ;
293 }
294
295 /* Check for already detected compiler */
296 for (index = 1; index <= compilers_detected_count; ++index) {
297 const char *name = compilers_detected[index].name;
298 if (
299 #if _WIN32
300 !stricmp(name,glflags.CU_producer)
301 #else
302 !strcmp(name, glflags.CU_producer)
303 #endif /* _WIN32 */
304 ) {
305 /* Set current compiler index */
306 current_compiler = index;
307 cFound = TRUE;
308 break;
309 } else {
310 }
311 }
312 if (!cFound) {
313 /* Record a new detected compiler name. */
314 if ((compilers_detected_count + 1) < COMPILER_TABLE_MAX) {
315 Compiler *pCompiler = 0;
316 char *cmp = makename(glflags.CU_producer);
317 /* Set current compiler index,
318 first compiler at position [1] */
319 current_compiler = ++compilers_detected_count;
320 pCompiler = &compilers_detected[current_compiler];
321 reset_compiler_entry(pCompiler);
322 pCompiler->name = cmp;
323 }
324 }
325 }
326
327 void
328 clean_up_compilers_detected(void)
329 {
330 memset(&compilers_detected[0],0,
331 COMPILER_TABLE_MAX*sizeof(Compiler));
332 compilers_detected_count = 0;
333 }
334
335 /* Are we checking for errors from the
336 compiler of the current compilation unit?
337 */
338 Dwarf_Bool
339 checking_this_compiler(void)
340 {
341 /* This flag has been update by 'update_compiler_target()'
342 and indicates if the current CU is in a targeted compiler
343 specified by the user. Default value is TRUE, which
344 means test all compilers until a CU is detected. */
345 return current_cu_is_checked_compiler;
346 }
347
348 static int
349 qsort_compare_compiler(const void *elem1,const void *elem2)
350 {
351 Compiler cmp1 = *(Compiler *)elem1;
352 Compiler cmp2 = *(Compiler *)elem2;
353 int cnt1 = cmp1.results[total_check_result].errors;
354 int cnt2 = cmp2.results[total_check_result].errors;
355
356 if (cnt1 < cnt2) {
357 return 1;
358 }
359 if (cnt1 > cnt2) {
360 return -1;
361 }
362 /* When error counts match, sort on name. */
363 {
364 int v = strcmp(cmp2.name, cmp1.name);
365 return v;
366 }
367 }
368
369 /* Print a summary of checks and errors */
370 void
371 print_checks_results(void)
372 {
373 int index = 0;
374 Compiler *pCompilers;
375 Compiler *pCompiler;
376
377 /* Sort based on errors detected; the first entry is reserved
378 and unused count started at 1 */
379 pCompilers = &compilers_detected[1];
380 if (compilers_detected_count > 1) {
381 qsort((void *)pCompilers,compilers_detected_count,
382 sizeof(Compiler),qsort_compare_compiler);
383 }
384
385 /* Print list of CUs for each compiler detected */
386 if (glflags.gf_producer_children_flag) {
387
388 a_name_chain *nc = 0;
389 a_name_chain *nc_next = 0;
390 int count = 0;
391 int total = 0;
392
393 printf("\n*** CU NAMES PER COMPILER ***\n");
394 for (index = 1; index <= compilers_detected_count; ++index) {
395 pCompiler = &compilers_detected[index];
396 printf("\n%02d: %s",index,sanitized(pCompiler->name));
397 count = 0;
398 for (nc = pCompiler->cu_list; nc; nc = nc_next) {
399 printf("\n %02d: '%s'",++count,
400 sanitized(nc->item));
401 nc_next = nc->next;
402 free(nc);
403 }
404 total += count;
405 printf("\n");
406 }
407 printf("\nDetected %d CU names\n",total);
408 }
409
410 /* Print error report only if errors have been detected */
411 /* Print error report if the -kd option */
412 if ((glflags.gf_do_check_dwarf && glflags.check_error) ||
413 glflags.gf_check_show_results) {
414 int count = 0;
415 int compilers_not_detected = 0;
416 int compilers_verified = 0;
417
418 /* Find out how many compilers have been verified. */
419 for (index = 1; index <= compilers_detected_count; ++index) {
420 if (compilers_detected[index].verified) {
421 ++compilers_verified;
422 }
423 }
424 /* Find out how many compilers have been not detected. */
425 for (index = 1; index <= compilers_targeted_count; ++index) {
426 if (!compilers_targeted[index].verified) {
427 ++compilers_not_detected;
428 }
429 }
430
431 /* Print compilers detected list */
432 printf(
433 "\n%d Compilers detected:\n",compilers_detected_count);
434 for (index = 1; index <= compilers_detected_count; ++index) {
435 pCompiler = &compilers_detected[index];
436 printf("%02d: %s\n",index,sanitized(pCompiler->name));
437 }
438
439 /* Print compiler list specified by the user with the
440 '-c<str>', that were not detected. */
441 if (compilers_not_detected) {
442 count = 0;
443 printf(
444 "\n%d Compilers not detected:\n",
445 compilers_not_detected);
446 for (index = 1; index <= compilers_targeted_count;
447 ++index) {
448 if (!compilers_targeted[index].verified) {
449 printf(
450 "%02d: '%s'\n",
451 ++count,
452 sanitized(compilers_targeted[index].name));
453 }
454 }
455 }
456
457 count = 0;
458 printf("\n%d Compilers verified:\n",compilers_verified);
459 for (index = 1; index <= compilers_detected_count; ++index) {
460 pCompiler = &compilers_detected[index];
461 if (pCompiler->verified) {
462 printf("%02d: errors = %5d, %s\n",
463 ++count,
464 pCompiler->results[total_check_result].errors,
465 sanitized(pCompiler->name));
466 }
467 }
468
469 /* Print summary if we have verified compilers or
470 if the -kd option used. */
471 if (compilers_verified || glflags.gf_check_show_results) {
472 /* Print compilers detected summary*/
473 if (glflags.gf_print_summary_all) {
474 count = 0;
475 printf("\n*** ERRORS PER COMPILER ***\n");
476 for (index = 1; index <= compilers_detected_count;
477 ++index) {
478 pCompiler = &compilers_detected[index];
479 if (pCompiler->verified) {
480 printf("\n%02d: %s",
481 ++count,sanitized(pCompiler->name));
482 print_specific_checks_results(pCompiler);
483 }
484 }
485 }
486
487 /* Print general summary (all compilers checked) */
488 printf("\n*** TOTAL ERRORS FOR ALL COMPILERS ***\n");
489 print_specific_checks_results(&compilers_detected[0]);
490 }
491 }
492 fflush(stdout);
493 }
494
495 void DWARF_CHECK_COUNT(Dwarf_Check_Categories category, int inc)
496 {
497 Compiler * c = 0;
498
499 c = &compilers_detected[0];
500 c->results[category].checks += inc;
501 c->results[total_check_result].checks += inc;
502 if (current_compiler > 0 &&
503 current_compiler < COMPILER_TABLE_MAX) {
504 c = &compilers_detected[current_compiler];
505 c->results[category].checks += inc;
506 c->results[total_check_result].checks += inc;
507 c->verified = TRUE;
508 }
509 }
510
511 void DWARF_ERROR_COUNT(Dwarf_Check_Categories category, int inc)
512 {
513 Compiler * c = 0;
514
515 c = &compilers_detected[0];
516 c->results[category].errors += inc;
517 c->results[total_check_result].errors += inc;
518 if (current_compiler > 0 &&
519 current_compiler < COMPILER_TABLE_MAX) {
520 c = &compilers_detected[current_compiler];
521 c->results[category].errors += inc;
522 c->results[total_check_result].errors += inc;
523 }
524 }
525
526 Dwarf_Bool
527 record_producer(char *name)
528 {
529 Dwarf_Bool recorded = FALSE;
530 if ((compilers_targeted_count+1) < COMPILER_TABLE_MAX) {
531 Compiler *pCompiler = 0;
532 const char *cmp = 0;
533 cmp = do_uri_translation(name,"-c<compiler name>");
534 /* First compiler at position [1] */
535 compilers_targeted_count++;
536 pCompiler = &compilers_targeted[compilers_targeted_count];
537 reset_compiler_entry(pCompiler);
538 pCompiler->name = cmp;
539 glflags.gf_check_all_compilers = FALSE;
540 recorded = TRUE;
541 }
542 return recorded;
543 }