dbgstabs.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  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. cclasses,
  22. systems,dbgbase,cgbase,
  23. symconst,symtype,symdef,symsym,symtable,symbase,
  24. aasmtai,aasmdata;
  25. const
  26. { stab types }
  27. STABS_N_GSYM = $20;
  28. STABS_N_STSYM = 38; { initialized const }
  29. STABS_N_LCSYM = 40; { non initialized variable}
  30. STABS_N_Function = $24; { function or const }
  31. STABS_N_TextLine = $44;
  32. STABS_N_DataLine = $46;
  33. STABS_N_BssLine = $48;
  34. STABS_N_RSYM = $40; { register variable }
  35. STABS_N_LSYM = $80;
  36. STABS_N_DECL = $8c;
  37. STABS_N_RPSYM = $8e;
  38. STABS_N_tsym = 160;
  39. STABS_N_SourceFile = $64;
  40. { APPLE LOCAL N_OSO: This is the stab that associated the .o file with the
  41. N_SO stab, in the case where debug info is mostly stored in the .o file. }
  42. STABS_N_OSO = $66;
  43. STABS_N_IncludeFile = $84;
  44. STABS_N_BINCL = $82;
  45. STABS_N_EINCL = $A2;
  46. STABS_N_LBRAC = $C0;
  47. STABS_N_EXCL = $C2;
  48. STABS_N_RBRAC = $E0;
  49. type
  50. TDebugInfoStabs=class(TDebugInfo)
  51. protected
  52. dbgtype: tdbg;
  53. stabsdir: TStabType;
  54. def_stab,
  55. regvar_stab,
  56. procdef_stab,
  57. constsym_stab,
  58. typesym_stab,
  59. globalvarsym_uninited_stab,
  60. globalvarsym_inited_stab,
  61. staticvarsym_uninited_stab,
  62. staticvarsym_inited_stab,
  63. localvarsymref_stab,
  64. paravarsymref_stab: byte;
  65. writing_def_stabs : boolean;
  66. global_stab_number : word;
  67. vardatadef: trecorddef;
  68. tagtypeprefix: ansistring;
  69. { tsym writing }
  70. function sym_var_value(const s:string;arg:pointer):string;
  71. function sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):ansistring;
  72. procedure write_sym_stabstr(list:TAsmList;sym:tsym;const ss:ansistring);
  73. function staticvarsym_mangled_name(sym: tstaticvarsym):string;virtual;
  74. procedure maybe_add_vmt_sym(list:TAsmList;def: tobjectdef);virtual;
  75. { tdef writing }
  76. function def_stab_number(def:tdef):string;
  77. function def_stab_classnumber(def:tabstractrecorddef):string;
  78. function def_var_value(const s:string;arg:pointer):string;
  79. function def_stabstr_evaluate(def:tdef;const s:string;const vars:array of string):ansistring;
  80. procedure write_def_stabstr(list:TAsmList;def:tdef;const ss:ansistring);virtual;
  81. procedure field_add_stabstr(p:TObject;arg:pointer);
  82. procedure method_add_stabstr(p:TObject;arg:pointer);
  83. procedure field_write_defs(p:TObject;arg:pointer);
  84. function get_enum_defstr(def: tenumdef; lowerbound: longint): ansistring;
  85. function get_appendsym_paravar_reg(sym:tparavarsym;const typ,stabstr:string;reg: tregister): ansistring;
  86. function base_stabs_str(typ: longint; const other, desc, value: ansistring): ansistring;overload;
  87. function base_stabs_str(const typ, other, desc, value: ansistring): ansistring;overload;virtual;
  88. function gen_procdef_startsym_stabs(def: tprocdef): TAsmList;virtual;
  89. function gen_procdef_endsym_stabs(def: tprocdef): TAsmList;virtual;
  90. protected
  91. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
  92. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
  93. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
  94. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
  95. procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
  96. procedure appendsym_type(list:TAsmList;sym:ttypesym);override;
  97. procedure appendsym_label(list:TAsmList;sym:tlabelsym);override;
  98. procedure beforeappenddef(list:TAsmList;def:tdef);override;
  99. procedure appenddef_ord(list:TAsmList;def:torddef);override;
  100. procedure appenddef_float(list:TAsmList;def:tfloatdef);override;
  101. procedure appenddef_file(list:TAsmList;def:tfiledef);override;
  102. procedure appenddef_enum(list:TAsmList;def:tenumdef);override;
  103. procedure appenddef_array(list:TAsmList;def:tarraydef);override;
  104. procedure appenddef_record(list:TAsmList;def:trecorddef);override;
  105. procedure appenddef_object(list:TAsmList;def:tobjectdef);override;
  106. procedure appenddef_pointer(list:TAsmList;def:tpointerdef);override;
  107. procedure appenddef_string(list:TAsmList;def:tstringdef);override;
  108. procedure appenddef_procvar(list:TAsmList;def:tprocvardef);override;
  109. procedure appenddef_variant(list:TAsmList;def:tvariantdef);override;
  110. procedure appenddef_set(list:TAsmList;def:tsetdef);override;
  111. procedure appenddef_formal(list:TAsmList;def:tformaldef);override;
  112. procedure appenddef_undefined(list:TAsmList;def: tundefineddef);override;
  113. procedure appendprocdef(list:TAsmList;def:tprocdef);override;
  114. public
  115. procedure inserttypeinfo;override;
  116. procedure insertmoduleinfo;override;
  117. procedure insertlineinfo(list:TAsmList);override;
  118. procedure referencesections(list:TAsmList);override;
  119. constructor Create;override;
  120. end;
  121. function GetSymTableName(SymTable : TSymTable) : string;
  122. const
  123. tagtypes = [
  124. recorddef,
  125. variantdef,
  126. enumdef,
  127. stringdef,
  128. filedef,
  129. objectdef
  130. ];
  131. implementation
  132. uses
  133. SysUtils,cutils,cfileutl,
  134. globals,globtype,verbose,constexp,
  135. defutil,
  136. cpuinfo,cpubase,paramgr,
  137. aasmbase,procinfo,
  138. finput,fmodule,ppu;
  139. function GetSymName(Sym : TSymEntry) : string;
  140. begin
  141. if Not (cs_stabs_preservecase in current_settings.globalswitches) then
  142. result := Sym.Name
  143. else
  144. result := Sym.RealName;
  145. if target_asm.dollarsign<>'$' then
  146. result:=ReplaceForbiddenAsmSymbolChars(result);
  147. end;
  148. function GetSymTableName(SymTable : TSymTable) : string;
  149. begin
  150. if Not (cs_stabs_preservecase in current_settings.globalswitches) then
  151. result := SymTable.Name^
  152. else
  153. result := SymTable.RealName^;
  154. if target_asm.dollarsign<>'$' then
  155. result:=ReplaceForbiddenAsmSymbolChars(result);
  156. end;
  157. const
  158. memsizeinc = 512;
  159. type
  160. get_var_value_proc=function(const s:string;arg:pointer):string of object;
  161. function string_evaluate(s:string;get_var_value:get_var_value_proc;get_var_value_arg:pointer;const vars:array of string):ansistring;
  162. (*
  163. S contains a prototype of a result. Stabstr_evaluate will expand
  164. variables and parameters.
  165. Output is s in ASCIIZ format, with the following expanded:
  166. ${varname} - The variable name is expanded.
  167. $n - The parameter n is expanded.
  168. $$ - Is expanded to $
  169. *)
  170. const maxvalue=9;
  171. maxdata=1023;
  172. var i,j:byte;
  173. varname:string[63];
  174. varno,varcounter:byte;
  175. varvalues:array[0..9] of pshortstring;
  176. {1 kb of parameters is the limit. 256 extra bytes are allocated to
  177. ensure buffer integrity.}
  178. varvaluedata:array[0..maxdata+256] of char;
  179. varptr:Pchar;
  180. varidx : byte;
  181. len:longint;
  182. r:Pchar;
  183. begin
  184. {Two pass approach, first, calculate the length and receive variables.}
  185. i:=1;
  186. len:=0;
  187. varcounter:=0;
  188. varptr:=@varvaluedata[0];
  189. while i<=length(s) do
  190. begin
  191. if (s[i]='$') and (i<length(s)) then
  192. begin
  193. if s[i+1]='$' then
  194. begin
  195. inc(len);
  196. inc(i);
  197. end
  198. else if (s[i+1]='{') and (length(s)>2) and (i<length(s)-2) then
  199. begin
  200. varname:='';
  201. inc(i,2);
  202. repeat
  203. inc(varname[0]);
  204. varname[length(varname)]:=s[i];
  205. s[i]:=char(varcounter);
  206. inc(i);
  207. until s[i]='}';
  208. varvalues[varcounter]:=pshortstring(varptr);
  209. if varptr>@varvaluedata[maxdata] then
  210. internalerrorproc(200411152);
  211. pshortstring(varptr)^:=get_var_value(varname,get_var_value_arg);
  212. inc(len,length(pshortstring(varptr)^));
  213. inc(varptr,length(pshortstring(varptr)^)+1);
  214. inc(varcounter);
  215. end
  216. else if s[i+1] in ['1'..'9'] then
  217. begin
  218. varidx:=byte(s[i+1])-byte('1');
  219. if varidx>high(vars) then
  220. internalerror(200509263);
  221. inc(len,length(vars[varidx]));
  222. inc(i);
  223. end;
  224. end
  225. else
  226. inc(len);
  227. inc(i);
  228. end;
  229. {Second pass, writeout result.}
  230. setlength(result,len);
  231. r:=pchar(result);
  232. i:=1;
  233. while i<=length(s) do
  234. begin
  235. if (s[i]='$') and (i<length(s)) then
  236. begin
  237. if s[i+1]='$' then
  238. begin
  239. r^:='$';
  240. inc(r);
  241. inc(i);
  242. end
  243. else if (s[i+1]='{') and (length(s)>2) and (i<length(s)-2) then
  244. begin
  245. varname:='';
  246. inc(i,2);
  247. varno:=byte(s[i]);
  248. repeat
  249. inc(i);
  250. until s[i]='}';
  251. for j:=1 to length(varvalues[varno]^) do
  252. begin
  253. r^:=varvalues[varno]^[j];
  254. inc(r);
  255. end;
  256. end
  257. else if s[i+1] in ['0'..'9'] then
  258. begin
  259. for j:=1 to length(vars[byte(s[i+1])-byte('1')]) do
  260. begin
  261. r^:=vars[byte(s[i+1])-byte('1')][j];
  262. inc(r);
  263. end;
  264. inc(i);
  265. end
  266. end
  267. else
  268. begin
  269. r^:=s[i];
  270. inc(r);
  271. end;
  272. inc(i);
  273. end;
  274. { verify that the length was correct }
  275. if r^<>#0 then
  276. internalerror(200802031);
  277. end;
  278. {****************************************************************************
  279. TDef support
  280. ****************************************************************************}
  281. function TDebugInfoStabs.def_stab_number(def:tdef):string;
  282. begin
  283. { procdefs only need a number, mark them as already written
  284. so they won't be written implicitly }
  285. if (def.typ=procdef) then
  286. def.dbg_state:=dbg_state_written;
  287. { Stab must already be written, or we must be busy writing it }
  288. if writing_def_stabs and
  289. not(def.dbg_state in [dbg_state_writing,dbg_state_written,dbg_state_queued]) then
  290. internalerror(200403091);
  291. { Keep track of used stabs, this info is only useful for stabs
  292. referenced by the symbols. Definitions will always include all
  293. required stabs }
  294. if def.dbg_state=dbg_state_unused then
  295. begin
  296. def.dbg_state:=dbg_state_used;
  297. deftowritelist.Add(def);
  298. end;
  299. { Need a new number? }
  300. if def.stab_number=0 then
  301. begin
  302. inc(global_stab_number);
  303. { classes require 2 numbers }
  304. if is_class(def) then
  305. inc(global_stab_number);
  306. def.stab_number:=global_stab_number;
  307. if global_stab_number>=defnumberlist.count then
  308. defnumberlist.count:=global_stab_number+250;
  309. defnumberlist[global_stab_number]:=def;
  310. end;
  311. result:=tostr(def.stab_number);
  312. end;
  313. function TDebugInfoStabs.def_stab_classnumber(def:tabstractrecorddef):string;
  314. begin
  315. if def.stab_number=0 then
  316. def_stab_number(def);
  317. if (def.typ=objectdef) and (tobjectdef(def).objecttype=odt_class) then
  318. result:=tostr(def.stab_number-1)
  319. else
  320. result:=tostr(def.stab_number);
  321. end;
  322. function TDebugInfoStabs.def_var_value(const s:string;arg:pointer):string;
  323. var
  324. def : tdef;
  325. begin
  326. def:=tdef(arg);
  327. result:='';
  328. if s='numberstring' then
  329. result:=def_stab_number(def)
  330. else if s='sym_name' then
  331. begin
  332. if assigned(def.typesym) then
  333. result:=GetSymName(Ttypesym(def.typesym));
  334. end
  335. else if s='savesize' then
  336. result:=tostr(def.size);
  337. end;
  338. function TDebugInfoStabs.def_stabstr_evaluate(def:tdef;const s:string;const vars:array of string):ansistring;
  339. begin
  340. result:=string_evaluate(s,@def_var_value,def,vars);
  341. end;
  342. procedure TDebugInfoStabs.field_add_stabstr(p:TObject;arg:pointer);
  343. var
  344. spec : string[3];
  345. varsize : asizeint;
  346. newss : ansistring;
  347. ss : pansistring absolute arg;
  348. begin
  349. if (tsym(p).visibility=vis_hidden) then
  350. exit;
  351. { static variables from objects are like global objects }
  352. if (Tsym(p).typ=fieldvarsym) and
  353. not(sp_static in Tsym(p).symoptions) then
  354. begin
  355. case tsym(p).visibility of
  356. vis_private,
  357. vis_strictprivate :
  358. spec:='/0';
  359. vis_protected,
  360. vis_strictprotected :
  361. spec:='/1';
  362. else
  363. spec:='';
  364. end;
  365. if (tabstractrecordsymtable(tsym(p).owner).usefieldalignment<>bit_alignment) then
  366. begin
  367. varsize:=tfieldvarsym(p).vardef.size;
  368. { open arrays made overflows !! }
  369. { how can a record/object/class contain an open array? (JM) }
  370. {$ifdef cpu16bitaddr}
  371. if varsize>$fff then
  372. varsize:=$fff;
  373. {$else cpu16bitaddr}
  374. if varsize>$fffffff then
  375. varsize:=$fffffff;
  376. {$endif cpu16bitaddr}
  377. newss:=def_stabstr_evaluate(nil,'$1:$2,$3,$4;',[GetSymName(tfieldvarsym(p)),
  378. spec+def_stab_number(tfieldvarsym(p).vardef),
  379. tostr(TConstExprInt(tfieldvarsym(p).fieldoffset)*8),tostr(varsize*8)])
  380. end
  381. else
  382. newss:=def_stabstr_evaluate(nil,'$1:$2,$3,$4;',[GetSymName(tfieldvarsym(p)),
  383. spec+def_stab_number(tfieldvarsym(p).vardef),
  384. tostr(TConstExprInt(tfieldvarsym(p).fieldoffset)),tostr(tfieldvarsym(p).vardef.packedbitsize)]);
  385. ss^:=ss^+newss;
  386. end;
  387. end;
  388. procedure TDebugInfoStabs.method_add_stabstr(p:TObject;arg:pointer);
  389. var
  390. virtualind,argnames : string;
  391. pd : tprocdef;
  392. lindex : longint;
  393. arglength : byte;
  394. sp : char;
  395. i : integer;
  396. parasym : tparavarsym;
  397. newss : ansistring;
  398. ss : pansistring absolute arg;
  399. begin
  400. if tsym(p).typ = procsym then
  401. begin
  402. pd :=tprocdef(tprocsym(p).ProcdefList[0]);
  403. if (po_virtualmethod in pd.procoptions) and
  404. not is_objectpascal_helper(pd.struct) then
  405. begin
  406. lindex := pd.extnumber;
  407. {doesnt seem to be necessary
  408. lindex := lindex or $80000000;}
  409. virtualind := '*'+tostr(lindex)+';'+def_stab_classnumber(pd.struct)+';'
  410. end
  411. else
  412. virtualind := '.';
  413. { used by gdbpas to recognize constructor and destructors }
  414. if (pd.proctypeoption=potype_constructor) then
  415. argnames:='__ct__'
  416. else if (pd.proctypeoption=potype_destructor) then
  417. argnames:='__dt__'
  418. else
  419. argnames := '';
  420. { arguments are not listed here }
  421. {we don't need another definition}
  422. for i:=0 to pd.paras.count-1 do
  423. begin
  424. parasym:=tparavarsym(pd.paras[i]);
  425. if Parasym.vardef.typ = formaldef then
  426. begin
  427. case Parasym.varspez of
  428. vs_var :
  429. argnames := argnames+'3var';
  430. vs_const :
  431. argnames:=argnames+'5const';
  432. vs_out :
  433. argnames:=argnames+'3out';
  434. vs_constref :
  435. argnames:=argnames+'8constref';
  436. end;
  437. end
  438. else
  439. begin
  440. { if the arg definition is like (v: ^byte;..
  441. there is no sym attached to data !!! }
  442. if assigned(Parasym.vardef.typesym) then
  443. begin
  444. arglength := length(GetSymName(Parasym.vardef.typesym));
  445. argnames := argnames + tostr(arglength)+GetSymName(Parasym.vardef.typesym);
  446. end
  447. else
  448. argnames:=argnames+'11unnamedtype';
  449. end;
  450. end;
  451. { here 2A must be changed for private and protected }
  452. { 0 is private 1 protected and 2 public }
  453. case tsym(p).visibility of
  454. vis_private,
  455. vis_strictprivate :
  456. sp:='0';
  457. vis_protected,
  458. vis_strictprotected :
  459. sp:='1'
  460. else
  461. sp:='2';
  462. end;
  463. newss:=def_stabstr_evaluate(nil,'$1::$2=##$3;:$4;$5A$6;',[GetSymName(tsym(p)),def_stab_number(pd),
  464. def_stab_number(pd.returndef),argnames,sp,
  465. virtualind]);
  466. ss^:=ss^+newss;
  467. end;
  468. end;
  469. procedure TDebugInfoStabs.field_write_defs(p:TObject;arg:pointer);
  470. begin
  471. if (Tsym(p).typ=fieldvarsym) and
  472. not(sp_static in Tsym(p).symoptions) then
  473. appenddef(TAsmList(arg),tfieldvarsym(p).vardef);
  474. end;
  475. procedure TDebugInfoStabs.write_def_stabstr(list:TAsmList;def:tdef;const ss:ansistring);
  476. var
  477. stabchar : string[2];
  478. symname : string[20];
  479. st : ansistring;
  480. begin
  481. { type prefix }
  482. if def.typ in tagtypes then
  483. stabchar := tagtypeprefix
  484. else
  485. stabchar := 't';
  486. { in case of writing the class record structure, we always have to
  487. use the class name (so it refers both to the struct and the
  488. pointer to the struct), otherwise gdb crashes (see tests/webtbs/tw9766.pp) }
  489. if is_class(def) and
  490. tobjectdef(def).writing_class_record_dbginfo then
  491. st:=def_stabstr_evaluate(def,'"${sym_name}:$1$2=',[stabchar,def_stab_classnumber(tobjectdef(def))])
  492. else
  493. begin
  494. { Type names for types defined in the current unit are already written in
  495. the typesym }
  496. if (def.owner.symtabletype=globalsymtable) and
  497. not(def.owner.iscurrentunit) then
  498. symname:='${sym_name}'
  499. else
  500. symname:='';
  501. st:=def_stabstr_evaluate(def,'"'+symname+':$1$2=',[stabchar,def_stab_number(def)]);
  502. end;
  503. st:=st+ss;
  504. { line info is set to 0 for all defs, because the def can be in another
  505. unit and then the linenumber is invalid in the current sourcefile }
  506. st:=st+def_stabstr_evaluate(def,'",'+base_stabs_str(def_stab,'0','0','0'),[]);
  507. { add to list }
  508. list.concat(Tai_stab.create_ansistr(stabsdir,st));
  509. end;
  510. procedure TDebugInfoStabs.appenddef_string(list:TAsmList;def:tstringdef);
  511. var
  512. bytest,charst,longst : string;
  513. ss : ansistring;
  514. slen : longint;
  515. begin
  516. ss:='';
  517. case def.stringtype of
  518. st_shortstring:
  519. begin
  520. { fix length of openshortstring }
  521. slen:=def.len;
  522. if slen=0 then
  523. slen:=255;
  524. charst:=def_stab_number(cansichartype);
  525. bytest:=def_stab_number(u8inttype);
  526. ss:=def_stabstr_evaluate(def,'s$1length:$2,0,8;st:ar$2;1;$3;$4,8,$5;;',
  527. [tostr(slen+1),bytest,tostr(slen),charst,tostr(slen*8)]);
  528. end;
  529. st_longstring:
  530. begin
  531. charst:=def_stab_number(cansichartype);
  532. bytest:=def_stab_number(u8inttype);
  533. longst:=def_stab_number(u32inttype);
  534. ss:=def_stabstr_evaluate(def,'s$1length:$2,0,32;dummy:$6,32,8;st:ar$2;1;$3;$4,40,$5;;',
  535. [tostr(def.len+5),longst,tostr(def.len),charst,tostr(def.len*8),bytest]);
  536. end;
  537. st_ansistring:
  538. begin
  539. { looks like a pchar }
  540. ss:='*'+def_stab_number(cansichartype);
  541. end;
  542. st_unicodestring,
  543. st_widestring:
  544. begin
  545. { looks like a pwidechar }
  546. ss:='*'+def_stab_number(cwidechartype);
  547. end;
  548. end;
  549. write_def_stabstr(list,def,ss);
  550. end;
  551. function TDebugInfoStabs.get_enum_defstr(def: tenumdef; lowerbound: longint): ansistring;
  552. var
  553. i: longint;
  554. p: tenumsym;
  555. begin
  556. { we can specify the size with @s<size>; prefix PM }
  557. if def.size <> std_param_align then
  558. result:='@s'+tostr(def.size*8)+';e'
  559. else
  560. result:='e';
  561. { the if-test is required because pred(def.minval) might overflow;
  562. the longint() typecast should be safe because stabs is not
  563. supported for 64 bit targets }
  564. if (def.minval<>lowerbound) then
  565. for i:=lowerbound to pred(longint(def.minval)) do
  566. result:=result+'<invalid>:'+tostr(i)+',';
  567. for i := 0 to def.symtable.SymList.Count - 1 do
  568. begin
  569. p := tenumsym(def.symtable.SymList[i]);
  570. if p.value<def.minval then
  571. continue
  572. else
  573. if p.value>def.maxval then
  574. break;
  575. result:=result+GetSymName(p)+':'+tostr(p.value)+',';
  576. end;
  577. { the final ',' is required to have a valid stabs }
  578. result:=result+';';
  579. end;
  580. procedure TDebugInfoStabs.appenddef_enum(list:TAsmList;def:tenumdef);
  581. begin
  582. write_def_stabstr(list,def,get_enum_defstr(def,def.minval));
  583. end;
  584. procedure TDebugInfoStabs.appenddef_ord(list:TAsmList;def:torddef);
  585. var
  586. ss : ansistring;
  587. begin
  588. ss:='';
  589. if cs_gdb_valgrind in current_settings.globalswitches then
  590. begin
  591. case def.ordtype of
  592. uvoid :
  593. ss:=def_stab_number(def);
  594. pasbool8,
  595. pasbool16,
  596. pasbool32,
  597. pasbool64,
  598. bool8bit,
  599. bool16bit,
  600. bool32bit,
  601. bool64bit :
  602. ss:=def_stabstr_evaluate(def,'r${numberstring};0;255;',[]);
  603. u32bit,
  604. s64bit,
  605. u64bit :
  606. ss:=def_stabstr_evaluate(def,'r${numberstring};0;-1;',[]);
  607. else
  608. ss:=def_stabstr_evaluate(def,'r${numberstring};$1;$2;',[tostr(longint(def.low.svalue)),tostr(longint(def.high.svalue))]);
  609. end;
  610. end
  611. else
  612. begin
  613. case def.ordtype of
  614. uvoid :
  615. ss:=def_stab_number(def);
  616. uchar :
  617. ss:='-20;';
  618. uwidechar :
  619. ss:='-30;';
  620. pasbool8,
  621. bool8bit :
  622. ss:='-21;';
  623. pasbool16,
  624. bool16bit :
  625. ss:='-22;';
  626. pasbool32,
  627. bool32bit :
  628. ss:='-23;';
  629. pasbool64,
  630. bool64bit :
  631. { no clue if this is correct (FK) }
  632. ss:='-23;';
  633. u64bit :
  634. ss:='-32;';
  635. s64bit :
  636. ss:='-31;';
  637. {u32bit : result:=def_stab_number(s32inttype)+';0;-1;'); }
  638. else
  639. ss:=def_stabstr_evaluate(def,'r${numberstring};$1;$2;',[tostr(longint(def.low.svalue)),tostr(longint(def.high.svalue))]);
  640. end;
  641. end;
  642. write_def_stabstr(list,def,ss);
  643. end;
  644. procedure TDebugInfoStabs.appenddef_float(list:TAsmList;def:tfloatdef);
  645. var
  646. ss : ansistring;
  647. begin
  648. ss:='';
  649. case def.floattype of
  650. s32real,
  651. s64real,
  652. s80real,
  653. sc80real:
  654. ss:=def_stabstr_evaluate(def,'r$1;${savesize};0;',[def_stab_number(s32inttype)]);
  655. s64currency,
  656. s64comp:
  657. ss:=def_stabstr_evaluate(def,'r$1;-${savesize};0;',[def_stab_number(s32inttype)]);
  658. else
  659. internalerror(200509261);
  660. end;
  661. write_def_stabstr(list,def,ss);
  662. end;
  663. procedure TDebugInfoStabs.appenddef_file(list:TAsmList;def:tfiledef);
  664. var
  665. ss : ansistring;
  666. begin
  667. {$ifdef cpu64bitaddr}
  668. ss:=def_stabstr_evaluate(def,'s${savesize}HANDLE:$1,0,32;MODE:$1,32,32;RECSIZE:$2,64,64;'+
  669. '_PRIVATE:ar$1;1;64;$3,128,256;USERDATA:ar$1;1;16;$3,384,128;'+
  670. 'NAME:ar$1;0;255;$4,512,2048;;',[def_stab_number(s32inttype),
  671. def_stab_number(s64inttype),
  672. def_stab_number(u8inttype),
  673. def_stab_number(cansichartype)]);
  674. {$else cpu64bitaddr}
  675. ss:=def_stabstr_evaluate(def,'s${savesize}HANDLE:$1,0,32;MODE:$1,32,32;RECSIZE:$1,64,32;'+
  676. '_PRIVATE:ar$1;1;32;$3,96,256;USERDATA:ar$1;1;16;$2,352,128;'+
  677. 'NAME:ar$1;0;255;$3,480,2048;;',[def_stab_number(s32inttype),
  678. def_stab_number(u8inttype),
  679. def_stab_number(cansichartype)]);
  680. {$endif cpu64bitaddr}
  681. write_def_stabstr(list,def,ss);
  682. end;
  683. procedure TDebugInfoStabs.appenddef_record(list:TAsmList;def:trecorddef);
  684. var
  685. ss : ansistring;
  686. begin
  687. ss:='s'+tostr(def.size);
  688. def.symtable.SymList.ForEachCall(@field_add_stabstr,@ss);
  689. ss[length(ss)]:=';';
  690. write_def_stabstr(list,def,ss);
  691. end;
  692. procedure TDebugInfoStabs.appenddef_object(list:TAsmList;def:tobjectdef);
  693. procedure do_write_object(list:TAsmList;def:tobjectdef);
  694. var
  695. ss : ansistring;
  696. anc : tobjectdef;
  697. begin
  698. ss:='';
  699. { Write the invisible pointer for the class? }
  700. if (def.objecttype=odt_class) and
  701. (not def.writing_class_record_dbginfo) then
  702. begin
  703. ss:='*'+def_stab_classnumber(def);
  704. write_def_stabstr(list,def,ss);
  705. exit;
  706. end;
  707. ss:='s'+tostr(tobjecTSymtable(def.symtable).datasize);
  708. if assigned(def.childof) then
  709. begin
  710. {only one ancestor not virtual, public, at base offset 0 }
  711. { !1 , 0 2 0 , }
  712. ss:=ss+'!1,020,'+def_stab_classnumber(def.childof)+';';
  713. end;
  714. {virtual table to implement yet}
  715. def.symtable.symList.ForEachCall(@field_add_stabstr,@ss);
  716. if (oo_has_vmt in def.objectoptions) and
  717. (
  718. not assigned(def.childof) or
  719. not(oo_has_vmt in def.childof.objectoptions)
  720. ) then
  721. ss:=ss+'$vf'+def_stab_classnumber(def)+':'+def_stab_number(vmtarraytype)+','+tostr(def.vmt_offset*8)+';';
  722. def.symtable.symList.ForEachCall(@method_add_stabstr,@ss);
  723. if (oo_has_vmt in def.objectoptions) then
  724. begin
  725. anc := def;
  726. while assigned(anc.childof) and (oo_has_vmt in anc.childof.objectoptions) do
  727. anc := anc.childof;
  728. { just in case anc = self }
  729. ss:=ss+';~%'+def_stab_classnumber(anc)+';';
  730. end
  731. else
  732. ss:=ss+';';
  733. write_def_stabstr(list,def,ss);
  734. end;
  735. var
  736. oldtypesym : tsym;
  737. begin
  738. { classes require special code to write the record and the invisible pointer }
  739. if is_class(def) then
  740. begin
  741. { Write the record class itself }
  742. tobjectdef(def).writing_class_record_dbginfo:=true;
  743. do_write_object(list,def);
  744. tobjectdef(def).writing_class_record_dbginfo:=false;
  745. { Write the invisible pointer class }
  746. oldtypesym:=def.typesym;
  747. def.typesym:=nil;
  748. do_write_object(list,def);
  749. def.typesym:=oldtypesym;
  750. end
  751. else
  752. do_write_object(list,def);
  753. { VMT symbol }
  754. maybe_add_vmt_sym(list,def);
  755. end;
  756. procedure TDebugInfoStabs.appenddef_variant(list:TAsmList;def:tvariantdef);
  757. var
  758. ss : ansistring;
  759. begin
  760. if not assigned(vardatadef) then
  761. exit;
  762. ss:='s'+tostr(vardatadef.size);
  763. vardatadef.symtable.SymList.ForEachCall(@field_add_stabstr,@ss);
  764. ss[length(ss)]:=';';
  765. write_def_stabstr(list,def,ss);
  766. end;
  767. procedure TDebugInfoStabs.appenddef_pointer(list:TAsmList;def:tpointerdef);
  768. var
  769. ss : ansistring;
  770. begin
  771. ss:='*'+def_stab_number(tpointerdef(def).pointeddef);
  772. write_def_stabstr(list,def,ss);
  773. end;
  774. procedure TDebugInfoStabs.appenddef_set(list:TAsmList;def:tsetdef);
  775. var
  776. st,
  777. ss : ansistring;
  778. elementdefstabnr: string;
  779. begin
  780. { ugly hack: create a temporary subrange type if the lower bound of
  781. the set's element type is not a multiple of 8 (because we store them
  782. as if the lower bound is a multiple of 8) }
  783. if (def.setbase<>get_min_value(def.elementdef)) then
  784. begin
  785. { allocate a def number }
  786. inc(global_stab_number);
  787. elementdefstabnr:=tostr(global_stab_number);
  788. { anonymous subrange def }
  789. st:='":t'+elementdefstabnr+'=';
  790. if (def.elementdef.typ = enumdef) then
  791. st:=st+get_enum_defstr(tenumdef(def.elementdef),def.setbase)
  792. else
  793. st:=st+def_stabstr_evaluate(def.elementdef,'r'+elementdefstabnr+';$1;$2;',[tostr(longint(def.setbase)),tostr(longint(get_max_value(def.elementdef).svalue))]);
  794. st:=st+'",'+base_stabs_str(def_stab,'0','0','0');
  795. { add to list }
  796. list.concat(Tai_stab.create_ansistr(stabsdir,st));
  797. end
  798. else
  799. elementdefstabnr:=def_stab_number(def.elementdef);
  800. ss:=def_stabstr_evaluate(def,'@s$1;S$2',[tostr(def.size*8),elementdefstabnr]);
  801. write_def_stabstr(list,def,ss);
  802. end;
  803. procedure TDebugInfoStabs.appenddef_formal(list:TAsmList;def:tformaldef);
  804. var
  805. ss : ansistring;
  806. begin
  807. ss:=def_stabstr_evaluate(def,'${numberstring};',[]);
  808. write_def_stabstr(list,def,ss);
  809. end;
  810. procedure TDebugInfoStabs.appenddef_array(list:TAsmList;def:tarraydef);
  811. var
  812. tempstr: shortstring;
  813. ss : ansistring;
  814. begin
  815. if not is_packed_array(def) then
  816. begin
  817. tempstr:='ar$1;$2;$3;$4';
  818. if is_dynamic_array(def) then
  819. tempstr:='*'+tempstr;
  820. ss:=def_stabstr_evaluate(def,tempstr,[def_stab_number(tarraydef(def).rangedef),
  821. tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange),def_stab_number(tarraydef(def).elementdef)])
  822. end
  823. else
  824. begin
  825. // the @P seems to be ignored by gdb
  826. tempstr:=def_stabstr_evaluate(tarraydef(def).rangedef,'r${numberstring};$1;$2;',
  827. [tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange)]);
  828. // will only show highrange-lowrange+1 bits in gdb
  829. ss:=def_stabstr_evaluate(def,'@s$1;@S;S$2',
  830. [tostr(TConstExprInt(tarraydef(def).elepackedbitsize) * tarraydef(def).elecount),tempstr]);
  831. end;
  832. write_def_stabstr(list,def,ss);
  833. end;
  834. procedure TDebugInfoStabs.appenddef_procvar(list:TAsmList;def:tprocvardef);
  835. var
  836. ss : ansistring;
  837. begin
  838. ss:='*f'+def_stab_number(tprocvardef(def).returndef);
  839. write_def_stabstr(list,def,ss);
  840. end;
  841. procedure TDebugInfoStabs.appenddef_undefined(list:TAsmList;def:tundefineddef);
  842. var
  843. ss : ansistring;
  844. begin
  845. ss:=def_stabstr_evaluate(def,'${numberstring};',[]);
  846. write_def_stabstr(list,def,ss);
  847. end;
  848. procedure TDebugInfoStabs.beforeappenddef(list:TAsmList;def:tdef);
  849. var
  850. anc : tobjectdef;
  851. i : longint;
  852. begin
  853. { write dependencies first }
  854. case def.typ of
  855. stringdef :
  856. begin
  857. if tstringdef(def).stringtype in [st_widestring,st_unicodestring] then
  858. appenddef(list,cwidechartype)
  859. else
  860. begin
  861. appenddef(list,cansichartype);
  862. appenddef(list,u8inttype);
  863. end;
  864. end;
  865. floatdef :
  866. appenddef(list,s32inttype);
  867. filedef :
  868. begin
  869. appenddef(list,s32inttype);
  870. {$ifdef cpu64bitaddr}
  871. appenddef(list,s64inttype);
  872. {$endif cpu64bitaddr}
  873. appenddef(list,u8inttype);
  874. appenddef(list,cansichartype);
  875. end;
  876. classrefdef :
  877. appenddef(list,pvmttype);
  878. pointerdef :
  879. appenddef(list,tpointerdef(def).pointeddef);
  880. setdef :
  881. appenddef(list,tsetdef(def).elementdef);
  882. procvardef :
  883. begin
  884. appenddef(list,tprocvardef(def).returndef);
  885. if assigned(tprocvardef(def).parast) then
  886. write_symtable_defs(list,tprocvardef(def).parast);
  887. end;
  888. procdef :
  889. begin
  890. appenddef(list,tprocdef(def).returndef);
  891. if assigned(tprocdef(def).parast) then
  892. write_symtable_defs(list,tprocdef(def).parast);
  893. if assigned(tprocdef(def).localst) and
  894. (tprocdef(def).localst.symtabletype=localsymtable) then
  895. write_symtable_defs(list,tprocdef(def).localst);
  896. end;
  897. arraydef :
  898. begin
  899. appenddef(list,tarraydef(def).rangedef);
  900. appenddef(list,tarraydef(def).elementdef);
  901. end;
  902. recorddef :
  903. trecorddef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  904. enumdef :
  905. if assigned(tenumdef(def).basedef) then
  906. appenddef(list,tenumdef(def).basedef);
  907. objectdef :
  908. begin
  909. { make sure we don't write child classdefs before their parent }
  910. { classdefs, because this crashes gdb }
  911. anc:=tobjectdef(def);
  912. while assigned(anc.childof) do
  913. begin
  914. anc:=anc.childof;
  915. if (anc.dbg_state=dbg_state_writing) then
  916. { happens in case a field of a parent is of the (forward }
  917. { defined) child type }
  918. begin
  919. { We don't explicitly requeue it, but the fact that }
  920. { a child type was used in a parent before the child }
  921. { type was fully defined means that it was forward }
  922. { declared, and will still be encountered later (it }
  923. { cannot have been declared in another unit, because }
  924. { then this and that other unit would depend on }
  925. { eachother's interface) }
  926. { Setting the state to queued however allows us to }
  927. { get the def number already without an IE }
  928. def.dbg_state:=dbg_state_queued;
  929. exit;
  930. end;
  931. end;
  932. appenddef(list,vmtarraytype);
  933. if assigned(tobjectdef(def).ImplementedInterfaces) then
  934. for i:=0 to tobjectdef(def).ImplementedInterfaces.Count-1 do
  935. appenddef(list,TImplementedInterface(tobjectdef(def).ImplementedInterfaces[i]).IntfDef);
  936. { first the parents }
  937. anc:=tobjectdef(def);
  938. while assigned(anc.childof) do
  939. begin
  940. anc:=anc.childof;
  941. appenddef(list,anc);
  942. if assigned(anc.ImplementedInterfaces) then
  943. for i:=0 to anc.ImplementedInterfaces.Count-1 do
  944. appenddef(list,TImplementedInterface(anc.ImplementedInterfaces[i]).IntfDef);
  945. end;
  946. tobjectdef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  947. end;
  948. end;
  949. end;
  950. procedure TDebugInfoStabs.appendprocdef(list:TAsmList;def:tprocdef);
  951. var
  952. hs : ansistring;
  953. templist : TAsmList;
  954. begin
  955. if not(def.in_currentunit) or
  956. { happens for init procdef of units without init section }
  957. not assigned(def.procstarttai) then
  958. exit;
  959. { mark as used so the local type defs also be written }
  960. def.dbg_state:=dbg_state_used;
  961. templist:=gen_procdef_endsym_stabs(def);
  962. current_asmdata.asmlists[al_procedures].insertlistafter(def.procendtai,templist);
  963. { FUNC stabs }
  964. templist.free;
  965. templist:=gen_procdef_startsym_stabs(def);
  966. current_asmdata.asmlists[al_procedures].insertlistbefore(def.procstarttai,templist);
  967. { para types }
  968. if assigned(def.parast) then
  969. write_symtable_syms(templist,def.parast);
  970. { local type defs and vars should not be written
  971. inside the main proc stab }
  972. if assigned(def.localst) and
  973. (def.localst.symtabletype=localsymtable) then
  974. write_symtable_syms(templist,def.localst);
  975. if assigned(def.funcretsym) and
  976. (tabstractnormalvarsym(def.funcretsym).refs>0) then
  977. begin
  978. if tabstractnormalvarsym(def.funcretsym).localloc.loc=LOC_REFERENCE then
  979. begin
  980. { TODO: Need to add gdb support for ret in param register calling}
  981. if paramanager.ret_in_param(def.returndef,def.proccalloption) then
  982. hs:='X*'
  983. else
  984. hs:='X';
  985. templist.concat(Tai_stab.create(stabsdir,strpnew(
  986. '"'+GetSymName(def.procsym)+':'+hs+def_stab_number(def.returndef)+'",'+
  987. base_stabs_str(localvarsymref_stab,'0','0',tostr(tabstractnormalvarsym(def.funcretsym).localloc.reference.offset)))));
  988. if (m_result in current_settings.modeswitches) then
  989. templist.concat(Tai_stab.create(stabsdir,strpnew(
  990. '"RESULT:'+hs+def_stab_number(def.returndef)+'",'+
  991. base_stabs_str(localvarsymref_stab,'0','0',tostr(tabstractnormalvarsym(def.funcretsym).localloc.reference.offset)))));
  992. end;
  993. end;
  994. current_asmdata.asmlists[al_procedures].insertlistbefore(def.procstarttai,templist);
  995. templist.free;
  996. end;
  997. {****************************************************************************
  998. TSym support
  999. ****************************************************************************}
  1000. function TDebugInfoStabs.sym_var_value(const s:string;arg:pointer):string;
  1001. var
  1002. sym : tsym absolute arg;
  1003. begin
  1004. result:='';
  1005. if s='name' then
  1006. result:=GetSymName(sym)
  1007. else if s='mangledname' then
  1008. result:=ReplaceForbiddenAsmSymbolChars(sym.mangledname)
  1009. else if s='ownername' then
  1010. result:=GetSymTableName(sym.owner)
  1011. else if s='line' then
  1012. result:=tostr(sym.fileinfo.line)
  1013. else
  1014. internalerror(200401152);
  1015. end;
  1016. function TDebugInfoStabs.sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):ansistring;
  1017. begin
  1018. result:=string_evaluate(s,@sym_var_value,sym,vars);
  1019. end;
  1020. procedure TDebugInfoStabs.write_sym_stabstr(list:TAsmList;sym:tsym;const ss:ansistring);
  1021. begin
  1022. if ss='' then
  1023. exit;
  1024. { add to list }
  1025. list.concat(Tai_stab.create_ansistr(stabsdir,ss));
  1026. end;
  1027. function TDebugInfoStabs.staticvarsym_mangled_name(sym: tstaticvarsym): string;
  1028. begin
  1029. result:=ReplaceForbiddenAsmSymbolChars(sym.mangledname);
  1030. end;
  1031. procedure TDebugInfoStabs.maybe_add_vmt_sym(list: TAsmList; def: tobjectdef);
  1032. begin
  1033. if (oo_has_vmt in def.objectoptions) and
  1034. assigned(def.owner) and
  1035. assigned(def.owner.name) then
  1036. list.concat(Tai_stab.create_ansistr(stabsdir,ansistring('"vmt_')+GetSymTableName(def.owner)+tobjectdef(def).objname^+':S'+
  1037. def_stab_number(vmttype)+'",'+
  1038. base_stabs_str(globalvarsym_inited_stab,'0','0',ReplaceForbiddenAsmSymbolChars(tobjectdef(def).vmt_mangledname))));
  1039. end;
  1040. procedure TDebugInfoStabs.appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);
  1041. var
  1042. ss : ansistring;
  1043. begin
  1044. ss:='';
  1045. if (sym.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1046. (sp_static in sym.symoptions) then
  1047. ss:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",'+base_stabs_str(globalvarsym_uninited_stab,'0','${line}','${mangledname}'),
  1048. [def_stab_number(sym.vardef)]);
  1049. write_sym_stabstr(list,sym,ss);
  1050. end;
  1051. procedure TDebugInfoStabs.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1052. var
  1053. ss : ansistring;
  1054. st : string;
  1055. threadvaroffset : string;
  1056. regidx : Tregisterindex;
  1057. nsym : byte;
  1058. begin
  1059. { external symbols can't be resolved at link time, so we
  1060. can't generate stabs for them }
  1061. if vo_is_external in sym.varoptions then
  1062. exit;
  1063. ss:='';
  1064. st:=def_stab_number(sym.vardef);
  1065. case sym.localloc.loc of
  1066. LOC_REGISTER,
  1067. LOC_CREGISTER,
  1068. LOC_MMREGISTER,
  1069. LOC_CMMREGISTER,
  1070. LOC_FPUREGISTER,
  1071. LOC_CFPUREGISTER :
  1072. begin
  1073. regidx:=findreg_by_number(sym.localloc.register);
  1074. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1075. { this is the register order for GDB}
  1076. if regidx<>0 then
  1077. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",'+base_stabs_str(regvar_stab,'0','${line}','$2'),[st,tostr(regstabs_table[regidx])]);
  1078. end;
  1079. else
  1080. begin
  1081. if (vo_is_thread_var in sym.varoptions) then
  1082. threadvaroffset:='+'+tostr(sizeof(pint))
  1083. else
  1084. threadvaroffset:='';
  1085. if (vo_is_typed_const in sym.varoptions) then
  1086. if vo_is_public in sym.varoptions then
  1087. nsym:=globalvarsym_inited_stab
  1088. else
  1089. nsym:=staticvarsym_inited_stab
  1090. else if vo_is_public in sym.varoptions then
  1091. nsym:=globalvarsym_uninited_stab
  1092. else
  1093. nsym:=staticvarsym_uninited_stab;
  1094. { Here we used S instead of
  1095. because with G GDB doesn't look at the address field
  1096. but searches the same name or with a leading underscore
  1097. but these names don't exist in pascal !}
  1098. st:='S'+st;
  1099. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(nsym,'0','${line}','$2$3'),[st,staticvarsym_mangled_name(sym),threadvaroffset]);
  1100. end;
  1101. end;
  1102. write_sym_stabstr(list,sym,ss);
  1103. end;
  1104. procedure TDebugInfoStabs.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  1105. var
  1106. ss : ansistring;
  1107. st : string;
  1108. regidx : Tregisterindex;
  1109. begin
  1110. { There is no space allocated for not referenced locals }
  1111. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1112. exit;
  1113. ss:='';
  1114. st:=def_stab_number(sym.vardef);
  1115. case sym.localloc.loc of
  1116. LOC_REGISTER,
  1117. LOC_CREGISTER,
  1118. LOC_MMREGISTER,
  1119. LOC_CMMREGISTER,
  1120. LOC_FPUREGISTER,
  1121. LOC_CFPUREGISTER :
  1122. begin
  1123. regidx:=findreg_by_number(sym.localloc.register);
  1124. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1125. { this is the register order for GDB}
  1126. if regidx<>0 then
  1127. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",'+base_stabs_str(regvar_stab,'0','${line}','$2'),[st,tostr(regstabs_table[regidx])]);
  1128. end;
  1129. LOC_REFERENCE :
  1130. { offset to ebp => will not work if the framepointer is esp
  1131. so some optimizing will make things harder to debug }
  1132. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(localvarsymref_stab,'0','${line}','$2'),[st,tostr(sym.localloc.reference.offset)])
  1133. else
  1134. internalerror(2003091814);
  1135. end;
  1136. write_sym_stabstr(list,sym,ss);
  1137. end;
  1138. function TDebugInfoStabs.get_appendsym_paravar_reg(sym:tparavarsym;const typ,stabstr:string;reg: tregister): ansistring;
  1139. var
  1140. ltyp: string[1];
  1141. regidx : Tregisterindex;
  1142. begin
  1143. result:='';
  1144. if typ='p' then
  1145. ltyp:='R'
  1146. else
  1147. ltyp:='a';
  1148. regidx:=findreg_by_number(reg);
  1149. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1150. { this is the register order for GDB}
  1151. if regidx<>0 then
  1152. result:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(regvar_stab,'0','${line}','$2'),[ltyp+stabstr,tostr(longint(regstabs_table[regidx]))]);
  1153. end;
  1154. function TDebugInfoStabs.base_stabs_str(typ: longint; const other, desc, value: ansistring): ansistring;
  1155. begin
  1156. result:=base_stabs_str(tostr(typ),other,desc,value);
  1157. end;
  1158. function TDebugInfoStabs.base_stabs_str(const typ, other, desc, value: ansistring): ansistring;
  1159. begin
  1160. result:=typ+','+other+','+desc+','+value
  1161. end;
  1162. function TDebugInfoStabs.gen_procdef_startsym_stabs(def: tprocdef): TAsmList;
  1163. var
  1164. RType : Char;
  1165. Obj,Info,
  1166. mangledname: ansistring;
  1167. begin
  1168. result:=TAsmList.create;
  1169. { "The stab representing a procedure is located immediately
  1170. following the code of the procedure. This stab is in turn
  1171. directly followed by a group of other stabs describing
  1172. elements of the procedure. These other stabs describe the
  1173. procedure's parameters, its block local variables, and its
  1174. block structure." (stab docs) }
  1175. { this is however incorrect in case "include source" statements }
  1176. { appear in the block, in that case the procedure stab must }
  1177. { appear before this include stabs (and we generate such an }
  1178. { stabs for all functions) (JM) }
  1179. obj := GetSymName(def.procsym);
  1180. info := '';
  1181. if (po_global in def.procoptions) then
  1182. RType := 'F'
  1183. else
  1184. RType := 'f';
  1185. if assigned(def.owner) then
  1186. begin
  1187. if (def.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1188. obj := GetSymTableName(def.owner)+'__'+GetSymName(def.procsym);
  1189. if not(cs_gdb_valgrind in current_settings.globalswitches) and
  1190. (def.owner.symtabletype=localsymtable) and
  1191. assigned(def.owner.defowner) and
  1192. assigned(tprocdef(def.owner.defowner).procsym) then
  1193. info := ','+GetSymName(def.procsym)+','+GetSymName(tprocdef(def.owner.defowner).procsym);
  1194. end;
  1195. mangledname:=ReplaceForbiddenAsmSymbolChars(def.mangledname);
  1196. if target_info.system in systems_dotted_function_names then
  1197. mangledname:='.'+mangledname;
  1198. result.concat(Tai_stab.Create_ansistr(stabsdir,'"'+obj+':'+RType+def_stab_number(def.returndef)+info+'",'+
  1199. base_stabs_str(procdef_stab,'0',tostr(def.fileinfo.line),mangledname)));
  1200. end;
  1201. function TDebugInfoStabs.gen_procdef_endsym_stabs(def: tprocdef): TAsmList;
  1202. var
  1203. ss, mangledname: ansistring;
  1204. stabsendlabel: tasmlabel;
  1205. begin
  1206. result:=TAsmList.create;
  1207. { end of procedure }
  1208. current_asmdata.getlabel(stabsendlabel,alt_dbgtype);
  1209. if dbgtype<>dbg_stabx then
  1210. begin
  1211. mangledname:=def.mangledname;
  1212. if target_info.system in systems_dotted_function_names then
  1213. mangledname:='.'+mangledname;
  1214. // LBRAC
  1215. ss:=tostr(STABS_N_LBRAC)+',0,0,'+mangledname;
  1216. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1217. begin
  1218. ss:=ss+'-';
  1219. ss:=ss+mangledname;
  1220. end;
  1221. result.concat(Tai_stab.Create_ansistr(stab_stabn,ss));
  1222. // RBRAC
  1223. ss:=tostr(STABS_N_RBRAC)+',0,0,'+stabsendlabel.name;
  1224. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1225. begin
  1226. ss:=ss+'-';
  1227. ss:=ss+mangledname;
  1228. end;
  1229. result.concat(Tai_stab.Create_ansistr(stab_stabn,ss));
  1230. { the stabsendlabel must come after all other stabs for this }
  1231. { function }
  1232. result.concat(tai_label.create(stabsendlabel));
  1233. { Add a "size" stab as described in the last paragraph of 2.5 at }
  1234. { http://sourceware.org/gdb/current/onlinedocs/stabs_2.html#SEC12 }
  1235. { This works at least on Darwin (and is needed on Darwin to get }
  1236. { correct smartlinking of stabs), but I don't know which binutils }
  1237. { version is required on other platforms }
  1238. { This stab must come after all other stabs for the procedure, }
  1239. { including the LBRAC/RBRAC ones }
  1240. if (target_info.system in systems_darwin) then
  1241. result.concat(Tai_stab.create(stabsdir,
  1242. strpnew('"",'+base_stabs_str(procdef_stab,'0','0',stabsendlabel.name+'-'+mangledname))));
  1243. end;
  1244. end;
  1245. procedure TDebugInfoStabs.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  1246. var
  1247. ss : ansistring;
  1248. c : string[1];
  1249. st : string;
  1250. regidx : Tregisterindex;
  1251. begin
  1252. ss:='';
  1253. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1254. { while stabs aren't adapted for regvars yet }
  1255. if (vo_is_self in sym.varoptions) then
  1256. begin
  1257. case sym.localloc.loc of
  1258. LOC_REGISTER,
  1259. LOC_CREGISTER:
  1260. regidx:=findreg_by_number(sym.localloc.register);
  1261. LOC_REFERENCE: ;
  1262. else
  1263. internalerror(2003091815);
  1264. end;
  1265. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1266. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1267. begin
  1268. if (sym.localloc.loc=LOC_REFERENCE) then
  1269. ss:=sym_stabstr_evaluate(sym,'"pvmt:p$1",'+base_stabs_str(localvarsymref_stab,'0','0','$2'),
  1270. [def_stab_number(pvmttype),tostr(sym.localloc.reference.offset)])
  1271. else
  1272. begin
  1273. regidx:=findreg_by_number(sym.localloc.register);
  1274. ss:=sym_stabstr_evaluate(sym,'"pvmt:r$1",'+base_stabs_str(regvar_stab,'0','0','$2'),
  1275. [def_stab_number(pvmttype),tostr(regstabs_table[regidx])]);
  1276. end
  1277. end
  1278. else
  1279. begin
  1280. if not(is_class(tprocdef(sym.owner.defowner).struct)) then
  1281. c:='v'
  1282. else
  1283. c:='p';
  1284. if (sym.localloc.loc=LOC_REFERENCE) then
  1285. ss:=sym_stabstr_evaluate(sym,'"$$t:$1",'+base_stabs_str(localvarsymref_stab,'0','0','$2'),
  1286. [c+def_stab_number(tprocdef(sym.owner.defowner).struct),tostr(sym.localloc.reference.offset)])
  1287. else
  1288. begin
  1289. if (c='p') then
  1290. c:='R'
  1291. else
  1292. c:='a';
  1293. regidx:=findreg_by_number(sym.localloc.register);
  1294. ss:=sym_stabstr_evaluate(sym,'"$$t:$1",'+base_stabs_str(regvar_stab,'0','0','$2'),
  1295. [c+def_stab_number(tprocdef(sym.owner.defowner).struct),tostr(regstabs_table[regidx])]);
  1296. end
  1297. end;
  1298. end
  1299. else
  1300. begin
  1301. st:=def_stab_number(sym.vardef);
  1302. if paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1303. not(vo_has_local_copy in sym.varoptions) and
  1304. not is_open_string(sym.vardef) then
  1305. c:='v' { should be 'i' but 'i' doesn't work }
  1306. else
  1307. c:='p';
  1308. case sym.localloc.loc of
  1309. LOC_REGISTER,
  1310. LOC_CREGISTER,
  1311. LOC_MMREGISTER,
  1312. LOC_CMMREGISTER,
  1313. LOC_FPUREGISTER,
  1314. LOC_CFPUREGISTER :
  1315. begin
  1316. ss:=get_appendsym_paravar_reg(sym,c,st,sym.localloc.register);
  1317. end;
  1318. LOC_REFERENCE :
  1319. begin
  1320. { When the *value* of a parameter (so not its address!) is
  1321. copied into a local variable, you have to generate two
  1322. stabs: one for the parmeter, and one for the local copy.
  1323. Not doing this breaks debugging under e.g. SPARC. Doc:
  1324. http://sourceware.org/gdb/current/onlinedocs/stabs_4.html#SEC26
  1325. }
  1326. if (target_dbg.id<>dbg_stabx) and
  1327. (c='p') and
  1328. not is_open_string(sym.vardef) and
  1329. ((sym.paraloc[calleeside].location^.loc<>sym.localloc.loc) or
  1330. ((sym.localloc.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  1331. ((sym.paraloc[calleeside].location^.reference.index<>sym.localloc.reference.base) or
  1332. (sym.paraloc[calleeside].location^.reference.offset<>sym.localloc.reference.offset))) or
  1333. ((sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  1334. (sym.localloc.register<>sym.paraloc[calleeside].location^.register))) then
  1335. begin
  1336. if not(sym.paraloc[calleeside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1337. ss:=get_appendsym_paravar_reg(sym,c,st,sym.paraloc[calleeside].location^.register)
  1338. else
  1339. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(localvarsymref_stab,'0','${line}','$2'),[c+st,tostr(sym.paraloc[calleeside].location^.reference.offset)]);
  1340. write_sym_stabstr(list,sym,ss);
  1341. { second stab has no parameter specifier }
  1342. c:='';
  1343. end;
  1344. { offset to ebp => will not work if the framepointer is esp
  1345. so some optimizing will make things harder to debug }
  1346. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(paravarsymref_stab,'0','${line}','$2'),[c+st,tostr(sym.localloc.reference.offset)])
  1347. end;
  1348. else
  1349. internalerror(2003091814);
  1350. end;
  1351. end;
  1352. write_sym_stabstr(list,sym,ss);
  1353. end;
  1354. function stabx_quote_const(const s: string): string;
  1355. var
  1356. i:byte;
  1357. begin
  1358. stabx_quote_const:='';
  1359. for i:=1 to length(s) do
  1360. begin
  1361. case s[i] of
  1362. #10:
  1363. stabx_quote_const:=stabx_quote_const+'\n';
  1364. #13:
  1365. stabx_quote_const:=stabx_quote_const+'\r';
  1366. { stabx strings cannot deal with embedded quotes }
  1367. '"':
  1368. stabx_quote_const:=stabx_quote_const+' ';
  1369. else
  1370. stabx_quote_const:=stabx_quote_const+s[i];
  1371. end;
  1372. end;
  1373. end;
  1374. procedure TDebugInfoStabs.appendsym_const(list:TAsmList;sym:tconstsym);
  1375. var
  1376. st : string;
  1377. ss : ansistring;
  1378. begin
  1379. ss:='';
  1380. { Don't write info for default parameter values, the N_Func breaks
  1381. the N_Func for the function itself.
  1382. Valgrind does not support constants }
  1383. if (sym.owner.symtabletype=parasymtable) or
  1384. (cs_gdb_valgrind in current_settings.globalswitches) then
  1385. exit;
  1386. case sym.consttyp of
  1387. conststring:
  1388. begin
  1389. if sym.value.len<200 then
  1390. if target_dbg.id=dbg_stabs then
  1391. st:='s'''+backspace_quote(octal_quote(strpas(pchar(sym.value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+''''
  1392. else
  1393. st:='s'''+stabx_quote_const(octal_quote(strpas(pchar(sym.value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']))
  1394. else
  1395. st:='<constant string too long>';
  1396. end;
  1397. constord:
  1398. st:='i'+tostr(sym.value.valueord);
  1399. constpointer:
  1400. st:='i'+tostr(sym.value.valueordptr);
  1401. constreal:
  1402. begin
  1403. system.str(pbestreal(sym.value.valueptr)^,st);
  1404. st := 'r'+st;
  1405. end;
  1406. else
  1407. begin
  1408. { if we don't know just put zero !! }
  1409. st:='i0';
  1410. end;
  1411. end;
  1412. ss:=sym_stabstr_evaluate(sym,'"${name}:c=$1;",'+base_stabs_str(constsym_stab,'0','${line}','0'),[st]);
  1413. write_sym_stabstr(list,sym,ss);
  1414. end;
  1415. procedure TDebugInfoStabs.appendsym_type(list:TAsmList;sym:ttypesym);
  1416. var
  1417. ss : ansistring;
  1418. stabchar : string[2];
  1419. begin
  1420. ss:='';
  1421. if not assigned(sym.typedef) then
  1422. internalerror(200509262);
  1423. if sym.typedef.typ in tagtypes then
  1424. stabchar:=tagtypeprefix
  1425. else
  1426. stabchar:='t';
  1427. ss:=sym_stabstr_evaluate(sym,'"${name}:$1$2",'+base_stabs_str(typesym_stab,'0','${line}','0'),[stabchar,def_stab_number(sym.typedef)]);
  1428. write_sym_stabstr(list,sym,ss);
  1429. end;
  1430. procedure TDebugInfoStabs.appendsym_label(list:TAsmList;sym:tlabelsym);
  1431. var
  1432. ss : ansistring;
  1433. begin
  1434. ss:=sym_stabstr_evaluate(sym,'"${name}",'+base_stabs_str(localvarsymref_stab,'0','${line}','0'),[]);
  1435. write_sym_stabstr(list,sym,ss);
  1436. end;
  1437. {****************************************************************************
  1438. Proc/Module support
  1439. ****************************************************************************}
  1440. procedure TDebugInfoStabs.inserttypeinfo;
  1441. var
  1442. stabsvarlist,
  1443. stabstypelist : TAsmList;
  1444. storefilepos : tfileposinfo;
  1445. i : longint;
  1446. vardatatype : ttypesym;
  1447. begin
  1448. storefilepos:=current_filepos;
  1449. current_filepos:=current_module.mainfilepos;
  1450. global_stab_number:=0;
  1451. defnumberlist:=TFPObjectlist.create(false);
  1452. deftowritelist:=TFPObjectlist.create(false);
  1453. stabsvarlist:=TAsmList.create;
  1454. stabstypelist:=TAsmList.create;
  1455. vardatatype:=try_search_system_type('TVARDATA');
  1456. if assigned(vardatatype) then
  1457. vardatadef:=trecorddef(vardatatype.typedef);
  1458. { include symbol that will be referenced from the main to be sure to
  1459. include this debuginfo .o file }
  1460. current_module.flags:=current_module.flags or uf_has_stabs_debuginfo;
  1461. if not(target_info.system in systems_darwin) then
  1462. begin
  1463. new_section(current_asmdata.asmlists[al_stabs],sec_data,GetSymTableName(current_module.localsymtable),0);
  1464. current_asmdata.asmlists[al_stabs].concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1465. end
  1466. else
  1467. new_section(current_asmdata.asmlists[al_stabs],sec_code,GetSymTableName(current_module.localsymtable),0);
  1468. { write all global/local variables. This will flag all required tdefs }
  1469. if assigned(current_module.globalsymtable) then
  1470. write_symtable_syms(stabsvarlist,current_module.globalsymtable);
  1471. if assigned(current_module.localsymtable) then
  1472. write_symtable_syms(stabsvarlist,current_module.localsymtable);
  1473. { write all procedures and methods. This will flag all required tdefs }
  1474. if assigned(current_module.globalsymtable) then
  1475. write_symtable_procdefs(stabsvarlist,current_module.globalsymtable);
  1476. if assigned(current_module.localsymtable) then
  1477. write_symtable_procdefs(stabsvarlist,current_module.localsymtable);
  1478. { reset unit type info flag }
  1479. reset_unit_type_info;
  1480. { write used types from the used units }
  1481. write_used_unit_type_info(stabstypelist,current_module);
  1482. { last write the types from this unit }
  1483. if assigned(current_module.globalsymtable) then
  1484. write_symtable_defs(stabstypelist,current_module.globalsymtable);
  1485. if assigned(current_module.localsymtable) then
  1486. write_symtable_defs(stabstypelist,current_module.localsymtable);
  1487. write_remaining_defs_to_write(stabstypelist);
  1488. current_asmdata.asmlists[al_stabs].concatlist(stabstypelist);
  1489. current_asmdata.asmlists[al_stabs].concatlist(stabsvarlist);
  1490. { reset stab numbers }
  1491. for i:=0 to defnumberlist.count-1 do
  1492. begin
  1493. if assigned(defnumberlist[i]) then
  1494. begin
  1495. tdef(defnumberlist[i]).stab_number:=0;
  1496. tdef(defnumberlist[i]).dbg_state:=dbg_state_unused;
  1497. end;
  1498. end;
  1499. defnumberlist.free;
  1500. defnumberlist:=nil;
  1501. deftowritelist.free;
  1502. deftowritelist:=nil;
  1503. stabsvarlist.free;
  1504. stabstypelist.free;
  1505. current_filepos:=storefilepos;
  1506. end;
  1507. procedure TDebugInfoStabs.insertlineinfo(list: TAsmList);
  1508. var
  1509. currfileinfo,
  1510. lastfileinfo : tfileposinfo;
  1511. currfuncname : pshortstring;
  1512. currsectype : TAsmSectiontype;
  1513. hlabel : tasmlabel;
  1514. hp : tai;
  1515. infile : tinputfile;
  1516. begin
  1517. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1518. currfuncname:=nil;
  1519. currsectype:=sec_code;
  1520. hp:=Tai(list.first);
  1521. while assigned(hp) do
  1522. begin
  1523. case hp.typ of
  1524. ait_section :
  1525. currsectype:=tai_section(hp).sectype;
  1526. ait_function_name :
  1527. currfuncname:=tai_function_name(hp).funcname;
  1528. ait_force_line :
  1529. lastfileinfo.line:=-1;
  1530. end;
  1531. if (currsectype=sec_code) and
  1532. (hp.typ=ait_instruction) then
  1533. begin
  1534. currfileinfo:=tailineinfo(hp).fileinfo;
  1535. { file changed ? (must be before line info) }
  1536. if (currfileinfo.fileindex<>0) and
  1537. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  1538. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  1539. begin
  1540. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  1541. if assigned(infile) then
  1542. begin
  1543. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1544. { emit stabs }
  1545. if not(ds_stabs_abs_include_files in current_settings.debugswitches) or
  1546. path_absolute(infile.path^) then
  1547. list.insertbefore(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(infile.path^,false))+FixFileName(infile.name^)+'",'+tostr(stabs_n_includefile)+
  1548. ',0,0,'+hlabel.name),hp)
  1549. else
  1550. list.insertbefore(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(getcurrentdir,false)+FixPath(infile.path^,false))+FixFileName(infile.name^)+'",'+tostr(stabs_n_includefile)+
  1551. ',0,0,'+hlabel.name),hp);
  1552. list.insertbefore(tai_label.create(hlabel),hp);
  1553. { force new line info }
  1554. lastfileinfo.line:=-1;
  1555. end;
  1556. end;
  1557. { line changed ? }
  1558. if (currfileinfo.line>lastfileinfo.line) and (currfileinfo.line<>0) then
  1559. begin
  1560. if assigned(currfuncname) and
  1561. not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1562. begin
  1563. current_asmdata.getlabel(hlabel,alt_dbgline);
  1564. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(stabs_n_textline)+',0,'+tostr(currfileinfo.line)+','+
  1565. hlabel.name+' - '+{$IFDEF POWERPC64}'.'+{$ENDIF POWERPC64}currfuncname^),hp);
  1566. list.insertbefore(tai_label.create(hlabel),hp);
  1567. end
  1568. else
  1569. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(stabs_n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  1570. end;
  1571. lastfileinfo:=currfileinfo;
  1572. end;
  1573. hp:=tai(hp.next);
  1574. end;
  1575. end;
  1576. procedure TDebugInfoStabs.insertmoduleinfo;
  1577. var
  1578. hlabel : tasmlabel;
  1579. infile : tinputfile;
  1580. begin
  1581. { emit main source n_sourcefile for start of module }
  1582. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1583. infile:=current_module.sourcefiles.get_file(1);
  1584. new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),0,secorder_begin);
  1585. if not(target_info.system in systems_darwin) then
  1586. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(make_mangledname('DEBUGSTART',current_module.localsymtable,''),AT_DATA,0));
  1587. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(getcurrentdir,false))+'",'+
  1588. base_stabs_str(stabs_n_sourcefile,'0','0',hlabel.name)));
  1589. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(infile.path^,false))+FixFileName(infile.name^)+'",'+
  1590. base_stabs_str(stabs_n_sourcefile,'0','0',hlabel.name)));
  1591. current_asmdata.asmlists[al_start].concat(tai_label.create(hlabel));
  1592. { for darwin, you need a "module marker" too to work around }
  1593. { either some assembler or gdb bug (radar 4386531 according to a }
  1594. { comment in dbxout.c of Apple's gcc) }
  1595. if (target_info.system in systems_darwin) then
  1596. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stabsdir,'"",'+base_stabs_str(STABS_N_OSO,'0','0','0')));
  1597. { emit empty n_sourcefile for end of module }
  1598. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1599. new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),0,secorder_end);
  1600. if not(target_info.system in systems_darwin) then
  1601. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(make_mangledname('DEBUGEND',current_module.localsymtable,''),AT_DATA,0));
  1602. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stabsdir,'"",'+base_stabs_str(stabs_n_sourcefile,'0','0',hlabel.name)));
  1603. current_asmdata.asmlists[al_end].concat(tai_label.create(hlabel));
  1604. end;
  1605. procedure TDebugInfoStabs.referencesections(list: TAsmList);
  1606. var
  1607. hp : tmodule;
  1608. dbgtable : tai_symbol;
  1609. begin
  1610. { Reference all DEBUGINFO sections from the main .fpc section }
  1611. if (target_info.system in ([system_powerpc_macos]+systems_darwin)) then
  1612. exit;
  1613. new_section(list,sec_fpc,'links',0);
  1614. { make sure the debuginfo doesn't get stripped out }
  1615. if (target_info.system in systems_darwin) then
  1616. begin
  1617. dbgtable:=tai_symbol.createname('DEBUGINFOTABLE',AT_DATA,0);
  1618. list.concat(tai_directive.create(asd_no_dead_strip,dbgtable.sym.name));
  1619. list.concat(dbgtable);
  1620. end;
  1621. { include reference to all debuginfo sections of used units }
  1622. hp:=tmodule(loaded_units.first);
  1623. while assigned(hp) do
  1624. begin
  1625. If (hp.flags and uf_has_stabs_debuginfo)=uf_has_stabs_debuginfo then
  1626. begin
  1627. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.localsymtable,''),0));
  1628. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  1629. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  1630. end;
  1631. hp:=tmodule(hp.next);
  1632. end;
  1633. end;
  1634. constructor TDebugInfoStabs.Create;
  1635. begin
  1636. inherited Create;
  1637. dbgtype:=dbg_stabs;
  1638. stabsdir:=stab_stabs;
  1639. def_stab:=STABS_N_LSYM;
  1640. regvar_stab:=STABS_N_RSYM;
  1641. procdef_stab:=STABS_N_Function;
  1642. constsym_stab:=STABS_N_Function;
  1643. typesym_stab:=STABS_N_LSYM;
  1644. globalvarsym_uninited_stab:=STABS_N_STSYM;
  1645. globalvarsym_inited_stab:=STABS_N_LCSYM;
  1646. staticvarsym_uninited_stab:=STABS_N_STSYM;
  1647. staticvarsym_inited_stab:=STABS_N_LCSYM;
  1648. localvarsymref_stab:=STABS_N_TSYM;
  1649. paravarsymref_stab:=STABS_N_TSYM;
  1650. tagtypeprefix:='Tt';
  1651. vardatadef:=nil;
  1652. end;
  1653. const
  1654. dbg_stabs_info : tdbginfo =
  1655. (
  1656. id : dbg_stabs;
  1657. idtxt : 'STABS';
  1658. );
  1659. initialization
  1660. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  1661. end.