lineinfo.pp 8.1 KB

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