dbgstabs.pas 73 KB

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