lnfodwrf.pp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Thomas Schatzl, member of the FreePascal
  4. Development team
  5. Parts (c) 2000 Peter Vreman (adapted from original dwarfs line
  6. reader)
  7. Dwarf LineInfo Retriever
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. {
  15. This unit should not be compiled in objfpc mode, since this would make it
  16. dependent on objpas unit.
  17. }
  18. unit lnfodwrf;
  19. interface
  20. {$S-}
  21. {$IF FPC_VERSION<3}
  22. type
  23. CodePointer = Pointer;
  24. {$ENDIF}
  25. function GetLineInfo(addr:codeptruint;var func,source:string;var line:longint) : boolean;
  26. function DwarfBackTraceStr(addr: CodePointer): string;
  27. procedure CloseDwarf;
  28. var
  29. // Allows more efficient operation by reusing previously loaded debug data
  30. // when the target module filename is the same. However, if an invalid memory
  31. // address is supplied then further calls may result in an undefined behaviour.
  32. // In summary: enable for speed, disable for resilience.
  33. AllowReuseOfLineInfoData: Boolean = True;
  34. implementation
  35. uses
  36. exeinfo;
  37. { Current issues:
  38. - ignores DW_LNS_SET_FILE
  39. }
  40. {$MACRO ON}
  41. { $DEFINE DEBUG_DWARF_PARSER}
  42. {$ifdef DEBUG_DWARF_PARSER}
  43. {$define DEBUG_WRITELN := WriteLn}
  44. {$define DEBUG_COMMENT := }
  45. {$else}
  46. {$define DEBUG_WRITELN := //}
  47. {$define DEBUG_COMMENT := //}
  48. {$endif}
  49. { some type definitions }
  50. type
  51. Bool8 = ByteBool;
  52. {$ifdef CPUI8086}
  53. TOffset = Word;
  54. {$else CPUI8086}
  55. TOffset = PtrUInt;
  56. {$endif CPUI8086}
  57. TSegment = Word;
  58. const
  59. EBUF_SIZE = 100;
  60. {$WARNING This code is not thread-safe, and needs improvement}
  61. var
  62. { the input file to read DWARF debug info from, i.e. paramstr(0) }
  63. e : TExeFile;
  64. EBuf: Array [0..EBUF_SIZE-1] of Byte;
  65. EBufCnt, EBufPos: Integer;
  66. { the offset and size of the DWARF debug_line section in the file }
  67. Dwarf_Debug_Line_Section_Offset,
  68. Dwarf_Debug_Line_Section_Size,
  69. { the offset and size of the DWARF debug_info section in the file }
  70. Dwarf_Debug_Info_Section_Offset,
  71. Dwarf_Debug_Info_Section_Size,
  72. { the offset and size of the DWARF debug_aranges section in the file }
  73. Dwarf_Debug_Aranges_Section_Offset,
  74. Dwarf_Debug_Aranges_Section_Size,
  75. { the offset and size of the DWARF debug_abbrev section in the file }
  76. Dwarf_Debug_Abbrev_Section_Offset,
  77. Dwarf_Debug_Abbrev_Section_Size : longint;
  78. { DWARF 2 default opcodes}
  79. const
  80. { Extended opcodes }
  81. DW_LNE_END_SEQUENCE = 1;
  82. DW_LNE_SET_ADDRESS = 2;
  83. DW_LNE_DEFINE_FILE = 3;
  84. {$ifdef CPUI8086}
  85. { non-standard Open Watcom extension; might conflict with future versions of
  86. the DWARF standard }
  87. DW_LNE_SET_SEGMENT = 4;
  88. {$endif CPUI8086}
  89. { Standard opcodes }
  90. DW_LNS_COPY = 1;
  91. DW_LNS_ADVANCE_PC = 2;
  92. DW_LNS_ADVANCE_LINE = 3;
  93. DW_LNS_SET_FILE = 4;
  94. DW_LNS_SET_COLUMN = 5;
  95. DW_LNS_NEGATE_STMT = 6;
  96. DW_LNS_SET_BASIC_BLOCK = 7;
  97. DW_LNS_CONST_ADD_PC = 8;
  98. DW_LNS_FIXED_ADVANCE_PC = 9;
  99. DW_LNS_SET_PROLOGUE_END = 10;
  100. DW_LNS_SET_EPILOGUE_BEGIN = 11;
  101. DW_LNS_SET_ISA = 12;
  102. DW_FORM_addr = $1;
  103. DW_FORM_block2 = $3;
  104. DW_FORM_block4 = $4;
  105. DW_FORM_data2 = $5;
  106. DW_FORM_data4 = $6;
  107. DW_FORM_data8 = $7;
  108. DW_FORM_string = $8;
  109. DW_FORM_block = $9;
  110. DW_FORM_block1 = $a;
  111. DW_FORM_data1 = $b;
  112. DW_FORM_flag = $c;
  113. DW_FORM_sdata = $d;
  114. DW_FORM_strp = $e;
  115. DW_FORM_udata = $f;
  116. DW_FORM_ref_addr = $10;
  117. DW_FORM_ref1 = $11;
  118. DW_FORM_ref2 = $12;
  119. DW_FORM_ref4 = $13;
  120. DW_FORM_ref8 = $14;
  121. DW_FORM_ref_udata = $15;
  122. DW_FORM_indirect = $16;
  123. DW_FORM_sec_offset = $17;
  124. DW_FORM_exprloc = $18;
  125. DW_FORM_flag_present = $19;
  126. type
  127. { state record for the line info state machine }
  128. TMachineState = record
  129. address : QWord;
  130. segment : TSegment;
  131. file_id : DWord;
  132. line : QWord;
  133. column : DWord;
  134. is_stmt : Boolean;
  135. basic_block : Boolean;
  136. end_sequence : Boolean;
  137. prolouge_end : Boolean;
  138. epilouge_begin : Boolean;
  139. isa : DWord;
  140. append_row : Boolean;
  141. end;
  142. { DWARF line number program header preceding the line number program, 64 bit version }
  143. TLineNumberProgramHeader64 = packed record
  144. magic : DWord;
  145. unit_length : QWord;
  146. version : Word;
  147. length : QWord;
  148. minimum_instruction_length : Byte;
  149. default_is_stmt : Bool8;
  150. line_base : ShortInt;
  151. line_range : Byte;
  152. opcode_base : Byte;
  153. end;
  154. { DWARF line number program header preceding the line number program, 32 bit version }
  155. TLineNumberProgramHeader32 = packed record
  156. unit_length : DWord;
  157. version : Word;
  158. length : DWord;
  159. minimum_instruction_length : Byte;
  160. default_is_stmt : Bool8;
  161. line_base : ShortInt;
  162. line_range : Byte;
  163. opcode_base : Byte;
  164. end;
  165. TDebugInfoProgramHeader64 = packed record
  166. magic : DWord;
  167. unit_length : QWord;
  168. version : Word;
  169. debug_abbrev_offset : QWord;
  170. address_size : Byte;
  171. end;
  172. TDebugInfoProgramHeader32= packed record
  173. unit_length : DWord;
  174. version : Word;
  175. debug_abbrev_offset : DWord;
  176. address_size : Byte;
  177. end;
  178. TDebugArangesHeader64 = packed record
  179. magic : DWord;
  180. unit_length : QWord;
  181. version : Word;
  182. debug_info_offset : QWord;
  183. address_size : Byte;
  184. segment_size : Byte;
  185. {$ifndef CPUI8086}
  186. padding : DWord;
  187. {$endif CPUI8086}
  188. end;
  189. TDebugArangesHeader32= packed record
  190. unit_length : DWord;
  191. version : Word;
  192. debug_info_offset : DWord;
  193. address_size : Byte;
  194. segment_size : Byte;
  195. {$ifndef CPUI8086}
  196. padding : DWord;
  197. {$endif CPUI8086}
  198. end;
  199. {---------------------------------------------------------------------------
  200. I/O utility functions
  201. ---------------------------------------------------------------------------}
  202. type
  203. {$ifdef cpui8086}
  204. TFilePos = LongInt;
  205. {$else cpui8086}
  206. TFilePos = SizeInt;
  207. {$endif cpui8086}
  208. var
  209. base, limit : TFilePos;
  210. index : TFilePos;
  211. baseaddr : {$ifdef cpui8086}farpointer{$else}pointer{$endif};
  212. filename,
  213. dbgfn : string;
  214. lastfilename: string; { store last processed file }
  215. lastopendwarf: Boolean; { store last result of processing a file }
  216. {$ifdef cpui8086}
  217. function tofar(fp: FarPointer): FarPointer; inline;
  218. begin
  219. tofar:=fp;
  220. end;
  221. function tofar(cp: NearCsPointer): FarPointer; inline;
  222. begin
  223. tofar:=Ptr(CSeg,Word(cp));
  224. end;
  225. function tofar(cp: NearPointer): FarPointer; inline;
  226. begin
  227. tofar:=Ptr(DSeg,Word(cp));
  228. end;
  229. {$else cpui8086}
  230. type
  231. tofar=Pointer;
  232. {$endif cpui8086}
  233. function OpenDwarf(addr : codepointer) : boolean;
  234. var
  235. oldprocessaddress: TExeProcessAddress;
  236. begin
  237. // False by default
  238. OpenDwarf:=false;
  239. // Empty so can test if GetModuleByAddr has worked
  240. filename := '';
  241. // Get filename by address using GetModuleByAddr
  242. GetModuleByAddr(tofar(addr),baseaddr,filename);
  243. {$ifdef DEBUG_LINEINFO}
  244. writeln(stderr,filename,' Baseaddr: ',hexstr(baseaddr));
  245. {$endif DEBUG_LINEINFO}
  246. // Check if GetModuleByAddr has worked
  247. if filename = '' then
  248. exit;
  249. // If target filename same as previous, then re-use previous result
  250. if AllowReuseOfLineInfoData and (filename = lastfilename) then
  251. begin
  252. {$ifdef DEBUG_LINEINFO}
  253. writeln(stderr,'Reusing debug data');
  254. {$endif DEBUG_LINEINFO}
  255. OpenDwarf:=lastopendwarf;
  256. exit;
  257. end;
  258. // Close previously opened Dwarf
  259. CloseDwarf;
  260. // Reset last open dwarf result
  261. lastopendwarf := false;
  262. // Save newly processed filename
  263. lastfilename := filename;
  264. // Open exe file or debug link
  265. if not OpenExeFile(e,filename) then
  266. exit;
  267. if ReadDebugLink(e,dbgfn) then
  268. begin
  269. oldprocessaddress:=e.processaddress;
  270. CloseExeFile(e);
  271. if not OpenExeFile(e,dbgfn) then
  272. exit;
  273. e.processaddress:=oldprocessaddress;
  274. end;
  275. // Find debug data section
  276. e.processaddress:=ptruint(baseaddr)-e.processaddress;
  277. if FindExeSection(e,'.debug_line',Dwarf_Debug_Line_Section_offset,dwarf_Debug_Line_Section_size) and
  278. FindExeSection(e,'.debug_info',Dwarf_Debug_Info_Section_offset,dwarf_Debug_Info_Section_size) and
  279. FindExeSection(e,'.debug_abbrev',Dwarf_Debug_Abbrev_Section_offset,dwarf_Debug_Abbrev_Section_size) and
  280. FindExeSection(e,'.debug_aranges',Dwarf_Debug_Aranges_Section_offset,dwarf_Debug_Aranges_Section_size) then
  281. begin
  282. lastopendwarf:=true;
  283. OpenDwarf:=true;
  284. DEBUG_WRITELN('.debug_line starts at offset $',hexstr(Dwarf_Debug_Line_Section_offset,8),' with a size of ',Dwarf_Debug_Line_Section_Size,' Bytes');
  285. DEBUG_WRITELN('.debug_info starts at offset $',hexstr(Dwarf_Debug_Info_Section_offset,8),' with a size of ',Dwarf_Debug_Info_Section_Size,' Bytes');
  286. DEBUG_WRITELN('.debug_abbrev starts at offset $',hexstr(Dwarf_Debug_Abbrev_Section_offset,8),' with a size of ',Dwarf_Debug_Abbrev_Section_Size,' Bytes');
  287. DEBUG_WRITELN('.debug_aranges starts at offset $',hexstr(Dwarf_Debug_Aranges_Section_offset,8),' with a size of ',Dwarf_Debug_Aranges_Section_Size,' Bytes');
  288. end
  289. else
  290. CloseExeFile(e);
  291. end;
  292. procedure CloseDwarf;
  293. begin
  294. if e.isopen then
  295. CloseExeFile(e);
  296. // Reset last processed filename
  297. lastfilename := '';
  298. end;
  299. function Init(aBase, aLimit : Int64) : Boolean;
  300. begin
  301. base := aBase;
  302. limit := aLimit;
  303. Init := (aBase + limit) <= e.size;
  304. seek(e.f, base);
  305. EBufCnt := 0;
  306. EBufPos := 0;
  307. index := 0;
  308. end;
  309. function Init(aBase : Int64) : Boolean;
  310. begin
  311. Init := Init(aBase, limit - (aBase - base));
  312. end;
  313. function Pos() : TFilePos;
  314. begin
  315. Pos := index;
  316. end;
  317. procedure Seek(const newIndex : Int64);
  318. begin
  319. index := newIndex;
  320. system.seek(e.f, base + index);
  321. EBufCnt := 0;
  322. EBufPos := 0;
  323. end;
  324. { Returns the next Byte from the input stream, or -1 if there has been
  325. an error }
  326. function ReadNext() : Longint; inline;
  327. var
  328. bytesread : SizeInt;
  329. begin
  330. ReadNext := -1;
  331. if EBufPos >= EBufCnt then begin
  332. EBufPos := 0;
  333. EBufCnt := EBUF_SIZE;
  334. if EBufCnt > limit - index then
  335. EBufCnt := limit - index;
  336. blockread(e.f, EBuf, EBufCnt, bytesread);
  337. EBufCnt := bytesread;
  338. end;
  339. if EBufPos < EBufCnt then begin
  340. ReadNext := EBuf[EBufPos];
  341. inc(EBufPos);
  342. inc(index);
  343. end
  344. else
  345. ReadNext := -1;
  346. end;
  347. { Reads the next size bytes into dest. Returns true if successful,
  348. false otherwise. Note that dest may be partially overwritten after
  349. returning false. }
  350. function ReadNext(var dest; size : SizeInt) : Boolean;
  351. var
  352. bytesread, totalread : SizeInt;
  353. r: Boolean;
  354. d: PByte;
  355. begin
  356. d := @dest;
  357. totalread := 0;
  358. r := True;
  359. while (totalread < size) and r do begin;
  360. if EBufPos >= EBufCnt then begin
  361. EBufPos := 0;
  362. EBufCnt := EBUF_SIZE;
  363. if EBufCnt > limit - index then
  364. EBufCnt := limit - index;
  365. blockread(e.f, EBuf, EBufCnt, bytesread);
  366. EBufCnt := bytesread;
  367. if bytesread <= 0 then
  368. r := False;
  369. end;
  370. if EBufPos < EBufCnt then begin
  371. bytesread := EBufCnt - EBufPos;
  372. if bytesread > size - totalread then bytesread := size - totalread;
  373. System.Move(EBuf[EBufPos], d[totalread], bytesread);
  374. inc(EBufPos, bytesread);
  375. inc(index, bytesread);
  376. inc(totalread, bytesread);
  377. end;
  378. end;
  379. ReadNext := r;
  380. end;
  381. { Reads an unsigned LEB encoded number from the input stream }
  382. function ReadULEB128() : QWord;
  383. var
  384. shift : Byte;
  385. data : PtrInt;
  386. val : QWord;
  387. begin
  388. shift := 0;
  389. ReadULEB128 := 0;
  390. data := ReadNext();
  391. while (data <> -1) do begin
  392. val := data and $7f;
  393. ReadULEB128 := ReadULEB128 or (val shl shift);
  394. inc(shift, 7);
  395. if ((data and $80) = 0) then
  396. break;
  397. data := ReadNext();
  398. end;
  399. end;
  400. { Reads a signed LEB encoded number from the input stream }
  401. function ReadLEB128() : Int64;
  402. var
  403. shift : Byte;
  404. data : PtrInt;
  405. val : Int64;
  406. begin
  407. shift := 0;
  408. ReadLEB128 := 0;
  409. data := ReadNext();
  410. while (data <> -1) do begin
  411. val := data and $7f;
  412. ReadLEB128 := ReadLEB128 or (val shl shift);
  413. inc(shift, 7);
  414. if ((data and $80) = 0) then
  415. break;
  416. data := ReadNext();
  417. end;
  418. { extend sign. Note that we can not use shl/shr since the latter does not
  419. translate to arithmetic shifting for signed types }
  420. ReadLEB128 := (not ((ReadLEB128 and (Int64(1) shl (shift-1)))-1)) or ReadLEB128;
  421. end;
  422. {$ifdef CPUI8086}
  423. { Reads an address from the current input stream }
  424. function ReadAddress(addr_size: smallint) : LongWord;
  425. begin
  426. if addr_size = 4 then
  427. ReadNext(ReadAddress, 4)
  428. else if addr_size = 2 then begin
  429. ReadAddress := 0;
  430. ReadNext(ReadAddress, 2);
  431. end
  432. else
  433. ReadAddress := 0;
  434. end;
  435. { Reads a segment from the current input stream }
  436. function ReadSegment() : Word;
  437. begin
  438. ReadNext(ReadSegment, sizeof(ReadSegment));
  439. end;
  440. {$else CPUI8086}
  441. { Reads an address from the current input stream }
  442. function ReadAddress(addr_size: smallint) : PtrUInt;
  443. begin
  444. ReadNext(ReadAddress, sizeof(ReadAddress));
  445. end;
  446. {$endif CPUI8086}
  447. { Reads a zero-terminated string from the current input stream. If the
  448. string is larger than 255 chars (maximum allowed number of elements in
  449. a ShortString, excess characters will be chopped off. }
  450. function ReadString() : ShortString;
  451. var
  452. temp : PtrInt;
  453. i : PtrUInt;
  454. begin
  455. i := 1;
  456. temp := ReadNext();
  457. while (temp > 0) do begin
  458. ReadString[i] := char(temp);
  459. if (i = 255) then begin
  460. { skip remaining characters }
  461. repeat
  462. temp := ReadNext();
  463. until (temp <= 0);
  464. break;
  465. end;
  466. inc(i);
  467. temp := ReadNext();
  468. end;
  469. { unexpected end of file occurred? }
  470. if (temp = -1) then
  471. ReadString := ''
  472. else
  473. Byte(ReadString[0]) := i-1;
  474. end;
  475. { Reads an unsigned Half from the current input stream }
  476. function ReadUHalf() : Word;
  477. begin
  478. ReadNext(ReadUHalf, sizeof(ReadUHalf));
  479. end;
  480. {---------------------------------------------------------------------------
  481. Generic Dwarf lineinfo reader
  482. The line info reader is based on the information contained in
  483. DWARF Debugging Information Format Version 3
  484. Chapter 6.2 "Line Number Information"
  485. from the
  486. DWARF Debugging Information Format Workgroup.
  487. For more information on this document see also
  488. http://dwarf.freestandards.org/
  489. ---------------------------------------------------------------------------}
  490. { initializes the line info state to the default values }
  491. procedure InitStateRegisters(var state : TMachineState; const aIs_Stmt : Bool8);
  492. begin
  493. with state do begin
  494. address := 0;
  495. segment := 0;
  496. file_id := 1;
  497. line := 1;
  498. column := 0;
  499. is_stmt := aIs_Stmt;
  500. basic_block := false;
  501. end_sequence := false;
  502. prolouge_end := false;
  503. epilouge_begin := false;
  504. isa := 0;
  505. append_row := false;
  506. end;
  507. end;
  508. { Skips all line info directory entries }
  509. procedure SkipDirectories();
  510. var s : ShortString;
  511. begin
  512. while (true) do begin
  513. s := ReadString();
  514. if (s = '') then break;
  515. DEBUG_WRITELN('Skipping directory : ', s);
  516. end;
  517. end;
  518. { Skips an LEB128 }
  519. procedure SkipLEB128();
  520. {$ifdef DEBUG_DWARF_PARSER}
  521. var temp : QWord;
  522. {$endif}
  523. begin
  524. {$ifdef DEBUG_DWARF_PARSER}temp := {$endif}ReadLEB128();
  525. DEBUG_WRITELN('Skipping LEB128 : ', temp);
  526. end;
  527. { Skips the filename section from the current file stream }
  528. procedure SkipFilenames();
  529. var s : ShortString;
  530. begin
  531. while (true) do begin
  532. s := ReadString();
  533. if (s = '') then break;
  534. DEBUG_WRITELN('Skipping filename : ', s);
  535. SkipLEB128(); { skip the directory index for the file }
  536. SkipLEB128(); { skip last modification time for file }
  537. SkipLEB128(); { skip length of file }
  538. end;
  539. end;
  540. function CalculateAddressIncrement(opcode : Byte; const header : TLineNumberProgramHeader64) : Int64;
  541. begin
  542. CalculateAddressIncrement := (Int64(opcode) - header.opcode_base) div header.line_range * header.minimum_instruction_length;
  543. end;
  544. function GetFullFilename(const filenameStart, directoryStart : Int64; const file_id : DWord) : ShortString;
  545. var
  546. i : DWord;
  547. filename, directory : ShortString;
  548. dirindex : Int64;
  549. begin
  550. filename := '';
  551. directory := '';
  552. i := 1;
  553. Seek(filenameStart);
  554. while (i <= file_id) do begin
  555. filename := ReadString();
  556. DEBUG_WRITELN('Found "', filename, '"');
  557. if (filename = '') then break;
  558. dirindex := ReadLEB128(); { read the directory index for the file }
  559. SkipLEB128(); { skip last modification time for file }
  560. SkipLEB128(); { skip length of file }
  561. inc(i);
  562. end;
  563. { if we could not find the file index, exit }
  564. if (filename = '') then begin
  565. GetFullFilename := '(Unknown file)';
  566. exit;
  567. end;
  568. Seek(directoryStart);
  569. i := 1;
  570. while (i <= dirindex) do begin
  571. directory := ReadString();
  572. if (directory = '') then break;
  573. inc(i);
  574. end;
  575. if (directory<>'') and (directory[length(directory)]<>'/') then
  576. directory:=directory+'/';
  577. GetFullFilename := directory + filename;
  578. end;
  579. function ParseCompilationUnit(const addr : TOffset; const segment : TSegment; const file_offset : QWord;
  580. var source : String; var line : longint; var found : Boolean) : QWord;
  581. var
  582. state : TMachineState;
  583. { we need both headers on the stack, although we only use the 64 bit one internally }
  584. header64 : TLineNumberProgramHeader64;
  585. header32 : TLineNumberProgramHeader32;
  586. adjusted_opcode : Int64;
  587. opcode : PtrInt;
  588. extended_opcode : PtrInt;
  589. extended_opcode_length : PtrInt;
  590. i, addrIncrement, lineIncrement : PtrInt;
  591. {$ifdef DEBUG_DWARF_PARSER}
  592. s : ShortString;
  593. {$endif}
  594. numoptable : array[1..255] of Byte;
  595. { the offset into the file where the include directories are stored for this compilation unit }
  596. include_directories : QWord;
  597. { the offset into the file where the file names are stored for this compilation unit }
  598. file_names : Int64;
  599. temp_length : DWord;
  600. unit_length : QWord;
  601. header_length : SizeInt;
  602. first_row : Boolean;
  603. prev_line : QWord;
  604. prev_file : DWord;
  605. begin
  606. prev_line := 0;
  607. prev_file := 0;
  608. first_row := true;
  609. found := false;
  610. ReadNext(temp_length, sizeof(temp_length));
  611. if (temp_length <> $ffffffff) then begin
  612. unit_length := temp_length + sizeof(temp_length)
  613. end else begin
  614. ReadNext(unit_length, sizeof(unit_length));
  615. inc(unit_length, 12);
  616. end;
  617. ParseCompilationUnit := file_offset + unit_length;
  618. Init(file_offset, unit_length);
  619. DEBUG_WRITELN('Unit length: ', unit_length);
  620. if (temp_length <> $ffffffff) then begin
  621. DEBUG_WRITELN('32 bit DWARF detected');
  622. ReadNext(header32, sizeof(header32));
  623. header64.magic := $ffffffff;
  624. header64.unit_length := header32.unit_length;
  625. header64.version := header32.version;
  626. header64.length := header32.length;
  627. header64.minimum_instruction_length := header32.minimum_instruction_length;
  628. header64.default_is_stmt := header32.default_is_stmt;
  629. header64.line_base := header32.line_base;
  630. header64.line_range := header32.line_range;
  631. header64.opcode_base := header32.opcode_base;
  632. header_length :=
  633. sizeof(header32.length) + sizeof(header32.version) +
  634. sizeof(header32.unit_length);
  635. end else begin
  636. DEBUG_WRITELN('64 bit DWARF detected');
  637. ReadNext(header64, sizeof(header64));
  638. header_length :=
  639. sizeof(header64.magic) + sizeof(header64.version) +
  640. sizeof(header64.length) + sizeof(header64.unit_length);
  641. end;
  642. inc(header_length, header64.length);
  643. fillchar(numoptable, sizeof(numoptable), #0);
  644. ReadNext(numoptable, header64.opcode_base-1);
  645. DEBUG_WRITELN('Opcode parameter count table');
  646. for i := 1 to header64.opcode_base-1 do begin
  647. DEBUG_WRITELN('Opcode[', i, '] - ', numoptable[i], ' parameters');
  648. end;
  649. DEBUG_WRITELN('Reading directories...');
  650. include_directories := Pos();
  651. SkipDirectories();
  652. DEBUG_WRITELN('Reading filenames...');
  653. file_names := Pos();
  654. SkipFilenames();
  655. Seek(header_length);
  656. with header64 do begin
  657. InitStateRegisters(state, default_is_stmt);
  658. end;
  659. opcode := ReadNext();
  660. while (opcode <> -1) and (not found) do begin
  661. DEBUG_WRITELN('Next opcode: ');
  662. case (opcode) of
  663. { extended opcode }
  664. 0 : begin
  665. extended_opcode_length := ReadULEB128();
  666. extended_opcode := ReadNext();
  667. case (extended_opcode) of
  668. -1: begin
  669. exit;
  670. end;
  671. DW_LNE_END_SEQUENCE : begin
  672. state.end_sequence := true;
  673. state.append_row := true;
  674. DEBUG_WRITELN('DW_LNE_END_SEQUENCE');
  675. end;
  676. DW_LNE_SET_ADDRESS : begin
  677. state.address := ReadAddress(extended_opcode_length-1);
  678. DEBUG_WRITELN('DW_LNE_SET_ADDRESS (', hexstr(state.address, sizeof(state.address)*2), ')');
  679. end;
  680. {$ifdef CPUI8086}
  681. DW_LNE_SET_SEGMENT : begin
  682. state.segment := ReadSegment();
  683. DEBUG_WRITELN('DW_LNE_SET_SEGMENT (', hexstr(state.segment, sizeof(state.segment)*2), ')');
  684. end;
  685. {$endif CPUI8086}
  686. DW_LNE_DEFINE_FILE : begin
  687. {$ifdef DEBUG_DWARF_PARSER}s := {$endif}ReadString();
  688. SkipLEB128();
  689. SkipLEB128();
  690. SkipLEB128();
  691. DEBUG_WRITELN('DW_LNE_DEFINE_FILE (', s, ')');
  692. end;
  693. else begin
  694. DEBUG_WRITELN('Unknown extended opcode (opcode ', extended_opcode, ' length ', extended_opcode_length, ')');
  695. for i := 0 to extended_opcode_length-2 do
  696. if ReadNext() = -1 then
  697. exit;
  698. end;
  699. end;
  700. end;
  701. DW_LNS_COPY : begin
  702. state.basic_block := false;
  703. state.prolouge_end := false;
  704. state.epilouge_begin := false;
  705. state.append_row := true;
  706. DEBUG_WRITELN('DW_LNS_COPY');
  707. end;
  708. DW_LNS_ADVANCE_PC : begin
  709. inc(state.address, ReadULEB128() * header64.minimum_instruction_length);
  710. DEBUG_WRITELN('DW_LNS_ADVANCE_PC (', hexstr(state.address, sizeof(state.address)*2), ')');
  711. end;
  712. DW_LNS_ADVANCE_LINE : begin
  713. // inc(state.line, ReadLEB128()); negative values are allowed
  714. // but those may generate a range check error
  715. state.line := state.line + ReadLEB128();
  716. DEBUG_WRITELN('DW_LNS_ADVANCE_LINE (', state.line, ')');
  717. end;
  718. DW_LNS_SET_FILE : begin
  719. state.file_id := ReadULEB128();
  720. DEBUG_WRITELN('DW_LNS_SET_FILE (', state.file_id, ')');
  721. end;
  722. DW_LNS_SET_COLUMN : begin
  723. state.column := ReadULEB128();
  724. DEBUG_WRITELN('DW_LNS_SET_COLUMN (', state.column, ')');
  725. end;
  726. DW_LNS_NEGATE_STMT : begin
  727. state.is_stmt := not state.is_stmt;
  728. DEBUG_WRITELN('DW_LNS_NEGATE_STMT (', state.is_stmt, ')');
  729. end;
  730. DW_LNS_SET_BASIC_BLOCK : begin
  731. state.basic_block := true;
  732. DEBUG_WRITELN('DW_LNS_SET_BASIC_BLOCK');
  733. end;
  734. DW_LNS_CONST_ADD_PC : begin
  735. inc(state.address, CalculateAddressIncrement(255, header64));
  736. DEBUG_WRITELN('DW_LNS_CONST_ADD_PC (', hexstr(state.address, sizeof(state.address)*2), ')');
  737. end;
  738. DW_LNS_FIXED_ADVANCE_PC : begin
  739. inc(state.address, ReadUHalf());
  740. DEBUG_WRITELN('DW_LNS_FIXED_ADVANCE_PC (', hexstr(state.address, sizeof(state.address)*2), ')');
  741. end;
  742. DW_LNS_SET_PROLOGUE_END : begin
  743. state.prolouge_end := true;
  744. DEBUG_WRITELN('DW_LNS_SET_PROLOGUE_END');
  745. end;
  746. DW_LNS_SET_EPILOGUE_BEGIN : begin
  747. state.epilouge_begin := true;
  748. DEBUG_WRITELN('DW_LNS_SET_EPILOGUE_BEGIN');
  749. end;
  750. DW_LNS_SET_ISA : begin
  751. state.isa := ReadULEB128();
  752. DEBUG_WRITELN('DW_LNS_SET_ISA (', state.isa, ')');
  753. end;
  754. else begin { special opcode }
  755. if (opcode < header64.opcode_base) then begin
  756. DEBUG_WRITELN('Unknown standard opcode $', hexstr(opcode, 2), '; skipping');
  757. for i := 1 to numoptable[opcode] do
  758. SkipLEB128();
  759. end else begin
  760. adjusted_opcode := opcode - header64.opcode_base;
  761. addrIncrement := CalculateAddressIncrement(opcode, header64);
  762. inc(state.address, addrIncrement);
  763. lineIncrement := header64.line_base + (adjusted_opcode mod header64.line_range);
  764. inc(state.line, lineIncrement);
  765. DEBUG_WRITELN('Special opcode $', hexstr(opcode, 2), ' address increment: ', addrIncrement, ' new line: ', lineIncrement);
  766. state.basic_block := false;
  767. state.prolouge_end := false;
  768. state.epilouge_begin := false;
  769. state.append_row := true;
  770. end;
  771. end;
  772. end;
  773. if (state.append_row) then begin
  774. DEBUG_WRITELN('Current state : address = ', hexstr(state.address, sizeof(state.address) * 2),
  775. {$ifdef CPUI8086}
  776. DEBUG_COMMENT ' segment = ', hexstr(state.segment, sizeof(state.segment) * 2),
  777. {$endif CPUI8086}
  778. DEBUG_COMMENT ' file_id = ', state.file_id, ' line = ', state.line, ' column = ', state.column,
  779. DEBUG_COMMENT ' is_stmt = ', state.is_stmt, ' basic_block = ', state.basic_block,
  780. DEBUG_COMMENT ' end_sequence = ', state.end_sequence, ' prolouge_end = ', state.prolouge_end,
  781. DEBUG_COMMENT ' epilouge_begin = ', state.epilouge_begin, ' isa = ', state.isa);
  782. if (first_row) then begin
  783. if (state.segment > segment) or
  784. ((state.segment = segment) and
  785. (state.address > addr)) then
  786. break;
  787. first_row := false;
  788. end;
  789. { when we have found the address we need to return the previous
  790. line because that contains the call instruction }
  791. if (state.segment > segment) or
  792. ((state.segment = segment) and
  793. (state.address >= addr)) then
  794. found:=true
  795. else
  796. begin
  797. { save line information }
  798. prev_file := state.file_id;
  799. prev_line := state.line;
  800. end;
  801. state.append_row := false;
  802. if (state.end_sequence) then begin
  803. InitStateRegisters(state, header64.default_is_stmt);
  804. first_row := true;
  805. end;
  806. end;
  807. opcode := ReadNext();
  808. end;
  809. if (found) then begin
  810. line := prev_line;
  811. source := GetFullFilename(file_names, include_directories, prev_file);
  812. end;
  813. end;
  814. var
  815. Abbrev_Offsets : array of QWord;
  816. Abbrev_Tags : array of QWord;
  817. Abbrev_Children : array of Byte;
  818. Abbrev_Attrs : array of array of record attr,form : QWord; end;
  819. procedure ReadAbbrevTable;
  820. var
  821. i : PtrInt;
  822. tag,
  823. nr,
  824. attr,
  825. form,
  826. PrevHigh : Int64;
  827. begin
  828. DEBUG_WRITELN('Starting to read abbrev. section at $',hexstr(Dwarf_Debug_Abbrev_Section_Offset+Pos,16));
  829. repeat
  830. nr:=ReadULEB128;
  831. if nr=0 then
  832. break;
  833. if nr>high(Abbrev_Offsets) then
  834. begin
  835. SetLength(Abbrev_Offsets,nr+1024);
  836. SetLength(Abbrev_Tags,nr+1024);
  837. SetLength(Abbrev_Attrs,nr+1024);
  838. SetLength(Abbrev_Children,nr+1024);
  839. end;
  840. Abbrev_Offsets[nr]:=Pos;
  841. { read tag }
  842. tag:=ReadULEB128;
  843. Abbrev_Tags[nr]:=tag;
  844. DEBUG_WRITELN('Abbrev ',nr,' at offset ',Pos,' has tag $',hexstr(tag,4));
  845. { read flag for children }
  846. Abbrev_Children[nr]:=ReadNext;
  847. i:=0;
  848. { ensure that length(Abbrev_Attrs)=0 if an entry is overwritten (not sure if this will ever happen) and
  849. the new entry has no attributes }
  850. Abbrev_Attrs[nr]:=nil;
  851. repeat
  852. attr:=ReadULEB128;
  853. form:=ReadULEB128;
  854. if attr<>0 then
  855. begin
  856. SetLength(Abbrev_Attrs[nr],i+1);
  857. Abbrev_Attrs[nr][i].attr:=attr;
  858. Abbrev_Attrs[nr][i].form:=form;
  859. end;
  860. inc(i);
  861. until attr=0;
  862. DEBUG_WRITELN('Abbrev ',nr,' has ',Length(Abbrev_Attrs[nr]),' attributes');
  863. until false;
  864. end;
  865. function ParseCompilationUnitForDebugInfoOffset(const addr : TOffset; const segment : TSegment; const file_offset : QWord;
  866. var debug_info_offset : QWord; var found : Boolean) : QWord;
  867. {$ifndef CPUI8086}
  868. const
  869. arange_segment = 0;
  870. {$endif CPUI8086}
  871. var
  872. { we need both headers on the stack, although we only use the 64 bit one internally }
  873. header64 : TDebugArangesHeader64;
  874. header32 : TDebugArangesHeader32;
  875. isdwarf64 : boolean;
  876. temp_length : DWord;
  877. unit_length : QWord;
  878. {$ifdef CPUI8086}
  879. arange_start, arange_size: DWord;
  880. arange_segment: Word;
  881. {$else CPUI8086}
  882. arange_start, arange_size: PtrUInt;
  883. {$endif CPUI8086}
  884. begin
  885. found := false;
  886. ReadNext(temp_length, sizeof(temp_length));
  887. if (temp_length <> $ffffffff) then begin
  888. unit_length := temp_length + sizeof(temp_length)
  889. end else begin
  890. ReadNext(unit_length, sizeof(unit_length));
  891. inc(unit_length, 12);
  892. end;
  893. ParseCompilationUnitForDebugInfoOffset := file_offset + unit_length;
  894. Init(file_offset, unit_length);
  895. DEBUG_WRITELN('Unit length: ', unit_length);
  896. if (temp_length <> $ffffffff) then
  897. begin
  898. DEBUG_WRITELN('32 bit DWARF detected');
  899. ReadNext(header32, sizeof(header32));
  900. header64.magic := $ffffffff;
  901. header64.unit_length := header32.unit_length;
  902. header64.version := header32.version;
  903. header64.debug_info_offset := header32.debug_info_offset;
  904. header64.address_size := header32.address_size;
  905. header64.segment_size := header32.segment_size;
  906. isdwarf64:=false;
  907. end
  908. else
  909. begin
  910. DEBUG_WRITELN('64 bit DWARF detected');
  911. ReadNext(header64, sizeof(header64));
  912. isdwarf64:=true;
  913. end;
  914. DEBUG_WRITELN('debug_info_offset: ',header64.debug_info_offset);
  915. DEBUG_WRITELN('address_size: ', header64.address_size);
  916. DEBUG_WRITELN('segment_size: ', header64.segment_size);
  917. arange_start:=ReadAddress(header64.address_size);
  918. {$ifdef CPUI8086}
  919. arange_segment:=ReadSegment();
  920. {$endif CPUI8086}
  921. arange_size:=ReadAddress(header64.address_size);
  922. while not((arange_start=0) and (arange_segment=0) and (arange_size=0)) and (not found) do
  923. begin
  924. if (segment=arange_segment) and (addr>=arange_start) and (addr<=arange_start+arange_size) then
  925. begin
  926. found:=true;
  927. debug_info_offset:=header64.debug_info_offset;
  928. DEBUG_WRITELN('Matching aranges entry $',hexStr(arange_start,header64.address_size*2),', $',hexStr(arange_size,header64.address_size*2));
  929. end;
  930. arange_start:=ReadAddress(header64.address_size);
  931. {$ifdef CPUI8086}
  932. arange_segment:=ReadSegment();
  933. {$endif CPUI8086}
  934. arange_size:=ReadAddress(header64.address_size);
  935. end;
  936. end;
  937. function ParseCompilationUnitForFunctionName(const addr : TOffset; const segment : TSegment; const file_offset : QWord;
  938. var func : String; var found : Boolean) : QWord;
  939. var
  940. { we need both headers on the stack, although we only use the 64 bit one internally }
  941. header64 : TDebugInfoProgramHeader64;
  942. header32 : TDebugInfoProgramHeader32;
  943. isdwarf64 : boolean;
  944. abbrev,
  945. high_pc,
  946. low_pc : QWord;
  947. temp_length : DWord;
  948. unit_length : QWord;
  949. name : String;
  950. level : Integer;
  951. procedure SkipAttr(form : QWord);
  952. var
  953. dummy : array[0..7] of byte;
  954. bl : byte;
  955. wl : word;
  956. dl : dword;
  957. ql : qword;
  958. i : PtrUInt;
  959. begin
  960. case form of
  961. DW_FORM_addr:
  962. ReadNext(dummy,header64.address_size);
  963. DW_FORM_block2:
  964. begin
  965. ReadNext(wl,SizeOf(wl));
  966. for i:=1 to wl do
  967. ReadNext;
  968. end;
  969. DW_FORM_block4:
  970. begin
  971. ReadNext(dl,SizeOf(dl));
  972. for i:=1 to dl do
  973. ReadNext;
  974. end;
  975. DW_FORM_data2:
  976. ReadNext(dummy,2);
  977. DW_FORM_data4:
  978. ReadNext(dummy,4);
  979. DW_FORM_data8:
  980. ReadNext(dummy,8);
  981. DW_FORM_string:
  982. ReadString;
  983. DW_FORM_block,
  984. DW_FORM_exprloc:
  985. begin
  986. ql:=ReadULEB128;
  987. for i:=1 to ql do
  988. ReadNext;
  989. end;
  990. DW_FORM_block1:
  991. begin
  992. bl:=ReadNext;
  993. for i:=1 to bl do
  994. ReadNext;
  995. end;
  996. DW_FORM_data1,
  997. DW_FORM_flag:
  998. ReadNext(dummy,1);
  999. DW_FORM_sdata:
  1000. ReadLEB128;
  1001. DW_FORM_ref_addr:
  1002. { the size of DW_FORM_ref_addr changed between DWAWRF2 and later versions:
  1003. in DWARF2 it depends on the architecture address size, in later versions on the DWARF type (32 bit/64 bit)
  1004. }
  1005. if header64.version>2 then
  1006. begin
  1007. if isdwarf64 then
  1008. ReadNext(dummy,8)
  1009. else
  1010. ReadNext(dummy,4);
  1011. end
  1012. else
  1013. begin
  1014. { address size for DW_FORM_ref_addr must be at least 32 bits }
  1015. { this is compatible with Open Watcom on i8086 }
  1016. if header64.address_size<4 then
  1017. ReadNext(dummy,4)
  1018. else
  1019. ReadNext(dummy,header64.address_size);
  1020. end;
  1021. DW_FORM_strp,
  1022. DW_FORM_sec_offset:
  1023. if isdwarf64 then
  1024. ReadNext(dummy,8)
  1025. else
  1026. ReadNext(dummy,4);
  1027. DW_FORM_udata:
  1028. ReadULEB128;
  1029. DW_FORM_ref1:
  1030. ReadNext(dummy,1);
  1031. DW_FORM_ref2:
  1032. ReadNext(dummy,2);
  1033. DW_FORM_ref4:
  1034. ReadNext(dummy,4);
  1035. DW_FORM_ref8:
  1036. ReadNext(dummy,8);
  1037. DW_FORM_ref_udata:
  1038. ReadULEB128;
  1039. DW_FORM_indirect:
  1040. SkipAttr(ReadULEB128);
  1041. DW_FORM_flag_present: {none};
  1042. else
  1043. begin
  1044. writeln(stderr,'Internal error: unknown dwarf form: $',hexstr(form,2));
  1045. ReadNext;
  1046. exit;
  1047. end;
  1048. end;
  1049. end;
  1050. var
  1051. i : PtrInt;
  1052. prev_base,prev_limit : TFilePos;
  1053. prev_pos : TFilePos;
  1054. begin
  1055. found := false;
  1056. ReadNext(temp_length, sizeof(temp_length));
  1057. if (temp_length <> $ffffffff) then begin
  1058. unit_length := temp_length + sizeof(temp_length)
  1059. end else begin
  1060. ReadNext(unit_length, sizeof(unit_length));
  1061. inc(unit_length, 12);
  1062. end;
  1063. ParseCompilationUnitForFunctionName := file_offset + unit_length;
  1064. Init(file_offset, unit_length);
  1065. DEBUG_WRITELN('Unit length: ', unit_length);
  1066. if (temp_length <> $ffffffff) then begin
  1067. DEBUG_WRITELN('32 bit DWARF detected');
  1068. ReadNext(header32, sizeof(header32));
  1069. header64.magic := $ffffffff;
  1070. header64.unit_length := header32.unit_length;
  1071. header64.version := header32.version;
  1072. header64.debug_abbrev_offset := header32.debug_abbrev_offset;
  1073. header64.address_size := header32.address_size;
  1074. isdwarf64:=false;
  1075. end else begin
  1076. DEBUG_WRITELN('64 bit DWARF detected');
  1077. ReadNext(header64, sizeof(header64));
  1078. isdwarf64:=true;
  1079. end;
  1080. DEBUG_WRITELN('debug_abbrev_offset: ',header64.debug_abbrev_offset);
  1081. DEBUG_WRITELN('address_size: ',header64.address_size);
  1082. { not nice, but we have to read the abbrev section after the start of the debug_info section has been read }
  1083. prev_limit:=limit;
  1084. prev_base:=base;
  1085. prev_pos:=Pos;
  1086. Init(Dwarf_Debug_Abbrev_Section_Offset+header64.debug_abbrev_offset,Dwarf_Debug_Abbrev_Section_Size);
  1087. ReadAbbrevTable;
  1088. { restore previous reading state and position }
  1089. Init(prev_base,prev_limit);
  1090. Seek(prev_pos);
  1091. abbrev:=ReadULEB128;
  1092. level:=0;
  1093. while (abbrev <> 0) and (not found) do
  1094. begin
  1095. DEBUG_WRITELN('Next abbrev: ',abbrev);
  1096. if Abbrev_Children[abbrev]<>0 then
  1097. inc(level);
  1098. { DW_TAG_subprogram? }
  1099. if Abbrev_Tags[abbrev]=$2e then
  1100. begin
  1101. low_pc:=1;
  1102. high_pc:=0;
  1103. name:='';
  1104. for i:=0 to high(Abbrev_Attrs[abbrev]) do
  1105. begin
  1106. { DW_AT_low_pc }
  1107. if (Abbrev_Attrs[abbrev][i].attr=$11) and
  1108. (Abbrev_Attrs[abbrev][i].form=DW_FORM_addr) then
  1109. begin
  1110. low_pc:=0;
  1111. ReadNext(low_pc,header64.address_size);
  1112. end
  1113. { DW_AT_high_pc }
  1114. else if (Abbrev_Attrs[abbrev][i].attr=$12) and
  1115. (Abbrev_Attrs[abbrev][i].form=DW_FORM_addr) then
  1116. begin
  1117. high_pc:=0;
  1118. ReadNext(high_pc,header64.address_size);
  1119. end
  1120. { DW_AT_name }
  1121. else if (Abbrev_Attrs[abbrev][i].attr=$3) and
  1122. { avoid that we accidently read an DW_FORM_strp entry accidently }
  1123. (Abbrev_Attrs[abbrev][i].form=DW_FORM_string) then
  1124. begin
  1125. name:=ReadString;
  1126. end
  1127. else
  1128. SkipAttr(Abbrev_Attrs[abbrev][i].form);
  1129. end;
  1130. DEBUG_WRITELN('Got DW_TAG_subprogram with low pc = $',hexStr(low_pc,header64.address_size*2),', high pc = $',hexStr(high_pc,header64.address_size*2),', name = ',name);
  1131. if (addr>low_pc) and (addr<high_pc) then
  1132. begin
  1133. found:=true;
  1134. func:=name;
  1135. end;
  1136. end
  1137. else
  1138. begin
  1139. for i:=0 to high(Abbrev_Attrs[abbrev]) do
  1140. SkipAttr(Abbrev_Attrs[abbrev][i].form);
  1141. end;
  1142. abbrev:=ReadULEB128;
  1143. { skip entries signaling that no more child entries are following }
  1144. while (level>0) and (abbrev=0) do
  1145. begin
  1146. dec(level);
  1147. abbrev:=ReadULEB128;
  1148. end;
  1149. end;
  1150. end;
  1151. const
  1152. { 64 bit and 32 bit CPUs tend to have more memory }
  1153. {$if defined(CPU64)}
  1154. LineInfoCacheLength = 2039;
  1155. {$elseif defined(CPU32)}
  1156. LineInfoCacheLength = 251;
  1157. {$else}
  1158. LineInfoCacheLength = 1;
  1159. {$endif CPU64}
  1160. var
  1161. LineInfoCache : array[0..LineInfoCacheLength-1] of
  1162. record
  1163. addr : codeptruint;
  1164. func, source : string;
  1165. line : longint;
  1166. end;
  1167. function GetLineInfo(addr : codeptruint; var func, source : string; var line : longint) : boolean;
  1168. var
  1169. current_offset,
  1170. end_offset, debug_info_offset_from_aranges : QWord;
  1171. segment : Word = 0;
  1172. found, found_aranges : Boolean;
  1173. CacheIndex: CodePtrUInt;
  1174. begin
  1175. func := '';
  1176. source := '';
  1177. GetLineInfo:=false;
  1178. CacheIndex:=addr mod LineInfoCacheLength;
  1179. if LineInfoCache[CacheIndex].addr=addr then
  1180. begin
  1181. func:=LineInfoCache[CacheIndex].func;
  1182. source:=LineInfoCache[CacheIndex].source;
  1183. line:=LineInfoCache[CacheIndex].line;
  1184. GetLineInfo:=true;
  1185. exit;
  1186. end;
  1187. if not OpenDwarf(codepointer(addr)) then
  1188. exit;
  1189. {$ifdef CPUI8086}
  1190. {$if defined(FPC_MM_MEDIUM) or defined(FPC_MM_LARGE) or defined(FPC_MM_HUGE)}
  1191. segment := (addr shr 16) - e.processsegment;
  1192. addr := Word(addr);
  1193. {$else}
  1194. segment := CSeg - e.processsegment;
  1195. {$endif}
  1196. {$endif CPUI8086}
  1197. addr := addr - e.processaddress;
  1198. current_offset := Dwarf_Debug_Line_Section_Offset;
  1199. end_offset := Dwarf_Debug_Line_Section_Offset + Dwarf_Debug_Line_Section_Size;
  1200. found := false;
  1201. while (current_offset < end_offset) and (not found) do begin
  1202. Init(current_offset, end_offset - current_offset);
  1203. current_offset := ParseCompilationUnit(addr, segment, current_offset,
  1204. source, line, found);
  1205. end;
  1206. current_offset := Dwarf_Debug_Aranges_Section_Offset;
  1207. end_offset := Dwarf_Debug_Aranges_Section_Offset + Dwarf_Debug_Aranges_Section_Size;
  1208. found_aranges := false;
  1209. while (current_offset < end_offset) and (not found_aranges) do begin
  1210. Init(current_offset, end_offset - current_offset);
  1211. current_offset := ParseCompilationUnitForDebugInfoOffset(addr, segment, current_offset, debug_info_offset_from_aranges, found_aranges);
  1212. end;
  1213. { no function name found yet }
  1214. found := false;
  1215. if found_aranges then
  1216. begin
  1217. DEBUG_WRITELN('Found .debug_info offset $',hexstr(debug_info_offset_from_aranges,8),' from .debug_aranges');
  1218. current_offset := Dwarf_Debug_Info_Section_Offset + debug_info_offset_from_aranges;
  1219. end_offset := Dwarf_Debug_Info_Section_Offset + debug_info_offset_from_aranges + Dwarf_Debug_Info_Section_Size;
  1220. DEBUG_WRITELN('Reading .debug_info at section offset $',hexStr(current_offset-Dwarf_Debug_Info_Section_Offset,16));
  1221. Init(current_offset, end_offset - current_offset);
  1222. current_offset := ParseCompilationUnitForFunctionName(addr, segment, current_offset, func, found);
  1223. if found then
  1224. DEBUG_WRITELN('Found .debug_info entry by using .debug_aranges information');
  1225. end
  1226. else
  1227. DEBUG_WRITELN('No .debug_info offset found from .debug_aranges');
  1228. current_offset := Dwarf_Debug_Info_Section_Offset;
  1229. end_offset := Dwarf_Debug_Info_Section_Offset + Dwarf_Debug_Info_Section_Size;
  1230. while (current_offset < end_offset) and (not found) do begin
  1231. DEBUG_WRITELN('Reading .debug_info at section offset $',hexStr(current_offset-Dwarf_Debug_Info_Section_Offset,16));
  1232. Init(current_offset, end_offset - current_offset);
  1233. current_offset := ParseCompilationUnitForFunctionName(addr, segment, current_offset, func, found);
  1234. end;
  1235. if not AllowReuseOfLineInfoData then
  1236. CloseDwarf;
  1237. LineInfoCache[CacheIndex].addr:=addr;
  1238. LineInfoCache[CacheIndex].func:=func;
  1239. LineInfoCache[CacheIndex].source:=source;
  1240. LineInfoCache[CacheIndex].line:=line;
  1241. GetLineInfo:=true;
  1242. end;
  1243. function DwarfBackTraceStr(addr: CodePointer): string;
  1244. var
  1245. func,
  1246. source : string;
  1247. hs : string;
  1248. line : longint;
  1249. Store : TBackTraceStrFunc;
  1250. Success : boolean;
  1251. begin
  1252. {$ifdef DEBUG_LINEINFO}
  1253. writeln(stderr,'DwarfBackTraceStr called');
  1254. {$endif DEBUG_LINEINFO}
  1255. { reset to prevent infinite recursion if problems inside the code }
  1256. Success:=false;
  1257. Store := BackTraceStrFunc;
  1258. BackTraceStrFunc := @SysBackTraceStr;
  1259. Success:=GetLineInfo(codeptruint(addr), func, source, line);
  1260. { create string }
  1261. DwarfBackTraceStr :=' $' + HexStr(addr);
  1262. if Success then
  1263. begin
  1264. if func<>'' then
  1265. DwarfBackTraceStr := DwarfBackTraceStr + ' ' + func;
  1266. if source<>'' then
  1267. begin
  1268. if func<>'' then
  1269. DwarfBackTraceStr := DwarfBackTraceStr + ', ';
  1270. if line<>0 then
  1271. begin
  1272. str(line, hs);
  1273. DwarfBackTraceStr := DwarfBackTraceStr + ' line ' + hs;
  1274. end;
  1275. DwarfBackTraceStr := DwarfBackTraceStr + ' of ' + source;
  1276. end;
  1277. end;
  1278. BackTraceStrFunc := Store;
  1279. end;
  1280. initialization
  1281. lastfilename := '';
  1282. lastopendwarf := false;
  1283. BackTraceStrFunc := @DwarfBacktraceStr;
  1284. finalization
  1285. CloseDwarf;
  1286. end.