dbgstabs.pas 60 KB

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