dbgstabs.pas 61 KB

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