diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c842a42c..de919367 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -212,7 +212,7 @@ jobs: sudo apt-get install -y --no-install-recommends \ qemu-system-arm qemu-system-misc qemu-system-mips \ qemu-system-ppc qemu-system-sparc qemu-system-x86 \ - ${{ matrix.qemu-riscv }} libglib2.0-dev \ + ${{ matrix.qemu-riscv }} libglib2.0-dev gdb-multiarch \ clang-${{ matrix.llvm }} llvm-${{ matrix.llvm }} lld-${{ matrix.llvm }} echo "/usr/lib/llvm-${{ matrix.llvm }}/bin" >> "$GITHUB_PATH" # The TriCore harness's output and the instruction counts are QEMU diff --git a/src/arch/regalloc.c b/src/arch/regalloc.c index 5e14a428..3c56be87 100644 --- a/src/arch/regalloc.c +++ b/src/arch/regalloc.c @@ -2487,12 +2487,19 @@ void ra_debug_locs(struct ir_func *fn, const struct ir_vhome *home) /* -g: room for each instruction's code offset (ir_func.ins_pc), when the * function has a variable mem2reg followed; NULL otherwise. */ +/* -g: does a local live in a nested block? Its DW_TAG_lexical_block needs + * its instructions' code, so ins_pc (dwarf.c, emit_scopes). */ +static int has_block_locals(const struct ir_func *fn) +{ + return fn->nscopes > 1 && fn->scope_up; +} + void ra_debug_pc_begin(struct ir_func *fn) { free(fn->ins_pc); fn->ins_pc = NULL; - if (((target_debug_info() && fn->vreg_var) || target_stack_info()) && - fn->nins) { + if (((target_debug_info() && (fn->vreg_var || has_block_locals(fn))) || + target_stack_info()) && fn->nins) { fn->ins_pc = xmalloc(((size_t)fn->nins + 1) * sizeof *fn->ins_pc); for (int k = 0; k <= fn->nins; k++) fn->ins_pc[k] = -1; diff --git a/src/debug/dwarf.c b/src/debug/dwarf.c index cc82ed65..865a4e57 100644 --- a/src/debug/dwarf.c +++ b/src/debug/dwarf.c @@ -18,6 +18,8 @@ #define DW_TAG_union_type 0x17 #define DW_TAG_member 0x0d #define DW_TAG_subrange_type 0x21 +#define DW_TAG_lexical_block 0x0b +#define DW_TAG_inlined_subroutine 0x1d #define DW_CHILDREN_no 0x00 #define DW_CHILDREN_yes 0x01 #define DW_AT_name 0x03 @@ -37,6 +39,13 @@ #define DW_AT_upper_bound 0x2f #define DW_AT_data_member_location 0x38 #define DW_AT_data_bit_offset 0x6b +#define DW_AT_decl_file 0x3a +#define DW_AT_decl_line 0x3b +#define DW_AT_inline 0x20 +#define DW_AT_abstract_origin 0x31 +#define DW_AT_call_column 0x57 +#define DW_AT_call_file 0x58 +#define DW_AT_call_line 0x59 #define DW_FORM_addr 0x01 #define DW_FORM_block1 0x0a #define DW_FORM_data1 0x0b @@ -149,6 +158,20 @@ #define AB_PARAM_LL 22 #define AB_VAR_T_LL 23 /* variable, with type, location list */ #define AB_VAR_LL 24 +#define AB_LEXBLOCK 25 /* lexical_block: a nested scope's code */ +#define AB_ABSFN 26 /* subprogram: an inlined function's abstract instance */ +#define AB_ABSFN_T 27 /* ...with a return type */ +#define AB_ABSPARAM_T 28 /* its parameter, with type, no location */ +#define AB_ABSPARAM 29 +#define AB_ABSVAR_T 30 /* its local */ +#define AB_ABSVAR 31 +#define AB_INLSUB 32 /* inlined_subroutine: one inlined call */ +#define AB_IPARAM 33 /* its parameter: the abstract one's, and where */ +#define AB_IPARAM_LL 34 +#define AB_IVAR 35 +#define AB_IVAR_LL 36 +#define AB_LEXBLOCK_R 37 /* lexical_block whose code is in pieces */ +#define AB_INLSUB_R 38 /* inlined_subroutine whose code is in pieces */ static int dw_ver = 5; @@ -370,11 +393,15 @@ static void emit_abbrev(struct dbuf *b) sub[3] = sub_t[5] = DW_FORM_addrx; sub[5] = sub_t[7] = DW_FORM_data4; } + /* (each with the file and line it was declared at: the line table's + * one file, 1) */ static const unsigned char param_t[] = { - DW_AT_name, DW_FORM_string, DW_AT_type, DW_FORM_ref4, + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_type, DW_FORM_ref4, DW_AT_location, DW_FORM_exprloc }; static const unsigned char param[] = { - DW_AT_name, DW_FORM_string, DW_AT_location, DW_FORM_exprloc }; + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_location, DW_FORM_exprloc }; static const unsigned char base[] = { DW_AT_name, DW_FORM_string, DW_AT_encoding, DW_FORM_data1, DW_AT_byte_size, DW_FORM_data1 }; @@ -398,10 +425,10 @@ static void emit_abbrev(struct dbuf *b) one_abbrev(b, AB_CU, DW_TAG_compile_unit, DW_CHILDREN_yes, cu, dw_split ? 4 : 7); /* (split: its pc and lines the skeleton's) */ one_abbrev(b, AB_SUBPROGRAM, DW_TAG_subprogram, DW_CHILDREN_yes, sub, 4); - one_abbrev(b, AB_PARAM_T, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t, 3); - one_abbrev(b, AB_PARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, param, 2); - one_abbrev(b, AB_VAR_T, DW_TAG_variable, DW_CHILDREN_no, param_t, 3); - one_abbrev(b, AB_VAR, DW_TAG_variable, DW_CHILDREN_no, param, 2); + one_abbrev(b, AB_PARAM_T, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t, 5); + one_abbrev(b, AB_PARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, param, 4); + one_abbrev(b, AB_VAR_T, DW_TAG_variable, DW_CHILDREN_no, param_t, 5); + one_abbrev(b, AB_VAR, DW_TAG_variable, DW_CHILDREN_no, param, 4); one_abbrev(b, AB_BASE, DW_TAG_base_type, DW_CHILDREN_no, base, 3); one_abbrev(b, AB_PTR_T, DW_TAG_pointer_type, DW_CHILDREN_no, ptr_t, 2); one_abbrev(b, AB_PTR, DW_TAG_pointer_type, DW_CHILDREN_no, ptr, 1); @@ -417,16 +444,73 @@ static void emit_abbrev(struct dbuf *b) one_abbrev(b, AB_SUBRANGE_NB, DW_TAG_subrange_type, DW_CHILDREN_no, subrange, 0); one_abbrev(b, AB_CU_RANGES, DW_TAG_compile_unit, DW_CHILDREN_yes, cu_ranges, 7); unsigned char param_t_ll[] = { - DW_AT_name, DW_FORM_string, DW_AT_type, DW_FORM_ref4, + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_type, DW_FORM_ref4, DW_AT_location, DW_FORM_sec_offset }; unsigned char param_ll[] = { - DW_AT_name, DW_FORM_string, DW_AT_location, DW_FORM_sec_offset }; + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_location, DW_FORM_sec_offset }; if (dw_split) - param_t_ll[5] = param_ll[3] = DW_FORM_loclistx; - one_abbrev(b, AB_PARAM_T_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t_ll, 3); - one_abbrev(b, AB_PARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_ll, 2); - one_abbrev(b, AB_VAR_T_LL, DW_TAG_variable, DW_CHILDREN_no, param_t_ll, 3); - one_abbrev(b, AB_VAR_LL, DW_TAG_variable, DW_CHILDREN_no, param_ll, 2); + param_t_ll[9] = param_ll[7] = DW_FORM_loclistx; + one_abbrev(b, AB_PARAM_T_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_t_ll, 5); + one_abbrev(b, AB_PARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, param_ll, 4); + one_abbrev(b, AB_VAR_T_LL, DW_TAG_variable, DW_CHILDREN_no, param_t_ll, 5); + one_abbrev(b, AB_VAR_LL, DW_TAG_variable, DW_CHILDREN_no, param_ll, 4); + /* a nested block's scope: where its code is, its variables inside */ + unsigned char lexb[] = { + DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_addr }; + if (dw_split) { + lexb[1] = DW_FORM_addrx; + lexb[3] = DW_FORM_data4; + } + one_abbrev(b, AB_LEXBLOCK, DW_TAG_lexical_block, DW_CHILDREN_yes, lexb, 2); + /* an inlined function's abstract instance (3.3.8.1): its name, that + * it was inlined, and its parameters and locals without locations */ + static const unsigned char absfn[] = { + DW_AT_name, DW_FORM_string, DW_AT_inline, DW_FORM_data1 }; + static const unsigned char absfn_t[] = { + DW_AT_name, DW_FORM_string, DW_AT_type, DW_FORM_ref4, + DW_AT_inline, DW_FORM_data1 }; + static const unsigned char absv_t[] = { + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata, DW_AT_type, DW_FORM_ref4 }; + static const unsigned char absv[] = { + DW_AT_name, DW_FORM_string, DW_AT_decl_file, DW_FORM_data1, + DW_AT_decl_line, DW_FORM_udata }; + one_abbrev(b, AB_ABSFN, DW_TAG_subprogram, DW_CHILDREN_yes, absfn, 2); + one_abbrev(b, AB_ABSFN_T, DW_TAG_subprogram, DW_CHILDREN_yes, absfn_t, 3); + one_abbrev(b, AB_ABSPARAM_T, DW_TAG_formal_parameter, DW_CHILDREN_no, absv_t, 4); + one_abbrev(b, AB_ABSPARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, absv, 3); + one_abbrev(b, AB_ABSVAR_T, DW_TAG_variable, DW_CHILDREN_no, absv_t, 4); + one_abbrev(b, AB_ABSVAR, DW_TAG_variable, DW_CHILDREN_no, absv, 3); + /* one inlined call: what was inlined, its code, where it was called */ + unsigned char inl[] = { + DW_AT_abstract_origin, DW_FORM_ref4, DW_AT_low_pc, DW_FORM_addr, + DW_AT_high_pc, DW_FORM_addr, DW_AT_call_file, DW_FORM_data1, + DW_AT_call_line, DW_FORM_udata, DW_AT_call_column, DW_FORM_udata }; + if (dw_split) { + inl[3] = DW_FORM_addrx; + inl[5] = DW_FORM_data4; + } + one_abbrev(b, AB_INLSUB, DW_TAG_inlined_subroutine, DW_CHILDREN_yes, inl, 6); + unsigned char iv[] = { DW_AT_abstract_origin, DW_FORM_ref4, + DW_AT_location, DW_FORM_exprloc }; + unsigned char iv_ll[] = { DW_AT_abstract_origin, DW_FORM_ref4, + DW_AT_location, DW_FORM_sec_offset }; + if (dw_split) + iv_ll[3] = DW_FORM_loclistx; + one_abbrev(b, AB_IPARAM, DW_TAG_formal_parameter, DW_CHILDREN_no, iv, 2); + one_abbrev(b, AB_IPARAM_LL, DW_TAG_formal_parameter, DW_CHILDREN_no, iv_ll, 2); + one_abbrev(b, AB_IVAR, DW_TAG_variable, DW_CHILDREN_no, iv, 2); + one_abbrev(b, AB_IVAR_LL, DW_TAG_variable, DW_CHILDREN_no, iv_ll, 2); + /* ...and both, where their code is not one run: DW_AT_ranges */ + static const unsigned char lexb_r[] = { DW_AT_ranges, DW_FORM_sec_offset }; + static const unsigned char inl_r[] = { + DW_AT_abstract_origin, DW_FORM_ref4, DW_AT_ranges, DW_FORM_sec_offset, + DW_AT_call_file, DW_FORM_data1, DW_AT_call_line, DW_FORM_udata, + DW_AT_call_column, DW_FORM_udata }; + one_abbrev(b, AB_LEXBLOCK_R, DW_TAG_lexical_block, DW_CHILDREN_yes, lexb_r, 1); + one_abbrev(b, AB_INLSUB_R, DW_TAG_inlined_subroutine, DW_CHILDREN_yes, inl_r, 5); db_uleb(b, 0); /* end of the abbrev table */ } @@ -670,6 +754,9 @@ static void emit_globals(struct dwarf_out *out, struct dbuf *b, continue; db_uleb(b, AB_VAR_T); db_str(b, g->name); + db_u8(b, 1); /* decl_file */ + db_uleb(b, (unsigned long)(g->name_line > 0 ? g->name_line + : g->line > 0 ? g->line : 0)); db_u32(b, (unsigned long)toff); long dsp = target_get() == TARGET_AVR ? AVR_DATA_SPACE : 0; if (dw_split) { @@ -793,6 +880,340 @@ static int list_header(struct dbuf *b) return at; } +/* The abstract instance of each function inlined somewhere in the unit: + * its DIE's offset, and its own variables' (by dbgvars index; -1 for one + * that came from a call inlined into it). */ +struct absfn { const struct func *f; int off; int *voff; int nv; }; + +/* The range lists of the blocks and inlined calls whose code is in more + * than one piece, appended to .debug_rnglists (.debug_ranges) after the + * unit's own list: dw_brg_base is where they start in that section. */ +static struct dbuf dw_brg; +static long dw_brg_base; + +/* A scope's code, as the ranges [lo[k], hi[k]) in ascending order. */ +struct sranges { long *lo, *hi; int n, cap; }; + +static void sr_add(struct sranges *r, long a, long z) +{ + if (r->n && r->hi[r->n - 1] >= a) { /* touching or overlapping */ + if (z > r->hi[r->n - 1]) r->hi[r->n - 1] = z; + return; + } + if (r->n == r->cap) { + r->cap = r->cap ? r->cap * 2 : 4; + r->lo = xrealloc(r->lo, (size_t)r->cap * sizeof *r->lo); + r->hi = xrealloc(r->hi, (size_t)r->cap * sizeof *r->hi); + } + r->lo[r->n] = a; + r->hi[r->n] = z; + r->n++; +} + +/* A list of r's ranges in dw_brg; its offset in the section. DWARF 5: + * start_end entries, each address relocated; DWARF 4: a base address + * selection entry of 0, then the address pairs, then the end pair. */ +static long emit_brg(struct dwarf_out *out, const struct sranges *r) +{ + long at = dw_brg_base + dw_brg.len; + if (dw_ver < 5) { + db_addr(&dw_brg, addr_bytes() == 8 ? ~0UL : 0xffffffffUL); + db_addr(&dw_brg, 0); + } + for (int k = 0; k < r->n; k++) { + if (dw_ver >= 5) + db_u8(&dw_brg, DW_RLE_start_end); + reloc(out, DWSEC_RANGES, (int)(dw_brg_base + dw_brg.len), addr_bytes(), + DWTGT_TEXT, r->lo[k]); + db_addr(&dw_brg, 0); + reloc_end(out, DWSEC_RANGES, (int)(dw_brg_base + dw_brg.len), addr_bytes(), + r->hi[k]); + db_addr(&dw_brg, 0); + } + if (dw_ver >= 5) { + db_u8(&dw_brg, DW_RLE_end_of_list); + } else { + db_addr(&dw_brg, 0); + db_addr(&dw_brg, 0); + } + return at; +} +static struct absfn *dw_abs; +static int dw_nabs; + +static const struct absfn *abs_of(const struct func *f) +{ + for (int k = 0; k < dw_nabs; k++) + if (dw_abs[k].f == f) + return &dw_abs[k]; + return NULL; +} + +/* One abstract instance per function some call to it was inlined: a + * DW_TAG_subprogram with DW_AT_inline and its parameters and locals, + * none with a location. Each inlined call's DIE refers to it, so gdb + * names the call and shows its frame in a backtrace. CU children, before + * the subprograms, as the types are. */ +static void emit_abstracts(struct dbuf *b, struct typemap *tm, + struct ir_unit *iu) +{ + free(dw_abs); + dw_abs = NULL; + dw_nabs = 0; + for (int n = 0; n < iu->nfuncs; n++) { + struct ir_func *fn = &iu->funcs[n]; + if (!fn->scope_fn) + continue; + for (int s = 1; s < fn->nscopes; s++) { + const struct func *f = fn->scope_fn[s]; + if (!f || abs_of(f)) + continue; + /* the inlined function's own variables, from the copies of + * them its calls left in the callers (the function itself is + * often gone, every call inlined): by their index there */ + int nv = 0; + for (int m = 0; m < iu->nfuncs; m++) + for (int v = 0; v < iu->funcs[m].ndbgvars; v++) { + const struct ir_dbgvar *dv = &iu->funcs[m].dbgvars[v]; + if (dv->origin == f && dv->origin_idx >= nv) + nv = dv->origin_idx + 1; + } + const struct ir_dbgvar **pick = xcalloc((size_t)(nv ? nv : 1), sizeof *pick); + for (int m = 0; m < iu->nfuncs; m++) + for (int v = 0; v < iu->funcs[m].ndbgvars; v++) { + const struct ir_dbgvar *dv = &iu->funcs[m].dbgvars[v]; + if (dv->origin == f && dv->origin_idx >= 0 && !pick[dv->origin_idx]) + pick[dv->origin_idx] = dv; + } + dw_abs = xrealloc(dw_abs, (size_t)(dw_nabs + 1) * sizeof *dw_abs); + struct absfn *A = &dw_abs[dw_nabs++]; + A->f = f; + A->off = b->len; + A->nv = nv; + A->voff = xmalloc((size_t)(nv ? nv : 1) * sizeof *A->voff); + int rt = type_lookup(tm, f->ret_ty); + db_uleb(b, rt >= 0 ? AB_ABSFN_T : AB_ABSFN); + db_str(b, f->name); + if (rt >= 0) db_u32(b, (unsigned long)rt); + db_u8(b, 1); /* DW_INL_inlined */ + for (int v = 0; v < nv; v++) { + const struct ir_dbgvar *dv = pick[v]; + A->voff[v] = -1; + if (!dv) + continue; + int toff = type_lookup(tm, dv->ty); + A->voff[v] = b->len; + db_uleb(b, dv->is_param ? (toff >= 0 ? AB_ABSPARAM_T : AB_ABSPARAM) + : (toff >= 0 ? AB_ABSVAR_T : AB_ABSVAR)); + db_str(b, dv->name); + db_u8(b, 1); + db_uleb(b, (unsigned long)(dv->line > 0 ? dv->line : 0)); + if (toff >= 0) db_u32(b, (unsigned long)toff); + } + db_u8(b, 0); + free(pick); + } + } +} + +/* One parameter's or local's DIE: its name, where it was declared, its + * type, and its location -- a frame slot, or where the optimizer keeps it, + * range by range (ra_debug_locs). */ +static void emit_dbgvar(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, + struct typemap *tm, struct ir_func *fn, int v, long lo) +{ + struct ir_dbgvar *dv = &fn->dbgvars[v]; + int toff = type_lookup(tm, dv->ty); + int ll = fn->ndbglocs && fn->ndbglocs[v] > 0 && + (fn->var_off[dv->vreg] == IR_VAR_NO_LOC || dv->moved); + const struct absfn *A = dv->origin ? abs_of(dv->origin) : NULL; + int ao = A && dv->origin_idx >= 0 && dv->origin_idx < A->nv + ? A->voff[dv->origin_idx] : -1; + if (ao >= 0) { + /* an inlined call's: the abstract one's name and type, and where + * it is in this copy */ + db_uleb(b, dv->is_param ? (ll ? AB_IPARAM_LL : AB_IPARAM) + : (ll ? AB_IVAR_LL : AB_IVAR)); + db_u32(b, (unsigned long)ao); + goto location; + } + int code = dv->is_param + ? (toff >= 0 ? (ll ? AB_PARAM_T_LL : AB_PARAM_T) + : (ll ? AB_PARAM_LL : AB_PARAM)) + : (toff >= 0 ? (ll ? AB_VAR_T_LL : AB_VAR_T) + : (ll ? AB_VAR_LL : AB_VAR)); + db_uleb(b, code); + db_str(b, dv->name); + db_u8(b, 1); /* decl_file */ + db_uleb(b, (unsigned long)(dv->line > 0 ? dv->line : 0)); + if (toff >= 0) db_u32(b, (unsigned long)toff); +location: + if (ll && dw_split) { + if (dw_nll == dw_capll) { + dw_capll = dw_capll ? dw_capll * 2 : 16; + dw_lloff = xrealloc(dw_lloff, (size_t)dw_capll * sizeof *dw_lloff); + } + dw_lloff[dw_nll] = loc->len; + db_uleb(b, (unsigned long)dw_nll++); + emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); + } else if (ll) { + reloc(out, DWSEC_INFO, b->len, 4, DWTGT_LOC, loc->len); + db_u32(b, 0); + emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); + } else { + loc_fbreg(b, fn->var_off[dv->vreg]); + } +} + +/* Where instruction k's code starts: its ins_pc, or for one fused into + * the instruction before it (no offset of its own), where the next one's + * does; within the function. */ +static long ins_start(const struct ir_func *fn, int k, long lo, long hi) +{ + for (; k < fn->nins; k++) + if (fn->ins_pc[k] >= 0) + break; + long pc = fn->ins_pc[k < fn->nins ? k : fn->nins]; + if (pc < 0) + pc = hi; + return pc < lo ? lo : pc > hi ? hi : pc; +} + +/* One block's DIE, its variables, and the blocks nested in it. */ +/* Each scope's first child and next sibling, and its first variable and + * the next one in the same scope: built once, so a function of 200,000 + * blocks is not 200,000 scans of them all. */ +struct stree { int *kid, *sib, *var, *vnext; }; + +static void emit_block(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, + struct typemap *tm, struct ir_func *fn, long lo, + int s, const long *smin, const long *smax, + const struct sranges *sr, const struct stree *st) +{ + const struct absfn *A = s > 0 && fn->scope_fn ? abs_of(fn->scope_fn[s]) : NULL; + /* in pieces (not in a .dwo, which keeps the extent): DW_AT_ranges */ + int pieces = s > 0 && sr && sr[s].n > 1 && !dw_split; + if (A) { + db_uleb(b, pieces ? AB_INLSUB_R : AB_INLSUB); + db_u32(b, (unsigned long)A->off); + } else if (s > 0) { + db_uleb(b, pieces ? AB_LEXBLOCK_R : AB_LEXBLOCK); + } + if (pieces) { + reloc(out, DWSEC_INFO, b->len, 4, DWTGT_RANGES, emit_brg(out, &sr[s])); + db_u32(b, 0); + } else if (s > 0) { + if (dw_split) { + db_uleb(b, (unsigned long)addr_index(DWTGT_TEXT, smin[s], NULL, 0)); + db_u32(b, (unsigned long)(smax[s] - smin[s])); + } else { + reloc(out, DWSEC_INFO, b->len, addr_bytes(), DWTGT_TEXT, smin[s]); + db_addr(b, 0); + reloc_end(out, DWSEC_INFO, b->len, addr_bytes(), smax[s]); + db_addr(b, 0); + } + } + if (A) { + db_u8(b, 1); /* call_file */ + db_uleb(b, (unsigned long)(fn->scope_cline[s] > 0 ? fn->scope_cline[s] : 0)); + db_uleb(b, (unsigned long)(fn->scope_ccol[s] > 0 ? fn->scope_ccol[s] : 0)); + } + if (!st) { + for (int v = 0; v < fn->ndbgvars; v++) + emit_dbgvar(out, b, loc, tm, fn, v, lo); + } else { + for (int v = st->var[s]; v >= 0; v = st->vnext[v]) + emit_dbgvar(out, b, loc, tm, fn, v, lo); + for (int c = st->kid[s]; c >= 0; c = st->sib[c]) + if (smin[c] < smax[c]) + emit_block(out, b, loc, tm, fn, lo, c, smin, smax, sr, st); + } + if (s > 0) + db_u8(b, 0); +} + +/* A function's parameters and locals, each in its scope. A local declared + * in a nested block -- or in a for statement's first clause -- lives in a + * DW_TAG_lexical_block over its block's code, so `for (int i ...)` twice + * is two variables a debugger shows one at a time, and an inner `s` hides + * the outer one only inside its block. They were all the function's, and + * gdb printed the first `i` in the second loop -- at -O0, from its own, + * stale, slot. A block's code is that of every instruction irgen made in + * it or in a block inside it (ir_ins.scope, which instructions keep + * through every pass), its extent from the first to the last. A block the + * optimizer left with no code has no DIE, nor do its variables: nothing + * can stop inside it, and at the function's level its variable would + * shadow the function's own of that name. Where the backend recorded no + * ins_pc, every variable is the function's, as before. */ +static void emit_scopes(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, + struct typemap *tm, struct ir_func *fn, long lo, + long hi) +{ + if (fn->ndbgvars <= 0) + return; + if (!fn->ins_pc || fn->nscopes <= 1 || !fn->scope_up || fn->nins <= 0) { + emit_block(out, b, loc, tm, fn, lo, 0, NULL, NULL, NULL, NULL); + return; + } + int ns = fn->nscopes; + long *smin = xmalloc((size_t)ns * sizeof *smin); + long *smax = xmalloc((size_t)ns * sizeof *smax); + struct sranges *sr = xcalloc((size_t)ns, sizeof *sr); + for (int s = 0; s < ns; s++) { + smin[s] = hi; + smax[s] = lo; + } + for (int k = 0; k < fn->nins; k++) { + long a = ins_start(fn, k, lo, hi), z = ins_start(fn, k + 1, lo, hi); + if (z <= a) + continue; + for (int t = fn->ins[k].scope; t > 0 && t < ns; t = fn->scope_up[t]) { + if (a < smin[t]) smin[t] = a; + if (z > smax[t]) smax[t] = z; + sr_add(&sr[t], a, z); + } + } + /* a variable of a block with no code: shown nowhere */ + for (int v = 0; v < fn->ndbgvars; v++) { + int vs = fn->dbgvars[v].scope; + if (vs > 0 && vs < ns && smin[vs] >= smax[vs]) + fn->dbgvars[v].scope = -1; + } + struct stree st; + st.kid = xmalloc((size_t)ns * sizeof *st.kid); + st.sib = xmalloc((size_t)ns * sizeof *st.sib); + st.var = xmalloc((size_t)ns * sizeof *st.var); + st.vnext = xmalloc((size_t)(fn->ndbgvars ? fn->ndbgvars : 1) * sizeof *st.vnext); + for (int s = 0; s < ns; s++) + st.kid[s] = st.sib[s] = st.var[s] = -1; + for (int c = ns - 1; c > 0; c--) { /* in id order, each list */ + int u = fn->scope_up[c]; + if (u >= 0 && u < ns) { + st.sib[c] = st.kid[u]; + st.kid[u] = c; + } + } + for (int v = fn->ndbgvars - 1; v >= 0; v--) { + int vs = fn->dbgvars[v].scope; + st.vnext[v] = -1; + if (vs < 0) + continue; /* its block has no code */ + if (vs >= ns) + vs = 0; + st.vnext[v] = st.var[vs]; + st.var[vs] = v; + } + emit_block(out, b, loc, tm, fn, lo, 0, smin, smax, sr, &st); + free(st.kid); free(st.sib); free(st.var); free(st.vnext); + for (int s = 0; s < ns; s++) { + free(sr[s].lo); + free(sr[s].hi); + } + free(sr); + free(smin); + free(smax); +} + static void emit_info(struct dwarf_out *out, struct dbuf *b, struct dbuf *loc, struct ir_unit *iu, const char *filename, @@ -850,6 +1271,7 @@ static void emit_info(struct dwarf_out *out, struct dbuf *b, (void)ensure_type(b, &tm, iu->funcs[n].dbgvars[v].ty); } emit_globals(out, b, &tm, iu); + emit_abstracts(b, &tm, iu); for (int n = 0; n < iu->nfuncs; n++) { struct ir_func *fn = &iu->funcs[n]; @@ -944,38 +1366,7 @@ static void emit_info(struct dwarf_out *out, struct dbuf *b, } } - for (int v = 0; v < fn->ndbgvars; v++) { - struct ir_dbgvar *dv = &fn->dbgvars[v]; - int toff = type_lookup(&tm, dv->ty); - /* a variable the optimizer took out of memory: where it is, - * range by range (ra_debug_locs) */ - int ll = fn->ndbglocs && fn->ndbglocs[v] > 0 && - (fn->var_off[dv->vreg] == IR_VAR_NO_LOC || dv->moved); - int code = dv->is_param - ? (toff >= 0 ? (ll ? AB_PARAM_T_LL : AB_PARAM_T) - : (ll ? AB_PARAM_LL : AB_PARAM)) - : (toff >= 0 ? (ll ? AB_VAR_T_LL : AB_VAR_T) - : (ll ? AB_VAR_LL : AB_VAR)); - db_uleb(b, code); - db_str(b, dv->name); - if (toff >= 0) db_u32(b, (unsigned long)toff); - if (ll && dw_split) { - if (dw_nll == dw_capll) { - dw_capll = dw_capll ? dw_capll * 2 : 16; - dw_lloff = xrealloc(dw_lloff, - (size_t)dw_capll * sizeof *dw_lloff); - } - dw_lloff[dw_nll] = loc->len; - db_uleb(b, (unsigned long)dw_nll++); - emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); - } else if (ll) { - reloc(out, DWSEC_INFO, b->len, 4, DWTGT_LOC, loc->len); - db_u32(b, 0); - emit_loclist(out, loc, fn->dbglocs[v], fn->ndbglocs[v], lo); - } else { - loc_fbreg(b, fn->var_off[dv->vreg]); - } - } + emit_scopes(out, b, loc, &tm, fn, lo, hi); db_u8(b, 0); /* end of this subprogram's children */ } db_u8(b, 0); /* end of the CU's children */ @@ -1394,6 +1785,19 @@ static void emit_all(struct ir_unit *iu, const char *filename, int lc_empty = lc.len; emit_abbrev(&ab); emit_frame(out, &fr, iu); + /* the blocks' range lists start after the header and the unit's own + * list, whose size is known before any is written */ + { + int nf = 0; + for (int n = 0; n < iu->nfuncs; n++) + if (iu->funcs[n].src->code_len > 0) + nf++; + long ab2 = 2L * addr_bytes(); + dw_brg_base = dw_ver >= 5 ? 12 + (split ? nf * (1 + ab2) + 1 : 0) + : (split ? (nf + 1) * ab2 : 0); + free(dw_brg.p); + memset(&dw_brg, 0, sizeof dw_brg); + } emit_info(out, &in, &lc, iu, filename, lo, hi, split); emit_line(out, &ln, iu, filename); if (dw_split) { @@ -1404,11 +1808,12 @@ static void emit_all(struct ir_unit *iu, const char *filename, } else if (lc_at >= 0) { db_patch_u32(&lc, lc_at, (unsigned long)(lc.len - lc_at - 4)); } - if (split) { + if (split || dw_brg.len) { /* the ranges: [begin, end) per function, each relocated into the * function's own section -- .debug_rnglists' start_end entries * (DWARF 5), or .debug_ranges' address pairs ended by two zeros */ int rg_at = dw_ver >= 5 ? list_header(&rg) : -1; + if (split) { for (int n = 0; n < iu->nfuncs; n++) { if (iu->funcs[n].src->code_len <= 0) continue; long f_lo = iu->funcs[n].src->code_off; @@ -1422,11 +1827,19 @@ static void emit_all(struct ir_unit *iu, const char *filename, } if (dw_ver >= 5) { db_u8(&rg, DW_RLE_end_of_list); - db_patch_u32(&rg, rg_at, (unsigned long)(rg.len - rg_at - 4)); } else { db_addr(&rg, 0); db_addr(&rg, 0); } + } + if (rg.len != dw_brg_base) + internal_error("dwarf: the unit's range list is %d bytes, not the " + "%ld its blocks' lists were placed after", rg.len, + dw_brg_base); + for (int k = 0; k < dw_brg.len; k++) + db_u8(&rg, dw_brg.p[k]); + if (dw_ver >= 5) + db_patch_u32(&rg, rg_at, (unsigned long)(rg.len - rg_at - 4)); } out->sec[DWSEC_ABBREV] = ab.p; out->seclen[DWSEC_ABBREV] = ab.len; diff --git a/src/ir/ir.h b/src/ir/ir.h index 7f2f6cd6..408bedb2 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -311,6 +311,11 @@ struct ir_ins { int line; int col; int synth; + /* -g: the lexical scope the instruction's code belongs to -- 0 the + * function's own, else an index into ir_func.scope_up (irgen). Copied + * with the instruction, as its line is; an instruction a pass invents + * reads 0, which leaves its code outside any nested block. */ + int scope; int dst, a, b; int c; /* IR_CMPXCHG: the third operand (desired value) */ int nrp; /* IR_RET of a struct as its register units @@ -547,6 +552,12 @@ struct ir_dbgvar { int is_param; struct type *ty; int line, col; + int scope; /* its block's lexical scope (ir_ins.scope) */ + /* A variable of an inlined call: the function it was declared in and + * its index among that function's dbgvars (the DWARF's abstract + * origin); NULL and -1 for the function's own. */ + struct func *origin; + int origin_idx; /* mem2reg took this variable out of memory although something * assigned it: its slot no longer follows it. Its only reader is a * backend deciding whether the slot is a true DW_AT_location -- for a @@ -647,6 +658,18 @@ struct ir_func { * function-level locals span the whole function; only nested-block locals get * a narrower range. Length nvars; unused (NULL) when there are no locals. */ int *var_scope_lo, *var_scope_hi; + /* -g: the lexical scopes of the blocks that declare locals, 1 to + * nscopes - 1 (0 is the function's own): scope_up[s] the one it is + * nested in. Instructions name theirs (ir_ins.scope), and the DWARF + * writer makes a DW_TAG_lexical_block of each from its instructions' + * code -- which, unlike var_scope_lo/hi, no reordering makes stale. */ + int *scope_up; + int nscopes, scope_cap; + /* ...and where a scope is an inlined call (the inliner), the function + * inlined there and the call's line and column; NULL and 0 for a + * block. Each array nscopes long, or NULL while no call is inlined. */ + struct func **scope_fn; + int *scope_cline, *scope_ccol; /* exception regions (C++'s lowering), the catch types their landing * pads' selectors number (1-based; NULL: catch-all), and — codegen — * every call's code */ diff --git a/src/ir/irgen.c b/src/ir/irgen.c index eb481cb5..60c5ce2b 100644 --- a/src/ir/irgen.c +++ b/src/ir/irgen.c @@ -183,6 +183,17 @@ void ir_locals_fill(struct ir_func *fn, struct func *f, int nvars) } } +/* -g: the lexical scope being generated (ir_ins.scope). collect_locals + * gives each STMT_BLOCK or STMT_FOR that declares a local its scope, in + * the statement itself (dbg_scope): a lookup by pointer was a search, and + * 200,000 blocks in one function made it quadratic (many-declarations). */ +static int g_cur_scope; + +static int scope_of_stmt(const struct stmt *s) +{ + return s->dbg_scope - 1; +} + struct ir_ins *emit(struct ir_func *fn) { if (fn->nins == fn->cap) { @@ -200,6 +211,7 @@ struct ir_ins *emit(struct ir_func *fn) i->line = g_cur_line; i->col = g_cur_col; i->synth = g_synth; + i->scope = g_cur_scope; i->dst = i->a = i->b = -1; i->w = 4; i->size = 4; @@ -7083,6 +7095,10 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s, lc.cont = new_label(fn); lc.brk = new_label(fn); int for_vla = g_nvla; + int for_lo = fn->nins; + int for_scope = g_cur_scope, fs = scope_of_stmt(s); + if (fs >= 0) + g_cur_scope = fs; lc.brk_vla = lc.cont_vla = g_nvla; if (s->initdecl) gen_stmt(fn, s->initdecl, &lc); @@ -7105,6 +7121,17 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s, emit_label(fn, lc.brk); vla_release(fn, for_vla); g_nvla = for_vla; + /* `for (int i = 0; ...)`: i's scope is the for statement + * (C11 6.8.5p5), as a block's local's is its block -- so two + * loops' i are two variables a debugger tells apart, and may + * share a slot */ + for (struct stmt *c = s->initdecl; c; c = c->next) + if (c->kind == STMT_DECL && !c->is_extern && !c->sglob && + c->var_index >= 0 && c->var_index < fn->nvars) { + fn->var_scope_lo[c->var_index] = for_lo; + fn->var_scope_hi[c->var_index] = fn->nins; + } + g_cur_scope = for_scope; break; } case STMT_BLOCK: { @@ -7114,7 +7141,11 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s, * (codegen). Static/extern decls have no frame slot — skip them. */ int lo = fn->nins; int depth = g_nvla; + int up_scope = g_cur_scope, bs = scope_of_stmt(s); + if (bs >= 0) + g_cur_scope = bs; gen_stmt(fn, s->body, loop); + g_cur_scope = up_scope; vla_release(fn, depth); /* the block's VLAs end with it */ g_nvla = depth; int hi = fn->nins; @@ -7195,42 +7226,79 @@ static void add_dbgvar(struct ir_func *fn, const char *name, int vreg, v->line = line; v->col = col; v->moved = 0; + v->scope = 0; + v->origin = NULL; + v->origin_idx = -1; } /* -g: walk the body for block-scope locals. Each STMT_DECL owns a var slot * (var_index); a static local became a global (sglob) and has no frame slot, * so it is skipped. Flattened into the subprogram — lexical-block scoping is a * later refinement, not needed to print a local by name. */ -static void collect_locals(struct ir_func *fn, struct stmt *s) +/* Does this list of statements declare a local of its own (a slot, not an + * extern or a static)? */ +static int declares_local(const struct stmt *s) +{ + for (; s; s = s->next) + if (s->kind == STMT_DECL && !s->is_extern && !s->is_vm_typedef && + !s->sglob) + return 1; + return 0; +} + +/* A new lexical scope for block s, inside scope `up`. */ +static int new_scope(struct ir_func *fn, struct stmt *s, int up) +{ + if (fn->nscopes == fn->scope_cap) { + fn->scope_cap = fn->scope_cap ? fn->scope_cap * 2 : 8; + fn->scope_up = xrealloc(fn->scope_up, (size_t)fn->scope_cap * sizeof *fn->scope_up); + } + int id = fn->nscopes++; + fn->scope_up[id] = up; + s->dbg_scope = id + 1; + return id; +} + +static void collect_locals(struct ir_func *fn, struct stmt *s, int scope) { for (; s; s = s->next) { switch (s->kind) { case STMT_DECL: if (s->is_extern || s->is_vm_typedef) /* no local slot at all */ break; - if (!s->sglob) + if (!s->sglob) { add_dbgvar(fn, s->name, s->var_index, 0, fn->src->var_tys[s->var_index], s->line, s->col); + fn->dbgvars[fn->ndbgvars - 1].scope = scope; + } break; case STMT_IF: - collect_locals(fn, s->thn); - collect_locals(fn, s->els); + collect_locals(fn, s->thn, scope); + collect_locals(fn, s->els, scope); break; case STMT_EHREGION: - collect_locals(fn, s->body); - collect_locals(fn, s->thn); + collect_locals(fn, s->body, scope); + collect_locals(fn, s->thn, scope); break; case STMT_WHILE: case STMT_DO: - collect_locals(fn, s->body); + collect_locals(fn, s->body, scope); break; - case STMT_FOR: - collect_locals(fn, s->initdecl); - collect_locals(fn, s->body); + case STMT_FOR: { + /* `for (int i ...)`: i's scope is the for statement */ + s->dbg_scope = 0; + int in = declares_local(s->initdecl) ? new_scope(fn, s, scope) : scope; + collect_locals(fn, s->initdecl, in); + collect_locals(fn, s->body, in); break; + } case STMT_BLOCK: + s->dbg_scope = 0; + collect_locals(fn, s->body, + declares_local(s->body) ? new_scope(fn, s, scope) : scope); + break; case STMT_SWITCH: - collect_locals(fn, s->body); + collect_locals(fn, s->body, scope); break; default: break; @@ -7406,7 +7474,12 @@ static void gen_func_(struct ir_func *fn, struct func *f) for (int i = 0; i < f->nparams; i++) add_dbgvar(fn, f->params[i], i, 1, f->param_tys[i], f->param_lines[i], f->param_cols[i]); - collect_locals(fn, f->body); + g_cur_scope = 0; + fn->nscopes = 1; + fn->scope_cap = 8; + fn->scope_up = xmalloc((size_t)fn->scope_cap * sizeof *fn->scope_up); + fn->scope_up[0] = -1; + collect_locals(fn, f->body, 0); /* Local scope ranges: default to the whole function ([0, +inf), narrowed to * the real end below) so any local not inside a nested block never coalesces * — the safe default. gen_stmt's STMT_BLOCK case narrows nested-block locals. */ diff --git a/src/link/link.c b/src/link/link.c index 7e06a1b9..4df4af3d 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -1538,7 +1538,12 @@ static void collect_sections(struct linker *l, struct object *o) * Concatenated per name; the offset this object's piece * landed at is what its relocations resolve against. */ const char *nm = o->shstr + sh->sh_name; - if (l->keep_debug && sh->sh_type == SHT_PROGBITS && + /* (MIPS objects from GCC and clang type theirs + * SHT_MIPS_DWARF, 0x7000001e, not PROGBITS: GNU ld keeps + * them, and this dropped every one, so a clang-built MIPS + * image had no debug info at all) */ + if (l->keep_debug && (sh->sh_type == SHT_PROGBITS || + sh->sh_type == 0x7000001eu) && strncmp(nm, ".debug_", 7) == 0 && sh->sh_size && !(sh->sh_flags & 0x80000000u /* SHF_EXCLUDE */)) { int d = dbgsec_for(l, nm); diff --git a/src/opt/inline.c b/src/opt/inline.c index dec35a3e..173fafdf 100644 --- a/src/opt/inline.c +++ b/src/opt/inline.c @@ -252,6 +252,11 @@ static int inlinable(struct ir_func *cf, int force, int sole, const char **why, detail[0] = 0; if (c->is_varargs) { *why = "callee-is-varargs"; return 0; } + /* main stays a frame of its own, as GCC keeps it: called once, from a + * reset handler or crt0, it gains nothing inlined, and inlined it is + * an inline frame of its caller -- gdb's backtrace then runs past the + * entry function into 0x0, and `break main` stops in reset's code */ + if (cf->name && !strcmp(cf->name, "main")) { *why = "callee-is-main"; return 0; } if (cf->nins == 0) { *why = "callee-not-defined-here"; return 0; } /* __attribute__((always_inline)) overrides the SIZE budget and * nothing else. Every other test below is a thing this inliner @@ -587,6 +592,46 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf) ad->line = call.line; ad->col = call.col; } + /* -g: the callee's code is an inlined call, a scope of the caller's + * inside the call's, and the callee's own scopes inside that one -- + * its blocks, and the calls inlined into it before -- renumbered from + * `base`. The DWARF writer makes it a DW_TAG_inlined_subroutine. */ + int base = -1; + if (fn->scope_up && cf->scope_up && cf->nscopes > 0) { + base = fn->nscopes; + int ns = base + cf->nscopes; + int had = fn->scope_fn != NULL; + fn->scope_up = xrealloc(fn->scope_up, (size_t)ns * sizeof *fn->scope_up); + fn->scope_cap = ns; + fn->scope_fn = xrealloc(fn->scope_fn, (size_t)ns * sizeof *fn->scope_fn); + fn->scope_cline = xrealloc(fn->scope_cline, (size_t)ns * sizeof *fn->scope_cline); + fn->scope_ccol = xrealloc(fn->scope_ccol, (size_t)ns * sizeof *fn->scope_ccol); + if (!had) + for (int t = 0; t < base; t++) { + fn->scope_fn[t] = NULL; + fn->scope_cline[t] = fn->scope_ccol[t] = 0; + } + for (int t = 0; t < cf->nscopes; t++) { + int id = base + t; + if (t == 0) { + fn->scope_up[id] = call.scope; + fn->scope_fn[id] = cf->src; + fn->scope_cline[id] = call.line; + fn->scope_ccol[id] = call.col; + } else { + fn->scope_up[id] = base + cf->scope_up[t]; + fn->scope_fn[id] = cf->scope_fn ? cf->scope_fn[t] : NULL; + fn->scope_cline[id] = cf->scope_cline ? cf->scope_cline[t] : 0; + fn->scope_ccol[id] = cf->scope_ccol ? cf->scope_ccol[t] : 0; + } + } + fn->nscopes = ns; + } + for (int k = 0; k < m; k++) + buf[k].scope = base < 0 ? call.scope + : base + (buf[k].scope > 0 && buf[k].scope < cf->nscopes + ? buf[k].scope : 0); + /* 3. Splice buf over the call. */ int newn = fn->nins - 1 + m; struct ir_ins *ni = xmalloc((size_t)newn * sizeof *ni); @@ -655,6 +700,30 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf) fn->var_scope_lo = lo; fn->var_scope_hi = hi; } fn->nvars = nv; + + /* -g: the callee's parameters and locals are the caller's slots V + k + * now, so the caller's own mem2reg follows them (vreg_var) and they get + * location lists like any local; each in its inlined scope, its origin + * the callee's variable (or, inlined into the callee before, that + * one's own). */ + if (base >= 0) + for (int d = 0; d < cf->ndbgvars; d++) { + struct ir_dbgvar x = cf->dbgvars[d]; + if (x.vreg < 0 || x.vreg >= v) + continue; + x.vreg = V + x.vreg; + x.scope = base + (x.scope > 0 && x.scope < cf->nscopes ? x.scope : 0); + if (!x.origin) { + x.origin = cf->src; + x.origin_idx = d; + } + if (fn->ndbgvars == fn->dbgvarcap) { + fn->dbgvarcap = fn->dbgvarcap ? fn->dbgvarcap * 2 : 8; + fn->dbgvars = xrealloc(fn->dbgvars, + (size_t)fn->dbgvarcap * sizeof *fn->dbgvars); + } + fn->dbgvars[fn->ndbgvars++] = x; + } } /* How many calls in the whole unit name this function, and does its diff --git a/src/parse/ast.h b/src/parse/ast.h index 93f90f1e..5495caad 100644 --- a/src/parse/ast.h +++ b/src/parse/ast.h @@ -273,6 +273,9 @@ struct asm_stmt { struct stmt { enum stmt_kind kind; + /* -g: 1 + the lexical scope irgen gave this block or for statement + * (ir_func.scope_up), 0 when it declares no local of its own */ + int dbg_scope; int line; int col; /* 1-based column, for caret diagnostics (0 = unknown) */ const char *name; /* STMT_DECL */ diff --git a/tests/golden/debug-embedded.sh b/tests/golden/debug-embedded.sh index 3fa07a73..24fd4eea 100755 --- a/tests/golden/debug-embedded.sh +++ b/tests/golden/debug-embedded.sh @@ -104,7 +104,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # p is the first parameter: fbreg +0, and the prologue stores # the first argument register there. off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) [ -n "$off" ] || { echo "FAIL $t: no location for 'p'"; fail=1; continue; } n=$(printf '%d' "$off") @@ -123,7 +123,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # ...and MIPS, whose frame base is $sp too: p arrives in $4 (a0) o="$out/mipsel-none-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$OBJDUMP" -d "$o" 2>/dev/null | grep -qE "sw[[:space:]]+\\\$4,[[:space:]]*0x$(printf '%x' "$(printf '%d' "$off")")\(\\\$sp\)"; then @@ -135,7 +135,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # ...and LoongArch64, whose frame base is $sp (r3): p arrives in a0 o="$out/loongarch64-unknown-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$OBJDUMP" -d "$o" 2>/dev/null | grep -qE "st\.d[[:space:]]+\\\$a0, \\\$sp, $(printf '%d' "$off")\$"; then @@ -147,7 +147,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # ...and PowerPC, whose frame base is r1: p arrives in r3 o="$out/powerpc-none-eabi.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$OBJDUMP" -d "$o" 2>/dev/null | grep -qE "stw[[:space:]]+3,[[:space:]]*$(printf '%d' "$off")\(1\)"; then @@ -160,7 +160,7 @@ if command -v "$OBJDUMP" >/dev/null 2>&1; then # or not: p arrives in %i0 and the prologue stores it at %fp - n o="$out/sparc-none-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) # (LLVM 18's llvm-objdump prints the 13-bit offset unsigned: %fp+8172 # for %fp+-0x14) @@ -179,7 +179,7 @@ XOBJDUMP=$XTENSA_REF_DIR/bin/xtensa-esp-elf-objdump if [ -x "$XOBJDUMP" ]; then o="$out/xtensa-none-elf.o" off=$("$DWDUMP" --debug-info "$o" 2>/dev/null | - grep -A2 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | + grep -A4 'DW_AT_name ("p")' | grep -oE 'fbreg [+-][0-9]+' | grep -oE '[+-][0-9]+' | head -1) if [ -n "$off" ] && "$XOBJDUMP" -d "$o" 2>/dev/null | grep -qE "s32i(\.n)?[[:space:]]+a2, a1, $(printf '%d' "$off")\$"; then @@ -377,7 +377,7 @@ for t in thumbv7m-none-eabi riscv32-unknown-elf mipsel-none-elf \ grep -q "DW_AT_frame_base.*DW_OP_$dw" "$out/al-$t.dw" || { echo "FAIL $t: an alloca function's frame base is not $reg:" grep frame_base "$out/al-$t.dw"; fail=1; continue; } - off=$(grep -A3 'DW_AT_name.*"n"' "$out/al-$t.dw" | + off=$(grep -A5 'DW_AT_name.*"n"' "$out/al-$t.dw" | sed -n 's/.*DW_OP_fbreg +\([0-9]*\).*/\1/p' | head -1) hex=$(printf '0x%x' "$off") # (a zero offset disassembles as `[r7]` and `0(s0)`; LoongArch's is @@ -400,7 +400,7 @@ done t=powerpc-none-eabi if "$EMBCC" --target=$t -g -O0 -c "$out/al.c" -o "$out/al-$t.o" 2>/dev/null; then "$DWDUMP" --debug-info "$out/al-$t.o" > "$out/al-$t.dw" 2>/dev/null - off=$(grep -A3 'DW_AT_name.*"n"' "$out/al-$t.dw" | + off=$(grep -A5 'DW_AT_name.*"n"' "$out/al-$t.dw" | sed -n 's/.*DW_OP_fbreg +\([0-9]*\).*/\1/p' | head -1) grep -q "DW_AT_frame_base.*DW_OP_breg31" "$out/al-$t.dw" && "$OBJDUMP" -d "$out/al-$t.o" 2>/dev/null | diff --git a/tests/golden/debug-live.sh b/tests/golden/debug-live.sh index 6f8aee31..08dd00e4 100644 --- a/tests/golden/debug-live.sh +++ b/tests/golden/debug-live.sh @@ -70,6 +70,11 @@ if [ "$ARCH" = aarch64 ]; then image="$out/live.elf"; symbols="$out/live.elf" qargs="-M virt -cpu cortex-a72 -semihosting -nographic -kernel $image" else + # the QEMU harness links with newlib, whatever runs the corpus (a CI + # runner has no newlib; until it had gdb, this skipped for that) + [ -d "$X86_NEWLIB/lib" ] || { + echo "skipped: no x86_64 newlib at $X86_NEWLIB for the QEMU harness (set EMBCC_X86_NEWLIB)" + exit 0; } EMBCC_HARNESS_KEEP64=1 sh "$EMBCC_ROOT/tests/harness/x86_64/link.sh" \ -o "$out/live.elf" "$out/live.o" || { echo "harness link failed"; exit 1; } # QEMU boots the ELF32 Multiboot relabel; gdb reads the 64-bit original diff --git a/tests/golden/debug-optimized.sh b/tests/golden/debug-optimized.sh new file mode 100644 index 00000000..e462d69a --- /dev/null +++ b/tests/golden/debug-optimized.sh @@ -0,0 +1,102 @@ +#!/bin/sh +# -g at -O2 and -Os, proven by DEBUGGING against a reference: the same +# program, built by EmbCC at -O0, -O2 and -Os and by clang at -O0, runs on +# QEMU's boards under gdb, which stops on every line and reads every +# parameter and local in scope (debug-optimized/trace.py). Every value +# EmbCC's DWARF gives must be the one clang's -O0 build shows at the same +# stop -- the same call of each function on the stack, the same line, the +# same visit of it within that call (debug-optimized/cmp.py). A call +# EmbCC inlined is a frame of its own to gdb (DW_TAG_inlined_subroutine), +# so its stops pair with clang's real call. A location list that +# names the wrong register, a value that went stale, or a variable shown +# outside its block -- the second of two `for (int i ...)` loops showing +# the first's i, a block's `s` hiding the function's own after the block +# -- is a failure. What the optimized code does not keep is and counts as unavailable, not wrong; the share available is +# printed. A local is not compared up to its declaration's line (the +# DWARF scope is its block, C's starts at the declarator), and a line +# visited a different number of times within one call (a rotated loop's +# test, which the optimized line table gives several addresses) is not +# compared in that call. +# +# clang's -O0 is the reference because a build of EmbCC's own can be +# wrong the same way at every level: before lexical blocks, -O0 and -O2 +# both showed the first loop's i in the second loop, and agreed. +# +# Skips honestly without a gdb that has Python and the board's +# architecture (gdb-multiarch), clang, or the board's QEMU. +set -u +echo "TEST-MARKER debug-optimized" +. "$(dirname "$0")/../lib.sh" + +EMBCC=${EMBCC:-./embcc} +here=$EMBCC_ROOT/tests/golden/debug-optimized +out=$EMBCC_ROOT/tests/golden/out/debug-optimized +rm -rf "$out"; mkdir -p "$out" +GDB=${EMBCC_GDB:-} +if [ -z "$GDB" ]; then + for g in gdb-multiarch gdb; do + command -v $g >/dev/null 2>&1 && + $g -batch -nx -ex "python import gdb" -ex "set architecture armv7e-m" \ + >/dev/null 2>&1 && { GDB=$g; break; } + done +fi +[ -n "$GDB" ] || { echo "skipped: no gdb with Python and ARM (gdb-multiarch)"; exit 0; } +command -v clang >/dev/null 2>&1 || { echo "skipped: no clang for the reference"; exit 0; } + +fail=0 ran=0 +board() { # NAME TRIPLE HARNESS HARNESS-VAR GDB-ARCH "CLANG-FLAGS" QEMU... + b=$1 X=$2 H=$3 HV=$4 A=$5 CF=$6; shift 6 + command -v "$1" >/dev/null 2>&1 || { echo " $b: skipped ($1 absent)"; return; } + $GDB -batch -nx -ex "set architecture $A" >/dev/null 2>&1 || + { echo " $b: skipped ($GDB has no $A)"; return; } + d=$out/$b; mkdir -p "$d" + for f in boot io; do + "$EMBCC" --target=$X -c tests/harness/$H/$f.c -o "$d/$f.o" || { fail=1; return; } + done + for o in clang -O0 -O2 -Os; do + if [ $o = clang ]; then + # shellcheck disable=SC2086 + clang --target=$X $CF -O0 -g -ffreestanding -c "$here/prog.c" -o "$d/prog$o.o" + else + "$EMBCC" --target=$X $o -g -c "$here/prog.c" -o "$d/prog$o.o" + fi || { echo " $b $o: does not compile"; fail=1; continue; } + env $HV="$d" sh tests/harness/$H/link.sh "$d/prog$o.elf" "$d/prog$o.o" \ + build/libc/$X/librt.a > "$d/$o.ld" 2>&1 || + { echo " $b $o: does not link"; head -3 "$d/$o.ld"; fail=1; continue; } + port=$((20000 + ($$ * 7 + ran) % 20000)) + "$@" -nographic -kernel "$d/prog$o.elf" -S -gdb tcp::$port > "$d/$o.qemu" 2>&1 & + q=$! + sleep 1 + TRACE_PORT=$port TRACE_SRC=prog.c TRACE_LINES="1-$(wc -l < "$here/prog.c")" \ + TRACE_OUT="$d/$o.jsonl" TRACE_MAX=3000 \ + $GDB -batch -nx -ex "set architecture $A" -ex "file $d/prog$o.elf" \ + -x "$here/trace.py" > "$d/$o.gdb" 2>&1 + kill $q 2>/dev/null; wait $q 2>/dev/null + ran=$((ran + 1)) + done + for o in -O0 -O2 -Os; do + [ -s "$d/clang.jsonl" ] && [ -s "$d/$o.jsonl" ] || + { echo " $b $o: no trace"; fail=1; continue; } + r=$(python3 "$here/cmp.py" "$d/clang.jsonl" "$d/$o.jsonl") + echo " $b $o: $(echo "$r" | head -1)" + case $(echo "$r" | head -1) in + *", 0 DISAGREE") ;; + *) echo "$r" | sed -n '2,12p'; fail=1 ;; + esac + done +} +board m3 thumbv7m-none-eabi thumb EMBCC_THUMB_HARNESS armv7e-m \ + "-mcpu=cortex-m3 -mfloat-abi=soft" qemu-system-arm -M lm3s6965evb -cpu cortex-m3 +board m0 thumbv6m-none-eabi thumb-m0 EMBCC_THUMB_M0_HARNESS armv6-m \ + "-mcpu=cortex-m0 -mfloat-abi=soft" qemu-system-arm -M microbit +board rv32 riscv32-unknown-elf riscv EMBCC_RISCV_HARNESS riscv:rv32 \ + "-march=rv32imac -mabi=ilp32 -mno-relax" qemu-system-riscv32 -M virt -bios none -m 8 +# clang's reference for MIPS is DWARF 4: gdb 17 maps no address of a +# linked MIPS image whose unit's start is a DWARF 5 .debug_addr entry +# (DW_FORM_addrx, which it does not sign-extend as it does the line +# table's KSEG0 addresses), so clang's DWARF 5 image has no lines to it +board mips mipsel-none-elf mips EMBCC_MIPS_HARNESS mips:isa32r2 \ + "-mcpu=mips32r2 -msoft-float -gdwarf-4" qemu-system-mipsel -M malta -cpu 24Kc +[ $fail = 0 ] || { echo "FAIL: debug-optimized"; exit 1; } +echo "debug-optimized: every value EmbCC's DWARF gives at -O0, -O2 and -Os is clang's -O0 build's" diff --git a/tests/golden/debug-optimized/cmp.py b/tests/golden/debug-optimized/cmp.py new file mode 100644 index 00000000..b702fb92 --- /dev/null +++ b/tests/golden/debug-optimized/cmp.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +"""cmp.py REF.jsonl GOT.jsonl -- every value GOT's debug info gives must be REF's at the same +stop. A stop is named by its invocation -- which call of each function on its stack -- its line, +and which visit of that line within the innermost call. A line visited a different number of +times within one call in the two builds (a rotated loop's test) is not compared in that call. + +A stop inside a call GOT inlined (gdb's inline frame) cannot be told apart from the next call +of the same function when no stop comes between: there the value must be one REF shows for that +variable at that line in some call made from the same caller invocation.""" +import json, sys +from collections import Counter + +def load(p): + rows = [json.loads(l) for l in open(p)] + out = {}; calls = {}; prev = []; visits = {}; path = []; groups = {} + for r in rows: + st = r.get("stack") or [r["key"].split(":")[0]] + n = 0 + while n < len(prev) and n < len(st) and prev[n] == st[n] and n < len(path): + n += 1 + path = path[:n] + for d in range(n, len(st)): + parent = tuple(path) + c = calls.get((parent, st[d]), 0) + calls[(parent, st[d])] = c + 1 + path.append((st[d], c)) + prev = st + inv = tuple(path) + vk = (inv, r["line"]) + v = visits.get(vk, 0); visits[vk] = v + 1 + r["_inv"] = inv + out[(inv, r["line"], v)] = r + # every level's caller: (the calls above it, the functions from it down, the line) + for d in range(len(st)): + g = groups.setdefault((tuple(path[:d]), tuple(st[d:]), r["line"]), {}) + for name, val in r["vals"].items(): + g.setdefault(name, set()).add(val) + return out, visits, groups + +a, va, ga = load(sys.argv[1]); b, vb, gb = load(sys.argv[2]) +total = avail = agree = skipped = 0; bad = [] +for key, j in b.items(): + inv, line, v = key + flags = j.get("inl") or [] + first = next((d for d, x in enumerate(flags) if x), -1) + if first >= 0: + st = j.get("stack") + refset = ga.get((tuple(inv[:first]), tuple(st[first:]), line)) + ref = None + if refset is None: + skipped += 1; continue + else: + if va.get((inv, line)) != vb.get((inv, line)): + skipped += 1; continue + ref = a.get(key) + if ref is None: + skipped += 1; continue + for name, s in j["vals"].items(): + decl = (ref or {}).get("decl", {}).get(name) or j.get("decl", {}).get(name) or 0 + if decl and line <= decl: + continue + # where the reference's own gdb cannot read it (an older gdb, at + # clang's epilogue), there is nothing to compare with + if ref is not None and str(ref["vals"].get(name, "")).startswith("ERR"): + continue + total += 1 + if s is None: continue + avail += 1 + if ref is None: + ok = s in refset.get(name, set()) + r = "one of " + ",".join(sorted(str(x) for x in refset.get(name, set())))[:60] + else: + r = ref["vals"].get(name); ok = r == s + if ok: agree += 1 + else: bad.append((j["key"], name, s, r)) +print("%d stops, %d compared, %d not (visited differently within a call); %d values, %d available (%.0f%%), %d agree, %d DISAGREE" + % (len(b), len(b) - skipped, skipped, total, avail, 100.0 * avail / total if total else 0, agree, len(bad))) +c = Counter((x[0], x[1]) for x in bad) +for (key, v), n in sorted(c.items()): + ex = [x for x in bad if x[0] == key and x[1] == v][0] + print(" %-14s %-6s x%-3d e.g. got %s, reference %s" % (key, v, n, ex[2], ex[3])) diff --git a/tests/golden/debug-optimized/prog.c b/tests/golden/debug-optimized/prog.c new file mode 100644 index 00000000..e1524b1f --- /dev/null +++ b/tests/golden/debug-optimized/prog.c @@ -0,0 +1,107 @@ +void puts_(const char *s); +void putn(long v); + +struct acc { int sum; int n; unsigned max; }; + +__attribute__((noinline)) static unsigned crc8(const unsigned char *p, int len) +{ + unsigned crc = 0xff; + for (int i = 0; i < len; i++) { + unsigned b = p[i]; + crc ^= b; + for (int k = 0; k < 8; k++) { + unsigned top = crc & 0x80; + crc = (crc << 1) & 0xff; + if (top) + crc ^= 0x31; + } + } + return crc; +} + +__attribute__((noinline)) static void add(struct acc *a, int v) +{ + int old = a->sum; + a->sum = old + v; + a->n++; + if ((unsigned)v > a->max) + a->max = (unsigned)v; +} + +__attribute__((noinline)) static long long mix(long long x, int s, short t) +{ + long long y = x * 3 + s; + int z = t * 7 - s; + y ^= (long long)z << 5; + return y + z; +} + +__attribute__((noinline)) static int search(const int *v, int n, int want) +{ + int lo = 0, hi = n - 1; + while (lo <= hi) { + int mid = (lo + hi) / 2; + int got = v[mid]; + if (got == want) + return mid; + if (got < want) + lo = mid + 1; + else + hi = mid - 1; + } + return -1; +} + +/* names declared again in sibling and inner blocks */ +__attribute__((noinline)) static int shadow(int n) +{ + int s = 0; + for (int i = 0; i < n; i++) + s += i * 3; + for (int i = n; i > 0; i--) { + int t = i + s; + s ^= t; + } + { + int s = n * 11; + n += s; + } + return s + n; +} + +/* small enough to inline at -O2: its calls are inlined frames */ +static int clamp(int v, int lo, int hi) +{ + int r = v; + if (r < lo) + r = lo; + if (r > hi) + r = hi; + return r; +} + +static const unsigned char msg[] = "hello, location lists"; +static const int sorted[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + +int main(void) +{ + struct acc a = { 0, 0, 0 }; + unsigned c = crc8(msg, 6); /* (each byte is 8 rounds of stops) */ + for (int i = 0; i < 5; i++) + add(&a, (int)c - i * 40); + long long m = mix(0x123456789LL, a.sum, (short)a.n); + int where = search(sorted, 11, 19); + int none = search(sorted, 11, 4); + int sh = shadow(6); + int cl = 0; + for (int j = -3; j < 9; j += 2) + cl += clamp(j * (int)(c % 7), 0, 4); + putn((long)c); + putn(a.sum); + putn((long)(m & 0xffff)); + putn(where + none); + putn(sh); + putn(cl); + puts_("\n==END==\n"); + return 0; +} diff --git a/tests/golden/debug-optimized/trace.py b/tests/golden/debug-optimized/trace.py new file mode 100644 index 00000000..5e54752b --- /dev/null +++ b/tests/golden/debug-optimized/trace.py @@ -0,0 +1,98 @@ +# gdb -batch -x trace.py, with TRACE_PORT, TRACE_SRC (a basename), TRACE_LINES +# ("12,13,20-31"), TRACE_OUT and TRACE_MAX in the environment: connect to the +# stub, break on each of those lines of the source, and at every stop record +# the function, the line, which visit of that line it is, and every parameter +# and local in scope -- its value as gdb prints it, or None when gdb says it is +# optimized out. Written as JSON lines to TRACE_OUT. +import gdb, json, os + +port = os.environ["TRACE_PORT"] +src = os.environ["TRACE_SRC"] +mx = int(os.environ.get("TRACE_MAX", "4000")) +lines = [] +for part in os.environ["TRACE_LINES"].split(","): + if "-" in part: + a, b = part.split("-") + lines += range(int(a), int(b) + 1) + elif part: + lines.append(int(part)) + +gdb.execute("set pagination off") +gdb.execute("set confirm off") +gdb.execute("set print pretty off") +gdb.execute("set print address on") +gdb.execute("target remote :" + port) +for l in lines: + try: + gdb.execute("break %s:%d" % (src, l), to_string=True) + except gdb.error: + pass +# where a harness goes once main returns: the trace ends there (a board that +# then waits forever would never stop again) +for f in ("_exit", "exit", "abort"): + try: + gdb.execute("break " + f, to_string=True) + except gdb.error: + pass + +out = open(os.environ["TRACE_OUT"], "w") +seen = {} +for n in range(mx): + try: + gdb.execute("continue", to_string=True) + except gdb.error: + break + try: + f = gdb.selected_frame() + except gdb.error: + break + sal = f.find_sal() + if not sal.symtab or os.path.basename(sal.symtab.filename) != src: + break + fn = f.name() + key = "%s:%d" % (fn, sal.line) + k = seen.get(key, 0) + seen[key] = k + 1 + vals = {} + decl = {} + try: + blk = f.block() + except RuntimeError: + blk = None + while blk is not None: + for sym in blk: + if not (sym.is_variable or sym.is_argument) or sym.name in vals: + continue + decl[sym.name] = 0 if sym.is_argument else sym.line + try: + v = sym.value(f) + if v.is_optimized_out: + vals[sym.name] = None + else: + ptr = v.type.strip_typedefs().code == gdb.TYPE_CODE_PTR + s = str(v) + if ptr: # stack addresses differ between builds: null or not + s = "ptr:" + ("null" if int(v) == 0 else "set") + vals[sym.name] = s + except Exception as e: + vals[sym.name] = "ERR " + str(e)[:60] + if blk.function is not None: + break + blk = blk.superblock + # the call stack, outermost first: which invocation a stop is in + stack = [] + inl = [] + g = f + while g is not None and len(stack) < 16: + stack.append(g.name() or "?") + inl.append(g.type() == gdb.INLINE_FRAME) + try: + g = g.older() + except gdb.error: + break + stack.reverse() + inl.reverse() + out.write(json.dumps({"key": key, "line": sal.line, "k": k, "pc": int(f.pc()), + "vals": vals, "decl": decl, "stack": stack, + "inl": inl}) + "\n") +out.close() diff --git a/tests/golden/embdbg-remote.sh b/tests/golden/embdbg-remote.sh index b641fb4a..c6b8f5b0 100755 --- a/tests/golden/embdbg-remote.sh +++ b/tests/golden/embdbg-remote.sh @@ -191,7 +191,7 @@ CEOF if command -v llvm-dwarfdump >/dev/null 2>&1 && command -v llvm-nm >/dev/null 2>&1; then sym=$(llvm-nm "$d/fwa.elf" | awk '$3 == "acc" { print $1 }') dw=$(llvm-dwarfdump --debug-info "$d/fwa.elf" | - grep -A3 'DW_AT_name ("acc")' | sed -n 's/.*DW_OP_addr 0x\([0-9a-f]*\).*/\1/p') + grep -A5 'DW_AT_name ("acc")' | sed -n 's/.*DW_OP_addr 0x\([0-9a-f]*\).*/\1/p') [ -n "$sym" ] && [ -n "$dw" ] && [ $((0x$dw)) -eq $((0x800000 + 0x$sym)) ] || { printf "avr: acc's DW_OP_addr is 0x%s; it is at 0x%x in SRAM, so 0x%x\n" "$dw" $((0x$sym)) $((0x800000 + 0x$sym)) diff --git a/tests/golden/thumb-codegen.sh b/tests/golden/thumb-codegen.sh index e759f3db..176d402e 100755 --- a/tests/golden/thumb-codegen.sh +++ b/tests/golden/thumb-codegen.sh @@ -323,7 +323,7 @@ if command -v llvm-dwarfdump > /dev/null 2>&1; then llvm-dwarfdump "$out/g.o" > "$out/g.dw" 2>&1 for v in a b s; do # (a location list's entries are on the lines after it) - loc=$(grep -A4 "DW_AT_name (\"$v\")" "$out/g.dw" | + loc=$(grep -A6 "DW_AT_name (\"$v\")" "$out/g.dw" | sed -n '/DW_AT_location/,$p' | tr -s ' \n' ' ') case $o,$loc in -Og,*DW_OP_fbreg*) ;;