GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — async-profiler/src/dwarf.cpp at jonoffcpu-dev · jonoffcpu/async-profiler Expand file tree/ Copy pathdwarf.cppMore file actions 418 lines (377 loc) · 13.9 KB / Copy pathdwarf.cppFile metadata and controls418 lines (377 loc) · 13.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 /* * Copyright The async-profiler authors * SPDX-License-Identifier: Apache-2.0 */ #include <stdlib.h> #include "dwarf.h" #include "log.h" enum { DW_CFA_nop = 0x0, DW_CFA_set_loc = 0x1, DW_CFA_advance_loc1 = 0x2, DW_CFA_advance_loc2 = 0x3, DW_CFA_advance_loc4 = 0x4, DW_CFA_offset_extended = 0x5, DW_CFA_restore_extended = 0x6, DW_CFA_undefined = 0x7, DW_CFA_same_value = 0x8, DW_CFA_register = 0x9, DW_CFA_remember_state = 0xa, DW_CFA_restore_state = 0xb, DW_CFA_def_cfa = 0xc, DW_CFA_def_cfa_register = 0xd, DW_CFA_def_cfa_offset = 0xe, DW_CFA_def_cfa_expression = 0xf, DW_CFA_expression = 0x10, DW_CFA_offset_extended_sf = 0x11, DW_CFA_def_cfa_sf = 0x12, DW_CFA_def_cfa_offset_sf = 0x13, DW_CFA_val_offset = 0x14, DW_CFA_val_offset_sf = 0x15, DW_CFA_val_expression = 0x16, DW_CFA_AARCH64_negate_ra_state = 0x2d, DW_CFA_GNU_args_size = 0x2e, DW_CFA_advance_loc = 0x1, DW_CFA_offset = 0x2, DW_CFA_restore = 0x3, }; enum { DW_OP_breg_pc = 0x70 + DW_REG_PC, DW_OP_const1u = 0x08, DW_OP_const1s = 0x09, DW_OP_const2u = 0x0a, DW_OP_const2s = 0x0b, DW_OP_const4u = 0x0c, DW_OP_const4s = 0x0d, DW_OP_constu = 0x10, DW_OP_consts = 0x11, DW_OP_minus = 0x1c, DW_OP_plus = 0x22, }; FrameDesc FrameDesc::empty_frame = {0, DW_REG_SP | EMPTY_FRAME_SIZE << 8, DW_SAME_FP, INITIAL_PC_OFFSET}; FrameDesc FrameDesc::default_frame = {0, DW_REG_FP | LINKED_FRAME_SIZE << 8, -LINKED_FRAME_SIZE, -LINKED_FRAME_SIZE + DW_STACK_SLOT}; DwarfParser::DwarfParser(const char* name, const char* image_base) { _name = name; _image_base = image_base; _capacity = 128; _count = 0; _table = (FrameDesc*)malloc(_capacity * sizeof(FrameDesc)); _prev = nullptr; _last_cie = (u64)-1; _code_align = sizeof(instruction_t); _data_align = -(int)sizeof(void*); } void DwarfParser::parseEhFrame(const char* eh_frame_hdr) { u8 version = eh_frame_hdr[0]; u8 eh_frame_ptr_enc = eh_frame_hdr[1]; u8 fde_count_enc = eh_frame_hdr[2]; u8 table_enc = eh_frame_hdr[3]; if (version != 1 || (eh_frame_ptr_enc & 0x7) != 0x3 || (fde_count_enc & 0x7) != 0x3 || (table_enc & 0xf7) != 0x33) { Log::warn("Unsupported .eh_frame_hdr [%02x%02x%02x%02x] in %s", version, eh_frame_ptr_enc, fde_count_enc, table_enc, _name); return; } int fde_count = *(int*)(eh_frame_hdr + 8); int* table = (int*)(eh_frame_hdr + 16); for (int i = 0; i < fde_count; i++) { _ptr = eh_frame_hdr + table[i * 2]; parseFde(); } } void DwarfParser::parseCie() { u32 cie_len = get32(); if (cie_len == 0 || cie_len == 0xffffffff) { return; } _ptr += 5; while (*_ptr++) {} _code_align = getLeb(); _data_align = getSLeb(); } void DwarfParser::parseFde() { u32 fde_len = get32(); if (fde_len == 0 || fde_len == 0xffffffff) { return; } const char* fde_start = _ptr; u32 cie_offset = get32(); if (_count == 0) { _ptr = fde_start - cie_offset; parseCie(); _ptr = fde_start + 4; } u32 range_start = getRelAddr() - _image_base; u32 range_len = get32(); _ptr += getLeb(); parseInstructions(range_start, fde_start + fde_len); addRecord(range_start + range_len, DW_REG_FP, LINKED_FRAME_SIZE, -LINKED_FRAME_SIZE, -LINKED_FRAME_SIZE + DW_STACK_SLOT); } // Unlike .eh_frame, .debug_frame has no sorted index void DwarfParser::parseDebugFrame(const char* debug_frame_start, size_t size) { _ptr = debug_frame_start; const char* debug_frame_end = debug_frame_start + size; while (_ptr < debug_frame_end) { _ptr = parseDebugFde(debug_frame_start, debug_frame_end); } qsort(_table, _count, sizeof(FrameDesc), FrameDesc::comparator); } // CIE in .debug_frame differs from .eh_frame: it has no augmentation data, // and DWARF v4 adds address_size and segment_selector_size after the augmentation string. void DwarfParser::parseDebugCie() { u32 cie_len = get32(); bool dwarf64 = cie_len == 0xffffffff; _ptr += dwarf64 ? 16 : 4; // 8-byte CIE_length + 8-byte CIE_id or 4-byte CIE_id u8 version = get8(); while (*_ptr++) {} // augmentation string if (version >= 4) { _ptr += 2; // address_size + segment_selector_size } _code_align = getLeb(); _data_align = getSLeb(); } const char* DwarfParser::parseDebugFde(const char* debug_frame_start, const char* debug_frame_end) { u64 fde_len = get32(); bool dwarf64 = fde_len == 0xffffffff; if (dwarf64) { fde_len = get64(); } const char* fde_end = _ptr + fde_len; if (fde_end <= _ptr || fde_end > debug_frame_end) { return debug_frame_end; // stop parsing the whole section } u64 cie_ptr; if (dwarf64) { if ((cie_ptr = get64()) == 0xffffffffffffffffULL) return fde_end; } else { if ((cie_ptr = get32()) == 0xffffffff) return fde_end; } // Parse the linked CIE only if it is different from the previously parsed one if (cie_ptr != _last_cie) { const char* fde_start = _ptr; _ptr = debug_frame_start + cie_ptr; parseDebugCie(); _ptr = fde_start; _last_cie = cie_ptr; } u32 range_start = getAddr() - _image_base; if (range_start != 0) { // zero-address FDEs are leftovers from garbage‑collected functions getAddr(); // skip range_len parseInstructions(range_start, fde_end); } return fde_end; } void DwarfParser::parseInstructions(u32 loc, const char* end) { const u32 code_align = _code_align; const int data_align = _data_align; u32 cfa_reg = DW_REG_SP; int cfa_off = EMPTY_FRAME_SIZE; int fp_off = DW_SAME_FP; int pc_off = INITIAL_PC_OFFSET; u32 rem_cfa_reg = DW_REG_SP; int rem_cfa_off = EMPTY_FRAME_SIZE; int rem_fp_off = DW_SAME_FP; int rem_pc_off = INITIAL_PC_OFFSET; while (_ptr < end) { u8 op = get8(); switch (op >> 6) { case 0: switch (op) { case DW_CFA_nop: case DW_CFA_set_loc: _ptr = end; break; case DW_CFA_advance_loc1: addRecord(loc, cfa_reg, cfa_off, fp_off, pc_off); loc += get8() * code_align; break; case DW_CFA_advance_loc2: addRecord(loc, cfa_reg, cfa_off, fp_off, pc_off); loc += get16() * code_align; break; case DW_CFA_advance_loc4: addRecord(loc, cfa_reg, cfa_off, fp_off, pc_off); loc += get32() * code_align; break; case DW_CFA_offset_extended: switch (getLeb()) { case DW_REG_FP: fp_off = getLeb() * data_align; break; case DW_REG_PC: pc_off = getLeb() * data_align; break; default: skipLeb(); } break; case DW_CFA_restore_extended: case DW_CFA_undefined: case DW_CFA_same_value: if (getLeb() == DW_REG_FP) { fp_off = DW_SAME_FP; } break; case DW_CFA_register: skipLeb(); skipLeb(); break; case DW_CFA_remember_state: rem_cfa_reg = cfa_reg; rem_cfa_off = cfa_off; rem_fp_off = fp_off; rem_pc_off = pc_off; break; case DW_CFA_restore_state: cfa_reg = rem_cfa_reg; cfa_off = rem_cfa_off; fp_off = rem_fp_off; pc_off = rem_pc_off; break; case DW_CFA_def_cfa: cfa_reg = getLeb(); cfa_off = getLeb(); break; case DW_CFA_def_cfa_register: cfa_reg = getLeb(); break; case DW_CFA_def_cfa_offset: cfa_off = getLeb(); break; case DW_CFA_def_cfa_expression: { u32 len = getLeb(); cfa_reg = len == 11 ? DW_REG_PLT : DW_REG_INVALID; cfa_off = DW_STACK_SLOT; _ptr += len; break; } case DW_CFA_expression: skipLeb(); _ptr += getLeb(); break; case DW_CFA_offset_extended_sf: switch (getLeb()) { case DW_REG_FP: fp_off = getSLeb() * data_align; break; case DW_REG_PC: pc_off = getSLeb() * data_align; break; default: skipLeb(); } break; case DW_CFA_def_cfa_sf: cfa_reg = getLeb(); cfa_off = getSLeb() * data_align; break; case DW_CFA_def_cfa_offset_sf: cfa_off = getSLeb() * data_align; break; case DW_CFA_val_offset: case DW_CFA_val_offset_sf: skipLeb(); skipLeb(); break; case DW_CFA_val_expression: if (getLeb() == DW_REG_PC) { int pc_off = parseExpression(); if (pc_off != 0) { fp_off = DW_PC_OFFSET | (pc_off << 1); } } else { _ptr += getLeb(); } break; #ifdef __aarch64__ case DW_CFA_AARCH64_negate_ra_state: break; #endif case DW_CFA_GNU_args_size: skipLeb(); break; default: Log::warn("Unknown DWARF instruction 0x%x in %s", op, _name); return; } break; case DW_CFA_advance_loc: addRecord(loc, cfa_reg, cfa_off, fp_off, pc_off); loc += (op & 0x3f) * code_align; break; case DW_CFA_offset: switch (op & 0x3f) { case DW_REG_FP: fp_off = getLeb() * data_align; break; case DW_REG_PC: pc_off = getLeb() * data_align; break; default: skipLeb(); } break; case DW_CFA_restore: if ((op & 0x3f) == DW_REG_FP) { fp_off = DW_SAME_FP; } else if ((op & 0x3f) == DW_REG_PC) { pc_off = INITIAL_PC_OFFSET; } break; } } addRecord(loc, cfa_reg, cfa_off, fp_off, pc_off); } // Parse a limited subset of DWARF expressions, which is used in DW_CFA_val_expression // to point to the previous PC relative to the current PC. // Returns the offset of the previous PC from the current PC. int DwarfParser::parseExpression() { int pc_off = 0; int tos = 0; u32 len = getLeb(); const char* end = _ptr + len; while (_ptr < end) { u8 op = get8(); switch (op) { case DW_OP_breg_pc: pc_off = getSLeb(); break; case DW_OP_const1u: tos = get8(); break; case DW_OP_const1s: tos = (signed char)get8(); break; case DW_OP_const2u: tos = get16(); break; case DW_OP_const2s: tos = (short)get16(); break; case DW_OP_const4u: case DW_OP_const4s: tos = get32(); break; case DW_OP_constu: tos = getLeb(); break; case DW_OP_consts: tos = getSLeb(); break; case DW_OP_minus: pc_off -= tos; break; case DW_OP_plus: pc_off += tos; break; default: Log::warn("Unknown DWARF opcode 0x%x in %s", op, _name); _ptr = end; return 0; } } return pc_off; } void DwarfParser::addRecord(u32 loc, u32 cfa_reg, int cfa_off, int fp_off, int pc_off) { int cfa = cfa_reg | cfa_off << 8; if (_prev == nullptr || (_prev->loc == loc && --_count >= 0) || _prev->cfa != cfa || _prev->fp_off != fp_off || _prev->pc_off != pc_off) { _prev = addRecordRaw(loc, cfa, fp_off, pc_off); } } FrameDesc* DwarfParser::addRecordRaw(u32 loc, int cfa, int fp_off, int pc_off) { if (_count >= _capacity) { _capacity *= 2; _table = (FrameDesc*)realloc(_table, _capacity * sizeof(FrameDesc)); } FrameDesc* f = &_table[_count++]; f->loc = loc; f->cfa = cfa; f->fp_off = fp_off; f->pc_off = pc_off; return f; } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |