dbgstabs.pas 59 KB

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