dbgstabs.pas 63 KB

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