dbgstabs.pas 61 KB

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