dbgstabs.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. {
  2. Copyright (c) 2003-2004 by Peter Vreman and Florian Klaempfl
  3. This units contains support for STABS debug info generation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit dbgstabs;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. DbgBase,
  22. aasmtai;
  23. type
  24. TDebugInfoStabs=class(TDebugInfo)
  25. procedure insertmodulestart(list:taasmoutput);override;
  26. procedure insertmoduleend(list:taasmoutput);override;
  27. procedure insertlineinfo(list:taasmoutput);override;
  28. end;
  29. implementation
  30. uses
  31. cutils,
  32. systems,globals,
  33. aasmbase,
  34. finput,fmodule;
  35. const
  36. N_GSYM = $20;
  37. N_STSYM = 38; { initialized const }
  38. N_LCSYM = 40; { non initialized variable}
  39. N_Function = $24; { function or const }
  40. N_TextLine = $44;
  41. N_DataLine = $46;
  42. N_BssLine = $48;
  43. N_RSYM = $40; { register variable }
  44. N_LSYM = $80;
  45. N_tsym = 160;
  46. N_SourceFile = $64;
  47. N_IncludeFile = $84;
  48. N_BINCL = $82;
  49. N_EINCL = $A2;
  50. N_EXCL = $C2;
  51. procedure tdebuginfostabs.insertlineinfo(list:taasmoutput);
  52. var
  53. currfileinfo,
  54. lastfileinfo : tfileposinfo;
  55. currfuncname : pstring;
  56. currsectype : tasmsectiontype;
  57. hlabel : tasmlabel;
  58. hp : tai;
  59. infile : tinputfile;
  60. begin
  61. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  62. currfuncname:=nil;
  63. currsectype:=sec_code;
  64. hp:=Tai(list.first);
  65. while assigned(hp) do
  66. begin
  67. case hp.typ of
  68. ait_section :
  69. currsectype:=tai_section(hp).sectype;
  70. ait_function_name :
  71. currfuncname:=tai_function_name(hp).funcname;
  72. ait_force_line :
  73. lastfileinfo.line:=-1;
  74. end;
  75. if (currsectype=sec_code) and
  76. (hp.typ=ait_instruction) then
  77. begin
  78. currfileinfo:=tailineinfo(hp).fileinfo;
  79. { file changed ? (must be before line info) }
  80. if (currfileinfo.fileindex<>0) and
  81. (lastfileinfo.fileindex<>currfileinfo.fileindex) then
  82. begin
  83. infile:=current_module.sourcefiles.get_file(currfileinfo.fileindex);
  84. if assigned(infile) then
  85. begin
  86. objectlibrary.getlabel(hlabel,alt_dbgfile);
  87. { emit stabs }
  88. if (infile.path^<>'') then
  89. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_includefile)+
  90. ',0,0,'+hlabel.name),hp);
  91. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_includefile)+
  92. ',0,0,'+hlabel.name),hp);
  93. list.insertbefore(tai_label.create(hlabel),hp);
  94. { force new line info }
  95. lastfileinfo.line:=-1;
  96. end;
  97. end;
  98. { line changed ? }
  99. if (lastfileinfo.line<>currfileinfo.line) and (currfileinfo.line<>0) then
  100. begin
  101. if assigned(currfuncname) and
  102. (target_info.use_function_relative_addresses) then
  103. begin
  104. objectlibrary.getlabel(hlabel,alt_dbgline);
  105. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(n_textline)+',0,'+tostr(currfileinfo.line)+','+
  106. hlabel.name+' - '+currfuncname^),hp);
  107. list.insertbefore(tai_label.create(hlabel),hp);
  108. end
  109. else
  110. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  111. end;
  112. lastfileinfo:=currfileinfo;
  113. end;
  114. hp:=tai(hp.next);
  115. end;
  116. end;
  117. procedure tdebuginfostabs.insertmodulestart(list:taasmoutput);
  118. var
  119. hlabel : tasmlabel;
  120. infile : tinputfile;
  121. templist : taasmoutput;
  122. begin
  123. { emit main source n_sourcefile }
  124. objectlibrary.getlabel(hlabel,alt_dbgfile);
  125. infile:=current_module.sourcefiles.get_file(1);
  126. templist:=taasmoutput.create;
  127. new_section(templist,sec_code,'',0);
  128. if (infile.path^<>'') then
  129. templist.concat(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_sourcefile)+
  130. ',0,0,'+hlabel.name));
  131. templist.concat(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_sourcefile)+
  132. ',0,0,'+hlabel.name));
  133. templist.concat(tai_label.create(hlabel));
  134. list.insertlist(templist);
  135. templist.free;
  136. end;
  137. procedure tdebuginfostabs.insertmoduleend(list:taasmoutput);
  138. var
  139. hlabel : tasmlabel;
  140. templist : taasmoutput;
  141. begin
  142. { emit empty n_sourcefile }
  143. objectlibrary.getlabel(hlabel,alt_dbgfile);
  144. templist:=taasmoutput.create;
  145. new_section(templist,sec_code,'',0);
  146. templist.concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(n_sourcefile)+',0,0,'+hlabel.name));
  147. templist.concat(tai_label.create(hlabel));
  148. list.insertlist(templist);
  149. templist.free;
  150. end;
  151. const
  152. dbg_stabs_info : tdbginfo =
  153. (
  154. id : dbg_stabs;
  155. idtxt : 'STABS';
  156. );
  157. initialization
  158. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  159. end.