lineinfo.pp 9.1 KB

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