lnfodwrf.pp 42 KB

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