1 /* $NetBSD: debug.c,v 1.58 2023/07/30 22:27:21 rillig Exp $ */ 2 3 /*- 4 * Copyright (c) 2021 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Roland Illig <rillig@NetBSD.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if defined(__RCSID) 38 __RCSID("$NetBSD: debug.c,v 1.58 2023/07/30 22:27:21 rillig Exp $"); 39 #endif 40 41 #include <stdlib.h> 42 #include <string.h> 43 44 #include "lint1.h" 45 #include "cgram.h" 46 47 48 #ifdef DEBUG 49 50 static int debug_indentation = 0; 51 static bool did_indentation; 52 53 54 static FILE * 55 debug_file(void) 56 { 57 /* 58 * Using stdout preserves the order between the debug messages and 59 * lint's diagnostics. 60 */ 61 return yflag ? stderr : stdout; 62 } 63 64 static void 65 debug_vprintf(const char *fmt, va_list va) 66 { 67 68 if (!did_indentation) { 69 did_indentation = true; 70 fprintf(debug_file(), "%s%*s", 71 yflag ? "| " : "", 2 * debug_indentation, ""); 72 } 73 74 (void)vfprintf(debug_file(), fmt, va); 75 76 did_indentation = strchr(fmt, '\n') == NULL; 77 } 78 79 void 80 debug_printf(const char *fmt, ...) 81 { 82 va_list va; 83 84 va_start(va, fmt); 85 debug_vprintf(fmt, va); 86 va_end(va); 87 } 88 89 void 90 debug_skip_indent(void) 91 { 92 93 did_indentation = true; 94 } 95 96 void 97 debug_indent_inc(void) 98 { 99 100 debug_indentation++; 101 } 102 103 void 104 debug_indent_dec(void) 105 { 106 107 lint_assert(debug_indentation > 0); 108 debug_indentation--; 109 } 110 111 void 112 debug_step(const char *fmt, ...) 113 { 114 va_list va; 115 116 va_start(va, fmt); 117 debug_vprintf(fmt, va); 118 va_end(va); 119 debug_printf("\n"); 120 } 121 122 void 123 debug_enter_func(const char *func) 124 { 125 126 debug_step("+ %s", func); 127 debug_indent_inc(); 128 } 129 130 void 131 debug_leave_func(const char *func) 132 { 133 134 debug_indent_dec(); 135 debug_step("- %s", func); 136 } 137 138 static void 139 debug_type_details(const type_t *tp) 140 { 141 142 if (is_struct_or_union(tp->t_tspec)) { 143 debug_indent_inc(); 144 debug_step("size %u bits, align %u bits, %s", 145 tp->t_sou->sou_size_in_bits, tp->t_sou->sou_align_in_bits, 146 tp->t_sou->sou_incomplete ? "incomplete" : "complete"); 147 148 for (const sym_t *mem = tp->t_sou->sou_first_member; 149 mem != NULL; mem = mem->s_next) { 150 debug_sym("", mem, "\n"); 151 debug_type_details(mem->s_type); 152 } 153 debug_indent_dec(); 154 } 155 if (tp->t_is_enum) { 156 debug_indent_inc(); 157 for (const sym_t *en = tp->t_enum->en_first_enumerator; 158 en != NULL; en = en->s_next) { 159 debug_sym("", en, "\n"); 160 } 161 debug_indent_dec(); 162 } 163 } 164 165 void 166 debug_type(const type_t *tp) 167 { 168 169 debug_step("type details for '%s':", type_name(tp)); 170 debug_type_details(tp); 171 } 172 173 void 174 debug_node(const tnode_t *tn) // NOLINT(misc-no-recursion) 175 { 176 op_t op; 177 178 if (tn == NULL) { 179 debug_step("null"); 180 return; 181 } 182 183 op = tn->tn_op; 184 debug_printf("'%s'", 185 op == CVT && !tn->tn_cast ? "convert" : op_name(op)); 186 if (op == NAME) 187 debug_printf(" '%s' with %s", 188 tn->tn_sym->s_name, 189 storage_class_name(tn->tn_sym->s_scl)); 190 else 191 debug_printf(" type"); 192 debug_printf(" '%s'", type_name(tn->tn_type)); 193 if (tn->tn_lvalue) 194 debug_printf(", lvalue"); 195 if (tn->tn_parenthesized) 196 debug_printf(", parenthesized"); 197 if (tn->tn_sys) 198 debug_printf(", sys"); 199 200 switch (op) { 201 case NAME: 202 debug_printf("\n"); 203 break; 204 case CON: 205 if (is_floating(tn->tn_type->t_tspec)) 206 debug_printf(", value %Lg", tn->tn_val.u.floating); 207 else if (is_uinteger(tn->tn_type->t_tspec)) 208 debug_printf(", value %llu", 209 (unsigned long long)tn->tn_val.u.integer); 210 else if (is_integer(tn->tn_type->t_tspec)) 211 debug_printf(", value %lld", 212 (long long)tn->tn_val.u.integer); 213 else { 214 lint_assert(tn->tn_type->t_tspec == BOOL); 215 debug_printf(", value %s", 216 tn->tn_val.u.integer != 0 ? "true" : "false"); 217 } 218 if (tn->tn_val.v_unsigned_since_c90) 219 debug_printf(", unsigned_since_c90"); 220 if (tn->tn_val.v_char_constant) 221 debug_printf(", char_constant"); 222 debug_printf("\n"); 223 break; 224 case STRING: 225 if (tn->tn_string->st_char) 226 debug_printf(", length %zu, \"%s\"\n", 227 tn->tn_string->st_len, 228 (const char *)tn->tn_string->st_mem); 229 else { 230 size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1); 231 char *s = xmalloc(n); 232 (void)wcstombs(s, tn->tn_string->st_mem, n); 233 debug_printf(", length %zu, L\"%s\"\n", 234 tn->tn_string->st_len, s); 235 free(s); 236 } 237 break; 238 default: 239 debug_printf("\n"); 240 241 debug_indent_inc(); 242 lint_assert(tn->tn_left != NULL); 243 debug_node(tn->tn_left); 244 if (op != INCBEF && op != INCAFT 245 && op != DECBEF && op != DECAFT 246 && op != CALL && op != ICALL && op != PUSH) 247 lint_assert(is_binary(tn) == (tn->tn_right != NULL)); 248 if (tn->tn_right != NULL) 249 debug_node(tn->tn_right); 250 debug_indent_dec(); 251 } 252 } 253 254 static const char * 255 def_name(def_t def) 256 { 257 static const char *const name[] = { 258 "not-declared", 259 "declared", 260 "tentative-defined", 261 "defined", 262 }; 263 264 return name[def]; 265 } 266 267 const char * 268 decl_level_kind_name(decl_level_kind kind) 269 { 270 static const char *const name[] = { 271 "extern", 272 "struct", 273 "union", 274 "enum", 275 "old-style-function-arguments", 276 "prototype-parameters", 277 "auto", 278 "abstract", 279 }; 280 281 return name[kind]; 282 } 283 284 const char * 285 scl_name(scl_t scl) 286 { 287 static const char *const name[] = { 288 "none", 289 "extern", 290 "static", 291 "auto", 292 "register", 293 "typedef", 294 "thread_local", 295 "struct", 296 "union", 297 "enum", 298 "member-of-struct", 299 "member-of-union", 300 "abstract", 301 "old-style-function-argument", 302 "prototype-argument", 303 }; 304 305 return name[scl]; 306 } 307 308 const char * 309 symt_name(symt_t kind) 310 { 311 static const char *const name[] = { 312 "var-func-type", 313 "member", 314 "tag", 315 "label", 316 }; 317 318 return name[kind]; 319 } 320 321 const char * 322 type_qualifiers_string(type_qualifiers tq) 323 { 324 static char buf[32]; 325 326 snprintf(buf, sizeof(buf), "%s%s%s%s", 327 tq.tq_const ? " const" : "", 328 tq.tq_restrict ? " restrict" : "", 329 tq.tq_volatile ? " volatile" : "", 330 tq.tq_atomic ? " atomic" : ""); 331 return buf[0] != '\0' ? buf + 1 : "none"; 332 } 333 334 const char * 335 function_specifier_name(function_specifier spec) 336 { 337 static const char *const name[] = { 338 "inline", 339 "_Noreturn", 340 }; 341 342 return name[spec]; 343 } 344 345 static void 346 debug_word(bool flag, const char *name) 347 { 348 349 if (flag) 350 debug_printf(" %s", name); 351 } 352 353 void 354 debug_sym(const char *prefix, const sym_t *sym, const char *suffix) 355 { 356 357 debug_printf("%s%s", prefix, sym->s_name); 358 if (sym->s_type != NULL) 359 debug_printf(" type='%s'", type_name(sym->s_type)); 360 if (sym->s_rename != NULL) 361 debug_printf(" rename=%s", sym->s_rename); 362 debug_printf(" %s", symt_name(sym->s_kind)); 363 debug_word(sym->s_keyword != NULL, "keyword"); 364 debug_word(sym->s_bitfield, "bit-field"); 365 debug_word(sym->s_set, "set"); 366 debug_word(sym->s_used, "used"); 367 debug_word(sym->s_arg, "argument"); 368 debug_word(sym->s_register, "register"); 369 debug_word(sym->s_defarg, "old-style-undefined"); 370 debug_word(sym->s_return_type_implicit_int, "return-int"); 371 debug_word(sym->s_osdef, "old-style"); 372 debug_word(sym->s_inline, "inline"); 373 debug_word(sym->s_ext_sym != NULL, "has-external"); 374 debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl)); 375 debug_word(sym->s_keyword == NULL, def_name(sym->s_def)); 376 377 if (sym->s_def_pos.p_file != NULL) 378 debug_printf(" defined-at=%s:%d", 379 sym->s_def_pos.p_file, sym->s_def_pos.p_line); 380 if (sym->s_set_pos.p_file != NULL) 381 debug_printf(" set-at=%s:%d", 382 sym->s_set_pos.p_file, sym->s_set_pos.p_line); 383 if (sym->s_use_pos.p_file != NULL) 384 debug_printf(" used-at=%s:%d", 385 sym->s_use_pos.p_file, sym->s_use_pos.p_line); 386 387 if (sym->s_type != NULL && sym->s_type->t_is_enum) 388 debug_printf(" value=%d", sym->u.s_enum_constant); 389 if (sym->s_type != NULL && sym->s_type->t_tspec == BOOL) 390 debug_printf(" value=%s", 391 sym->u.s_bool_constant ? "true" : "false"); 392 393 if (is_member(sym)) { 394 struct_or_union *sou = sym->u.s_member.sm_containing_type; 395 const char *tag = sou->sou_tag->s_name; 396 const sym_t *def = sou->sou_first_typedef; 397 if (tag == unnamed && def != NULL) 398 debug_printf(" sou='typedef %s'", def->s_name); 399 else 400 debug_printf(" sou='%s'", tag); 401 } 402 403 if (sym->s_keyword != NULL) { 404 int t = sym->u.s_keyword.sk_token; 405 if (t == T_TYPE || t == T_STRUCT_OR_UNION) 406 debug_printf(" %s", 407 tspec_name(sym->u.s_keyword.u.sk_tspec)); 408 if (t == T_QUAL) 409 debug_printf(" %s", type_qualifiers_string( 410 sym->u.s_keyword.u.sk_type_qualifier)); 411 if (t == T_FUNCTION_SPECIFIER) 412 debug_printf(" %s", function_specifier_name( 413 sym->u.s_keyword.u.function_specifier)); 414 } 415 416 debug_word(sym->s_osdef && sym->u.s_old_style_args != NULL, 417 "old-style-args"); 418 419 if (strcmp(suffix, "\n") == 0) 420 debug_printf("\n"); 421 else 422 debug_printf("%s", suffix); 423 } 424 425 static void 426 debug_decl_level(const decl_level *dl) 427 { 428 429 debug_printf("decl_level: %s", decl_level_kind_name(dl->d_kind)); 430 if (dl->d_scl != NOSCL) 431 debug_printf(" %s", scl_name(dl->d_scl)); 432 if (dl->d_type != NULL) 433 debug_printf(" '%s'", type_name(dl->d_type)); 434 else { 435 if (dl->d_abstract_type != NO_TSPEC) 436 debug_printf(" %s", tspec_name(dl->d_abstract_type)); 437 if (dl->d_complex_mod != NO_TSPEC) 438 debug_printf(" %s", tspec_name(dl->d_complex_mod)); 439 if (dl->d_sign_mod != NO_TSPEC) 440 debug_printf(" %s", tspec_name(dl->d_sign_mod)); 441 if (dl->d_rank_mod != NO_TSPEC) 442 debug_printf(" %s", tspec_name(dl->d_rank_mod)); 443 } 444 if (dl->d_redeclared_symbol != NULL) 445 debug_sym(" redeclared=(", dl->d_redeclared_symbol, ")"); 446 if (dl->d_sou_size_in_bits != 0) 447 debug_printf(" size=%u", dl->d_sou_size_in_bits); 448 if (dl->d_sou_align_in_bits != 0) 449 debug_printf(" align=%u", dl->d_sou_align_in_bits); 450 451 debug_word(dl->d_qual.tq_const, "const"); 452 debug_word(dl->d_qual.tq_restrict, "restrict"); 453 debug_word(dl->d_qual.tq_volatile, "volatile"); 454 debug_word(dl->d_qual.tq_atomic, "atomic"); 455 debug_word(dl->d_inline, "inline"); 456 debug_word(dl->d_multiple_storage_classes, "multiple_storage_classes"); 457 debug_word(dl->d_invalid_type_combination, "invalid_type_combination"); 458 debug_word(dl->d_nonempty_decl, "nonempty_decl"); 459 debug_word(dl->d_no_type_specifier, "no_type_specifier"); 460 debug_word(dl->d_asm, "asm"); 461 debug_word(dl->d_packed, "packed"); 462 debug_word(dl->d_used, "used"); 463 464 if (dl->d_tag_type != NULL) 465 debug_printf(" tag_type='%s'", type_name(dl->d_tag_type)); 466 for (const sym_t *arg = dl->d_func_args; 467 arg != NULL; arg = arg->s_next) 468 debug_sym(" arg(", arg, ")"); 469 if (dl->d_func_def_pos.p_file != NULL) 470 debug_printf(" func_def_pos=%s:%d:%d", 471 dl->d_func_def_pos.p_file, dl->d_func_def_pos.p_line, 472 dl->d_func_def_pos.p_uniq); 473 for (const sym_t *sym = dl->d_func_proto_syms; 474 sym != NULL; sym = sym->s_next) 475 debug_sym(" func_proto_sym(", sym, ")"); 476 debug_printf("\n"); 477 } 478 479 void 480 debug_dcs(bool all) 481 { 482 int prev_indentation = debug_indentation; 483 for (const decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) { 484 debug_decl_level(dl); 485 if (!all) 486 return; 487 debug_indentation++; 488 } 489 debug_indentation = prev_indentation; 490 } 491 #endif 492