lineinfo.pp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Peter Vreman
  4. Stabs Line Info Retriever
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. This unit should not be compiled in objfpc mode, since this would make it
  13. dependent on objpas unit.
  14. }
  15. unit lineinfo;
  16. interface
  17. {$S-}
  18. {$Q-}
  19. {$IF FPC_VERSION<3}
  20. type
  21. CodePointer = Pointer;
  22. {$ENDIF}
  23. function GetLineInfo(addr:ptruint;var func,source:string;var line:longint) : boolean;
  24. function StabBackTraceStr(addr:CodePointer):string;
  25. procedure CloseStabs;
  26. var
  27. // Allows more efficient operation by reusing previously loaded debug data
  28. // when the target module filename is the same. However, if an invalid memory
  29. // address is supplied then further calls may result in an undefined behaviour.
  30. // In summary: enable for speed, disable for resilience.
  31. AllowReuseOfLineInfoData: Boolean = True;
  32. implementation
  33. uses
  34. exeinfo,strings;
  35. const
  36. N_Function = $24;
  37. N_TextLine = $44;
  38. N_DataLine = $46;
  39. N_BssLine = $48;
  40. N_SourceFile = $64;
  41. N_IncludeFile = $84;
  42. maxstabs = 40; { size of the stabs buffer }
  43. var
  44. { GDB after 4.18 uses offset to function begin
  45. in text section but OS/2 version still uses 4.16 PM }
  46. StabsFunctionRelative: boolean;
  47. type
  48. pstab=^tstab;
  49. tstab=packed record
  50. strpos : longint;
  51. ntype : byte;
  52. nother : byte;
  53. ndesc : word;
  54. nvalue : dword;
  55. end;
  56. { We use static variable so almost no stack is required, and is thus
  57. more safe when an error has occurred in the program }
  58. {$WARNING This code is not thread-safe, and needs improvement }
  59. var
  60. e : TExeFile;
  61. stabcnt, { amount of stabs }
  62. stablen,
  63. stabofs, { absolute stab section offset in executable }
  64. stabstrlen,
  65. stabstrofs : longint; { absolute stabstr section offset in executable }
  66. dirlength : longint; { length of the dirctory part of the source file }
  67. stabs : array[0..maxstabs-1] of tstab; { buffer }
  68. funcstab, { stab with current function info }
  69. linestab, { stab with current line info }
  70. dirstab, { stab with current directory info }
  71. filestab : tstab; { stab with current file info }
  72. filename,
  73. lastfilename, { store last processed file }
  74. dbgfn : string;
  75. lastopenstabs: Boolean; { store last result of processing a file }
  76. function OpenStabs(addr : pointer) : boolean;
  77. var
  78. baseaddr : pointer;
  79. begin
  80. // False by default
  81. OpenStabs:=false;
  82. // Empty so can test if GetModuleByAddr has worked
  83. filename := '';
  84. // Get filename by address using GetModuleByAddr
  85. GetModuleByAddr(addr,baseaddr,filename);
  86. {$ifdef DEBUG_LINEINFO}
  87. writeln(stderr,filename,' Baseaddr: ',hexstr(ptruint(baseaddr),sizeof(baseaddr)*2));
  88. {$endif DEBUG_LINEINFO}
  89. // Check if GetModuleByAddr has worked
  90. if filename = '' then
  91. exit;
  92. // If target filename same as previous, then re-use previous result
  93. if AllowReuseOfLineInfoData and (filename = lastfilename) then
  94. begin
  95. {$ifdef DEBUG_LINEINFO}
  96. writeln(stderr,'Reusing debug data');
  97. {$endif DEBUG_LINEINFO}
  98. OpenStabs:=lastopenstabs;
  99. exit;
  100. end;
  101. // Close previously opened stabs
  102. CloseStabs;
  103. // Reset last open stabs result
  104. lastopenstabs := false;
  105. // Save newly processed filename
  106. lastfilename := filename;
  107. // Open exe file or debug link
  108. if not OpenExeFile(e,filename) then
  109. exit;
  110. if ReadDebugLink(e,dbgfn) then
  111. begin
  112. CloseExeFile(e);
  113. if not OpenExeFile(e,dbgfn) then
  114. exit;
  115. end;
  116. // Find stab section
  117. {$ifdef BeOS}
  118. { Do not change ProcessAddress field for BeOS/Haiku
  119. if baseAddr is lower than ProcessAdress }
  120. if ptruint(baseaddr)>ptruint(e.processaddress) then
  121. {$endif BeOS}
  122. e.processaddress:=ptruint(baseaddr)-e.processaddress;
  123. StabsFunctionRelative := E.FunctionRelative;
  124. if FindExeSection(e,'.stab',stabofs,stablen) and
  125. FindExeSection(e,'.stabstr',stabstrofs,stabstrlen) then
  126. begin
  127. stabcnt:=stablen div sizeof(tstab);
  128. lastopenstabs:=true;
  129. OpenStabs:=true;
  130. end
  131. else
  132. CloseExeFile(e);
  133. end;
  134. procedure CloseStabs;
  135. begin
  136. if e.isopen then
  137. CloseExeFile(e);
  138. // Reset last processed filename
  139. lastfilename := '';
  140. end;
  141. function GetLineInfo(addr:ptruint;var func,source:string;var line:longint) : boolean;
  142. var
  143. res,
  144. stabsleft,
  145. stabscnt,i : longint;
  146. found : boolean;
  147. lastfunc : tstab;
  148. begin
  149. GetLineInfo:=false;
  150. {$ifdef DEBUG_LINEINFO}
  151. writeln(stderr,'GetLineInfo called');
  152. {$endif DEBUG_LINEINFO}
  153. fillchar(func,high(func)+1,0);
  154. fillchar(source,high(source)+1,0);
  155. line:=0;
  156. if not OpenStabs(pointer(addr)) then
  157. exit;
  158. { correct the value to the correct address in the file }
  159. { processaddress is set in OpenStabs }
  160. addr := dword(addr - e.processaddress);
  161. {$ifdef DEBUG_LINEINFO}
  162. writeln(stderr,'Addr: ',hexstr(addr,sizeof(addr)*2));
  163. {$endif DEBUG_LINEINFO}
  164. fillchar(funcstab,sizeof(tstab),0);
  165. fillchar(filestab,sizeof(tstab),0);
  166. fillchar(dirstab,sizeof(tstab),0);
  167. fillchar(linestab,sizeof(tstab),0);
  168. fillchar(lastfunc,sizeof(tstab),0);
  169. found:=false;
  170. seek(e.f,stabofs);
  171. stabsleft:=stabcnt;
  172. repeat
  173. if stabsleft>maxstabs then
  174. stabscnt:=maxstabs
  175. else
  176. stabscnt:=stabsleft;
  177. blockread(e.f,stabs,stabscnt*sizeof(tstab),res);
  178. stabscnt:=res div sizeof(tstab);
  179. for i:=0 to stabscnt-1 do
  180. begin
  181. case stabs[i].ntype of
  182. N_BssLine,
  183. N_DataLine,
  184. N_TextLine :
  185. begin
  186. if (stabs[i].ntype=N_TextLine) and StabsFunctionRelative then
  187. inc(stabs[i].nvalue,lastfunc.nvalue);
  188. if (stabs[i].nvalue<=addr) and
  189. (stabs[i].nvalue>linestab.nvalue) then
  190. begin
  191. { if it's equal we can stop and take the last info }
  192. if stabs[i].nvalue=addr then
  193. found:=true
  194. else
  195. linestab:=stabs[i];
  196. end;
  197. end;
  198. N_Function :
  199. begin
  200. lastfunc:=stabs[i];
  201. if (stabs[i].nvalue<=addr) and
  202. (stabs[i].nvalue>funcstab.nvalue) then
  203. begin
  204. funcstab:=stabs[i];
  205. fillchar(linestab,sizeof(tstab),0);
  206. end;
  207. end;
  208. N_SourceFile,
  209. N_IncludeFile :
  210. begin
  211. if (stabs[i].nvalue<=addr) and
  212. (stabs[i].nvalue>=filestab.nvalue) then
  213. begin
  214. { if same value and type then the first one
  215. contained the directory PM }
  216. if (stabs[i].nvalue=filestab.nvalue) and
  217. (stabs[i].ntype=filestab.ntype) then
  218. dirstab:=filestab
  219. else
  220. fillchar(dirstab,sizeof(tstab),0);
  221. filestab:=stabs[i];
  222. fillchar(linestab,sizeof(tstab),0);
  223. { if new file then func is not valid anymore PM }
  224. if stabs[i].ntype=N_SourceFile then
  225. begin
  226. fillchar(funcstab,sizeof(tstab),0);
  227. fillchar(lastfunc,sizeof(tstab),0);
  228. end;
  229. end;
  230. end;
  231. end;
  232. end;
  233. dec(stabsleft,stabscnt);
  234. until found or (stabsleft=0);
  235. { get the line,source,function info }
  236. line:=linestab.ndesc;
  237. if dirstab.ntype<>0 then
  238. begin
  239. seek(e.f,stabstrofs+dirstab.strpos);
  240. blockread(e.f,source[1],high(source)-1,res);
  241. dirlength:=strlen(@source[1]);
  242. source[0]:=chr(dirlength);
  243. end
  244. else
  245. dirlength:=0;
  246. if filestab.ntype<>0 then
  247. begin
  248. seek(e.f,stabstrofs+filestab.strpos);
  249. blockread(e.f,source[dirlength+1],high(source)-(dirlength+1),res);
  250. source[0]:=chr(strlen(@source[1]));
  251. end;
  252. if funcstab.ntype<>0 then
  253. begin
  254. seek(e.f,stabstrofs+funcstab.strpos);
  255. blockread(e.f,func[1],high(func)-1,res);
  256. func[0]:=chr(strlen(@func[1]));
  257. i:=pos(':',func);
  258. if i>0 then
  259. Delete(func,i,255);
  260. end;
  261. if not AllowReuseOfLineInfoData then
  262. CloseStabs;
  263. GetLineInfo:=true;
  264. end;
  265. function StabBackTraceStr(addr:CodePointer):string;
  266. var
  267. func,
  268. source : string;
  269. hs : string;
  270. line : longint;
  271. Store : TBackTraceStrFunc;
  272. Success : boolean;
  273. begin
  274. {$ifdef DEBUG_LINEINFO}
  275. writeln(stderr,'StabBackTraceStr called');
  276. {$endif DEBUG_LINEINFO}
  277. { reset to prevent infinite recursion if problems inside the code PM }
  278. Success:=false;
  279. Store:=BackTraceStrFunc;
  280. BackTraceStrFunc:=@SysBackTraceStr;
  281. Success:=GetLineInfo(ptruint(addr),func,source,line);
  282. { create string }
  283. {$ifdef netware}
  284. { we need addr relative to code start on netware }
  285. dec(addr,ptruint(system.NWGetCodeStart));
  286. StabBackTraceStr:=' CodeStart + $'+HexStr(ptruint(addr),sizeof(ptruint)*2);
  287. {$else}
  288. StabBackTraceStr:=' $'+HexStr(ptruint(addr),sizeof(ptruint)*2);
  289. {$endif}
  290. if Success then
  291. begin
  292. if func<>'' then
  293. StabBackTraceStr:=StabBackTraceStr+' '+func;
  294. if source<>'' then
  295. begin
  296. if func<>'' then
  297. StabBackTraceStr:=StabBackTraceStr+', ';
  298. if line<>0 then
  299. begin
  300. str(line,hs);
  301. StabBackTraceStr:=StabBackTraceStr+' line '+hs;
  302. end;
  303. StabBackTraceStr:=StabBackTraceStr+' of '+source;
  304. end;
  305. end;
  306. BackTraceStrFunc:=Store;
  307. end;
  308. initialization
  309. lastfilename := '';
  310. lastopenstabs := false;
  311. BackTraceStrFunc:=@StabBackTraceStr;
  312. finalization
  313. CloseStabs;
  314. end.