dbgstabs.pas 61 KB

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