dbgstabs.pas 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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;
  25. type
  26. TDebugInfoStabs=class(TDebugInfo)
  27. private
  28. writing_def_stabs : boolean;
  29. global_stab_number : word;
  30. defnumberlist : tlist;
  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:taasmoutput;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:taasmoutput;def:tdef);
  44. procedure field_write_defs(p:Tnamedindexitem;arg:pointer);
  45. procedure method_write_defs(p :tnamedindexitem;arg:pointer);
  46. procedure write_symtable_defs(list:taasmoutput;st:tsymtable);
  47. procedure write_procdef(list:taasmoutput;pd:tprocdef);
  48. procedure insertsym(list:taasmoutput;sym:tsym);
  49. procedure insertdef(list:taasmoutput;def:tdef);
  50. public
  51. procedure inserttypeinfo;override;
  52. procedure insertmoduleinfo;override;
  53. procedure insertlineinfo(list:taasmoutput);override;
  54. procedure referencesections(list:taasmoutput);override;
  55. end;
  56. implementation
  57. uses
  58. strings,cutils,
  59. systems,globals,globtype,verbose,
  60. symconst,defutil,
  61. cpuinfo,cpubase,cgbase,paramgr,
  62. aasmbase,procinfo,
  63. finput,fmodule,ppu;
  64. const
  65. memsizeinc = 512;
  66. N_GSYM = $20;
  67. N_STSYM = 38; { initialized const }
  68. N_LCSYM = 40; { non initialized variable}
  69. N_Function = $24; { function or const }
  70. N_TextLine = $44;
  71. N_DataLine = $46;
  72. N_BssLine = $48;
  73. N_RSYM = $40; { register variable }
  74. N_LSYM = $80;
  75. N_tsym = 160;
  76. N_SourceFile = $64;
  77. N_IncludeFile = $84;
  78. N_BINCL = $82;
  79. N_EINCL = $A2;
  80. N_EXCL = $C2;
  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.stab_state:=stab_state_written;
  221. { Stab must already be written, or we must be busy writing it }
  222. if writing_def_stabs and
  223. not(def.stab_state in [stab_state_writing,stab_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.stab_state=stab_state_unused then
  229. def.stab_state:=stab_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_stab) 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:taasmoutput;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_stab 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.field_write_defs(p:Tnamedindexitem;arg:pointer);
  725. begin
  726. if (Tsym(p).typ=fieldvarsym) and
  727. not(sp_static in Tsym(p).symoptions) then
  728. insertdef(taasmoutput(arg),tfieldvarsym(p).vartype.def);
  729. end;
  730. procedure TDebugInfoStabs.method_write_defs(p :tnamedindexitem;arg:pointer);
  731. var
  732. pd : tprocdef;
  733. begin
  734. if tsym(p).typ = procsym then
  735. begin
  736. pd:=tprocsym(p).first_procdef;
  737. insertdef(taasmoutput(arg),pd.rettype.def);
  738. end;
  739. end;
  740. procedure TDebugInfoStabs.insertdef(list:taasmoutput;def:tdef);
  741. var
  742. anc : tobjectdef;
  743. oldtypesym : tsym;
  744. begin
  745. if (def.stab_state in [stab_state_writing,stab_state_written]) then
  746. exit;
  747. { never write generic template defs }
  748. if df_generic in def.defoptions then
  749. begin
  750. def.stab_state:=stab_state_written;
  751. exit;
  752. end;
  753. { to avoid infinite loops }
  754. def.stab_state := stab_state_writing;
  755. { write dependencies first }
  756. case def.deftype of
  757. stringdef :
  758. begin
  759. if tstringdef(def).string_typ=st_widestring then
  760. insertdef(list,cwidechartype.def)
  761. else
  762. begin
  763. insertdef(list,cchartype.def);
  764. insertdef(list,u8inttype.def);
  765. end;
  766. end;
  767. floatdef :
  768. insertdef(list,s32inttype.def);
  769. filedef :
  770. begin
  771. insertdef(list,s32inttype.def);
  772. {$ifdef cpu64bit}
  773. insertdef(list,s64inttype.def);
  774. {$endif cpu64bit}
  775. insertdef(list,u8inttype.def);
  776. insertdef(list,cchartype.def);
  777. end;
  778. classrefdef :
  779. insertdef(list,pvmttype.def);
  780. pointerdef :
  781. insertdef(list,tpointerdef(def).pointertype.def);
  782. setdef :
  783. insertdef(list,tsetdef(def).elementtype.def);
  784. procvardef,
  785. procdef :
  786. insertdef(list,tprocdef(def).rettype.def);
  787. arraydef :
  788. begin
  789. insertdef(list,tarraydef(def).rangetype.def);
  790. insertdef(list,tarraydef(def).elementtype.def);
  791. end;
  792. recorddef :
  793. trecorddef(def).symtable.foreach(@field_write_defs,list);
  794. objectdef :
  795. begin
  796. insertdef(list,vmtarraytype.def);
  797. { first the parents }
  798. anc:=tobjectdef(def);
  799. while assigned(anc.childof) do
  800. begin
  801. anc:=anc.childof;
  802. insertdef(list,anc);
  803. end;
  804. tobjectdef(def).symtable.foreach(@field_write_defs,list);
  805. tobjectdef(def).symtable.foreach(@method_write_defs,list);
  806. end;
  807. end;
  808. case def.deftype of
  809. objectdef :
  810. begin
  811. { classes require special code to write the record and the invisible pointer }
  812. if is_class(def) then
  813. begin
  814. { Write the record class itself }
  815. tobjectdef(def).writing_class_record_stab:=true;
  816. write_def_stabstr(list,def);
  817. tobjectdef(def).writing_class_record_stab:=false;
  818. { Write the invisible pointer class }
  819. oldtypesym:=def.typesym;
  820. def.typesym:=nil;
  821. write_def_stabstr(list,def);
  822. def.typesym:=oldtypesym;
  823. end
  824. else
  825. write_def_stabstr(list,def);
  826. { VMT symbol }
  827. if (oo_has_vmt in tobjectdef(def).objectoptions) and
  828. assigned(def.owner) and
  829. assigned(def.owner.name) then
  830. list.concat(Tai_stab.create(stab_stabs,strpnew('"vmt_'+def.owner.name^+tobjectdef(def).name+':S'+
  831. def_stab_number(vmttype.def)+'",'+tostr(N_STSYM)+',0,0,'+tobjectdef(def).vmt_mangledname)));
  832. end;
  833. procdef :
  834. begin
  835. { procdefs are handled separatly }
  836. end;
  837. else
  838. write_def_stabstr(list,def);
  839. end;
  840. def.stab_state := stab_state_written;
  841. end;
  842. procedure TDebugInfoStabs.write_symtable_defs(list:taasmoutput;st:tsymtable);
  843. procedure dowritestabs(list:taasmoutput;st:tsymtable);
  844. var
  845. p : tdef;
  846. begin
  847. p:=tdef(st.defindex.first);
  848. while assigned(p) do
  849. begin
  850. if (p.stab_state=stab_state_used) then
  851. insertdef(list,p);
  852. p:=tdef(p.indexnext);
  853. end;
  854. end;
  855. var
  856. old_writing_def_stabs : boolean;
  857. begin
  858. case st.symtabletype of
  859. staticsymtable :
  860. list.concat(tai_comment.Create(strpnew('Defs - Begin Staticsymtable')));
  861. globalsymtable :
  862. list.concat(tai_comment.Create(strpnew('Defs - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  863. end;
  864. old_writing_def_stabs:=writing_def_stabs;
  865. writing_def_stabs:=true;
  866. dowritestabs(list,st);
  867. writing_def_stabs:=old_writing_def_stabs;
  868. case st.symtabletype of
  869. staticsymtable :
  870. list.concat(tai_comment.Create(strpnew('Defs - End Staticsymtable')));
  871. globalsymtable :
  872. list.concat(tai_comment.Create(strpnew('Defs - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  873. end;
  874. end;
  875. procedure TDebugInfoStabs.write_procdef(list:taasmoutput;pd:tprocdef);
  876. var
  877. templist : taasmoutput;
  878. stabsendlabel : tasmlabel;
  879. mangled_length : longint;
  880. p : pchar;
  881. hs : string;
  882. begin
  883. if assigned(pd.procstarttai) then
  884. begin
  885. templist:=taasmoutput.create;
  886. { para types }
  887. write_def_stabstr(templist,pd);
  888. if assigned(pd.parast) then
  889. write_symtable_syms(templist,pd.parast);
  890. { local type defs and vars should not be written
  891. inside the main proc stab }
  892. if assigned(pd.localst) and
  893. (pd.localst.symtabletype=localsymtable) then
  894. write_symtable_syms(templist,pd.localst);
  895. asmlist[al_procedures].insertlistbefore(pd.procstarttai,templist);
  896. { end of procedure }
  897. objectlibrary.getlabel(stabsendlabel,alt_dbgtype);
  898. templist.concat(tai_label.create(stabsendlabel));
  899. if assigned(pd.funcretsym) and
  900. (tabstractnormalvarsym(pd.funcretsym).refs>0) then
  901. begin
  902. if tabstractnormalvarsym(pd.funcretsym).localloc.loc=LOC_REFERENCE then
  903. begin
  904. {$warning Need to add gdb support for ret in param register calling}
  905. if paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  906. hs:='X*'
  907. else
  908. hs:='X';
  909. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  910. '"'+pd.procsym.name+':'+hs+def_stab_number(pd.rettype.def)+'",'+
  911. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(pd.funcretsym).localloc.reference.offset))));
  912. if (m_result in aktmodeswitches) then
  913. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  914. '"RESULT:'+hs+def_stab_number(pd.rettype.def)+'",'+
  915. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(pd.funcretsym).localloc.reference.offset))));
  916. end;
  917. end;
  918. mangled_length:=length(pd.mangledname);
  919. getmem(p,2*mangled_length+50);
  920. strpcopy(p,'192,0,0,');
  921. {$IFDEF POWERPC64}strpcopy(strend(p), '.');{$ENDIF POWERPC64}
  922. strpcopy(strend(p),pd.mangledname);
  923. if (tf_use_function_relative_addresses in target_info.flags) then
  924. begin
  925. strpcopy(strend(p),'-');
  926. {$IFDEF POWERPC64}strpcopy(strend(p), '.');{$ENDIF POWERPC64}
  927. strpcopy(strend(p),pd.mangledname);
  928. end;
  929. templist.concat(Tai_stab.Create(stab_stabn,strnew(p)));
  930. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  931. if (tf_use_function_relative_addresses in target_info.flags) then
  932. begin
  933. strpcopy(strend(p),'-');
  934. {$IFDEF POWERPC64}strpcopy(strend(p), '.');{$ENDIF POWERPC64}
  935. strpcopy(strend(p),pd.mangledname);
  936. end;
  937. templist.concat(Tai_stab.Create(stab_stabn,strnew(p)));
  938. freemem(p,2*mangled_length+50);
  939. asmlist[al_procedures].insertlistbefore(pd.procendtai,templist);
  940. templist.free;
  941. end;
  942. end;
  943. {****************************************************************************
  944. TSym support
  945. ****************************************************************************}
  946. function TDebugInfoStabs.sym_var_value(const s:string;arg:pointer):string;
  947. var
  948. sym : tsym;
  949. begin
  950. sym:=tsym(arg);
  951. result:='';
  952. if s='name' then
  953. result:=sym.name
  954. else if s='mangledname' then
  955. result:=sym.mangledname
  956. else if s='ownername' then
  957. result:=sym.owner.name^
  958. else if s='line' then
  959. result:=tostr(sym.fileinfo.line)
  960. else if s='N_LSYM' then
  961. result:=tostr(N_LSYM)
  962. else if s='N_LCSYM' then
  963. result:=tostr(N_LCSYM)
  964. else if s='N_RSYM' then
  965. result:=tostr(N_RSYM)
  966. else if s='N_TSYM' then
  967. result:=tostr(N_TSYM)
  968. else if s='N_STSYM' then
  969. result:=tostr(N_STSYM)
  970. else if s='N_FUNCTION' then
  971. result:=tostr(N_FUNCTION)
  972. else
  973. internalerror(200401152);
  974. end;
  975. function TDebugInfoStabs.sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):Pchar;
  976. begin
  977. result:=string_evaluate(s,@sym_var_value,sym,vars);
  978. end;
  979. procedure TDebugInfoStabs.insertsym(list:taasmoutput;sym:tsym);
  980. function fieldvarsym_stabstr(sym:tfieldvarsym):Pchar;
  981. begin
  982. result:=nil;
  983. if (sym.owner.symtabletype=objectsymtable) and
  984. (sp_static in sym.symoptions) then
  985. result:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",${N_LCSYM},0,${line},${mangledname}',
  986. [def_stab_number(sym.vartype.def)]);
  987. end;
  988. function globalvarsym_stabstr(sym:tglobalvarsym):Pchar;
  989. var
  990. st : string;
  991. threadvaroffset : string;
  992. regidx : Tregisterindex;
  993. begin
  994. result:=nil;
  995. { external symbols can't be resolved at link time, so we
  996. can't generate stabs for them }
  997. if vo_is_external in sym.varoptions then
  998. exit;
  999. st:=def_stab_number(sym.vartype.def);
  1000. case sym.localloc.loc of
  1001. LOC_REGISTER,
  1002. LOC_CREGISTER,
  1003. LOC_MMREGISTER,
  1004. LOC_CMMREGISTER,
  1005. LOC_FPUREGISTER,
  1006. LOC_CFPUREGISTER :
  1007. begin
  1008. regidx:=findreg_by_number(sym.localloc.register);
  1009. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1010. { this is the register order for GDB}
  1011. if regidx<>0 then
  1012. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1013. end;
  1014. else
  1015. begin
  1016. if (vo_is_thread_var in sym.varoptions) then
  1017. threadvaroffset:='+'+tostr(sizeof(aint))
  1018. else
  1019. threadvaroffset:='';
  1020. { Here we used S instead of
  1021. because with G GDB doesn't look at the address field
  1022. but searches the same name or with a leading underscore
  1023. but these names don't exist in pascal !}
  1024. st:='S'+st;
  1025. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_LCSYM},0,${line},${mangledname}$2',[st,threadvaroffset]);
  1026. end;
  1027. end;
  1028. end;
  1029. function localvarsym_stabstr(sym:tlocalvarsym):Pchar;
  1030. var
  1031. st : string;
  1032. regidx : Tregisterindex;
  1033. begin
  1034. result:=nil;
  1035. { There is no space allocated for not referenced locals }
  1036. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1037. exit;
  1038. st:=def_stab_number(sym.vartype.def);
  1039. case sym.localloc.loc of
  1040. LOC_REGISTER,
  1041. LOC_CREGISTER,
  1042. LOC_MMREGISTER,
  1043. LOC_CMMREGISTER,
  1044. LOC_FPUREGISTER,
  1045. LOC_CFPUREGISTER :
  1046. begin
  1047. regidx:=findreg_by_number(sym.localloc.register);
  1048. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1049. { this is the register order for GDB}
  1050. if regidx<>0 then
  1051. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1052. end;
  1053. LOC_REFERENCE :
  1054. { offset to ebp => will not work if the framepointer is esp
  1055. so some optimizing will make things harder to debug }
  1056. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1057. else
  1058. internalerror(2003091814);
  1059. end;
  1060. end;
  1061. function paravarsym_stabstr(sym:tparavarsym):Pchar;
  1062. var
  1063. st : string;
  1064. regidx : Tregisterindex;
  1065. c : char;
  1066. begin
  1067. result:=nil;
  1068. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1069. { while stabs aren't adapted for regvars yet }
  1070. if (vo_is_self in sym.varoptions) then
  1071. begin
  1072. case sym.localloc.loc of
  1073. LOC_REGISTER,
  1074. LOC_CREGISTER:
  1075. regidx:=findreg_by_number(sym.localloc.register);
  1076. LOC_REFERENCE: ;
  1077. else
  1078. internalerror(2003091815);
  1079. end;
  1080. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1081. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1082. begin
  1083. if (sym.localloc.loc=LOC_REFERENCE) then
  1084. result:=sym_stabstr_evaluate(sym,'"pvmt:p$1",${N_TSYM},0,0,$2',
  1085. [def_stab_number(pvmttype.def),tostr(sym.localloc.reference.offset)]);
  1086. (* else
  1087. result:=sym_stabstr_evaluate(sym,'"pvmt:r$1",${N_RSYM},0,0,$2',
  1088. [def_stab_number(pvmttype.def),tostr(regstabs_table[regidx])]) *)
  1089. end
  1090. else
  1091. begin
  1092. if not(is_class(tprocdef(sym.owner.defowner)._class)) then
  1093. c:='v'
  1094. else
  1095. c:='p';
  1096. if (sym.localloc.loc=LOC_REFERENCE) then
  1097. result:=sym_stabstr_evaluate(sym,'"$$t:$1",${N_TSYM},0,0,$2',
  1098. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(sym.localloc.reference.offset)]);
  1099. (* else
  1100. result:=sym_stabstr_evaluate(sym,'"$$t:r$1",${N_RSYM},0,0,$2',
  1101. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(regstabs_table[regidx])]); *)
  1102. end;
  1103. end
  1104. else
  1105. begin
  1106. st:=def_stab_number(sym.vartype.def);
  1107. if paramanager.push_addr_param(sym.varspez,sym.vartype.def,tprocdef(sym.owner.defowner).proccalloption) and
  1108. not(vo_has_local_copy in sym.varoptions) and
  1109. not is_open_string(sym.vartype.def) then
  1110. st := 'v'+st { should be 'i' but 'i' doesn't work }
  1111. else
  1112. st := 'p'+st;
  1113. case sym.localloc.loc of
  1114. LOC_REGISTER,
  1115. LOC_CREGISTER,
  1116. LOC_MMREGISTER,
  1117. LOC_CMMREGISTER,
  1118. LOC_FPUREGISTER,
  1119. LOC_CFPUREGISTER :
  1120. begin
  1121. regidx:=findreg_by_number(sym.localloc.register);
  1122. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1123. { this is the register order for GDB}
  1124. if regidx<>0 then
  1125. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(longint(regstabs_table[regidx]))]);
  1126. end;
  1127. LOC_REFERENCE :
  1128. { offset to ebp => will not work if the framepointer is esp
  1129. so some optimizing will make things harder to debug }
  1130. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1131. else
  1132. internalerror(2003091814);
  1133. end;
  1134. end;
  1135. end;
  1136. function constsym_stabstr(sym:tconstsym):Pchar;
  1137. var
  1138. st : string;
  1139. begin
  1140. case sym.consttyp of
  1141. conststring:
  1142. begin
  1143. if sym.value.len<200 then
  1144. st:='s'''+backspace_quote(octal_quote(strpas(pchar(sym.value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+''''
  1145. else
  1146. st:='<constant string too long>';
  1147. end;
  1148. constord:
  1149. st:='i'+tostr(sym.value.valueord);
  1150. constpointer:
  1151. st:='i'+tostr(sym.value.valueordptr);
  1152. constreal:
  1153. begin
  1154. system.str(pbestreal(sym.value.valueptr)^,st);
  1155. st := 'r'+st;
  1156. end;
  1157. else
  1158. begin
  1159. { if we don't know just put zero !! }
  1160. st:='i0';
  1161. end;
  1162. end;
  1163. { valgrind does not support constants }
  1164. if cs_gdb_valgrind in aktglobalswitches then
  1165. result:=nil
  1166. else
  1167. result:=sym_stabstr_evaluate(sym,'"${name}:c=$1;",${N_FUNCTION},0,${line},0',[st]);
  1168. end;
  1169. function typesym_stabstr(sym:ttypesym) : pchar;
  1170. var
  1171. stabchar : string[2];
  1172. begin
  1173. result:=nil;
  1174. if not assigned(sym.restype.def) then
  1175. internalerror(200509262);
  1176. if sym.restype.def.deftype in tagtypes then
  1177. stabchar:='Tt'
  1178. else
  1179. stabchar:='t';
  1180. result:=sym_stabstr_evaluate(sym,'"${name}:$1$2",${N_LSYM},0,${line},0',[stabchar,def_stab_number(sym.restype.def)]);
  1181. end;
  1182. function procsym_stabstr(sym:tprocsym) : pchar;
  1183. var
  1184. i : longint;
  1185. begin
  1186. result:=nil;
  1187. for i:=1 to sym.procdef_count do
  1188. write_procdef(list,sym.procdef[i]);
  1189. end;
  1190. var
  1191. stabstr : Pchar;
  1192. begin
  1193. stabstr:=nil;
  1194. case sym.typ of
  1195. labelsym :
  1196. stabstr:=sym_stabstr_evaluate(sym,'"${name}",${N_LSYM},0,${line},0',[]);
  1197. fieldvarsym :
  1198. stabstr:=fieldvarsym_stabstr(tfieldvarsym(sym));
  1199. globalvarsym :
  1200. stabstr:=globalvarsym_stabstr(tglobalvarsym(sym));
  1201. localvarsym :
  1202. stabstr:=localvarsym_stabstr(tlocalvarsym(sym));
  1203. paravarsym :
  1204. stabstr:=paravarsym_stabstr(tparavarsym(sym));
  1205. typedconstsym :
  1206. stabstr:=sym_stabstr_evaluate(sym,'"${name}:S$1",${N_STSYM},0,${line},${mangledname}',
  1207. [def_stab_number(ttypedconstsym(sym).typedconsttype.def)]);
  1208. constsym :
  1209. stabstr:=constsym_stabstr(tconstsym(sym));
  1210. typesym :
  1211. stabstr:=typesym_stabstr(ttypesym(sym));
  1212. procsym :
  1213. stabstr:=procsym_stabstr(tprocsym(sym));
  1214. end;
  1215. if stabstr<>nil then
  1216. list.concat(Tai_stab.create(stab_stabs,stabstr));
  1217. { For object types write also the symtable entries }
  1218. if (sym.typ=typesym) and (ttypesym(sym).restype.def.deftype=objectdef) then
  1219. write_symtable_syms(list,tobjectdef(ttypesym(sym).restype.def).symtable);
  1220. sym.isstabwritten:=true;
  1221. end;
  1222. procedure TDebugInfoStabs.write_symtable_syms(list:taasmoutput;st:tsymtable);
  1223. var
  1224. p : tsym;
  1225. begin
  1226. case st.symtabletype of
  1227. staticsymtable :
  1228. list.concat(tai_comment.Create(strpnew('Syms - Begin Staticsymtable')));
  1229. globalsymtable :
  1230. list.concat(tai_comment.Create(strpnew('Syms - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  1231. end;
  1232. p:=tsym(st.symindex.first);
  1233. while assigned(p) do
  1234. begin
  1235. if (not p.isstabwritten) then
  1236. insertsym(list,p);
  1237. p:=tsym(p.indexnext);
  1238. end;
  1239. case st.symtabletype of
  1240. staticsymtable :
  1241. list.concat(tai_comment.Create(strpnew('Syms - End Staticsymtable')));
  1242. globalsymtable :
  1243. list.concat(tai_comment.Create(strpnew('Syms - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  1244. end;
  1245. end;
  1246. {****************************************************************************
  1247. Proc/Module support
  1248. ****************************************************************************}
  1249. procedure tdebuginfostabs.inserttypeinfo;
  1250. procedure reset_unit_type_info;
  1251. var
  1252. hp : tmodule;
  1253. begin
  1254. hp:=tmodule(loaded_units.first);
  1255. while assigned(hp) do
  1256. begin
  1257. hp.is_stab_written:=false;
  1258. hp:=tmodule(hp.next);
  1259. end;
  1260. end;
  1261. procedure write_used_unit_type_info(list:taasmoutput;hp:tmodule);
  1262. var
  1263. pu : tused_unit;
  1264. begin
  1265. pu:=tused_unit(hp.used_units.first);
  1266. while assigned(pu) do
  1267. begin
  1268. if not pu.u.is_stab_written then
  1269. begin
  1270. { prevent infinte loop for circular dependencies }
  1271. pu.u.is_stab_written:=true;
  1272. { write type info from used units, use a depth first
  1273. strategy to reduce the recursion in writing all
  1274. dependent stabs }
  1275. write_used_unit_type_info(list,pu.u);
  1276. if assigned(pu.u.globalsymtable) then
  1277. write_symtable_defs(list,pu.u.globalsymtable);
  1278. end;
  1279. pu:=tused_unit(pu.next);
  1280. end;
  1281. end;
  1282. var
  1283. stabsvarlist,
  1284. stabstypelist : taasmoutput;
  1285. storefilepos : tfileposinfo;
  1286. st : tsymtable;
  1287. i : longint;
  1288. begin
  1289. storefilepos:=aktfilepos;
  1290. aktfilepos:=current_module.mainfilepos;
  1291. global_stab_number:=0;
  1292. defnumberlist:=tlist.create;
  1293. stabsvarlist:=taasmoutput.create;
  1294. stabstypelist:=taasmoutput.create;
  1295. { include symbol that will be referenced from the main to be sure to
  1296. include this debuginfo .o file }
  1297. if current_module.is_unit then
  1298. begin
  1299. current_module.flags:=current_module.flags or uf_has_debuginfo;
  1300. st:=current_module.globalsymtable;
  1301. end
  1302. else
  1303. st:=current_module.localsymtable;
  1304. new_section(asmlist[al_stabs],sec_data,st.name^,0);
  1305. asmlist[al_stabs].concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',st,''),AT_DATA,0));
  1306. { first write all global/local symbols. This will flag all required tdefs }
  1307. if assigned(current_module.globalsymtable) then
  1308. write_symtable_syms(stabsvarlist,current_module.globalsymtable);
  1309. if assigned(current_module.localsymtable) then
  1310. write_symtable_syms(stabsvarlist,current_module.localsymtable);
  1311. { reset unit type info flag }
  1312. reset_unit_type_info;
  1313. { write used types from the used units }
  1314. write_used_unit_type_info(stabstypelist,current_module);
  1315. { last write the types from this unit }
  1316. if assigned(current_module.globalsymtable) then
  1317. write_symtable_defs(stabstypelist,current_module.globalsymtable);
  1318. if assigned(current_module.localsymtable) then
  1319. write_symtable_defs(stabstypelist,current_module.localsymtable);
  1320. asmlist[al_stabs].concatlist(stabstypelist);
  1321. asmlist[al_stabs].concatlist(stabsvarlist);
  1322. { reset stab numbers }
  1323. for i:=0 to defnumberlist.count-1 do
  1324. begin
  1325. if assigned(defnumberlist[i]) then
  1326. begin
  1327. tdef(defnumberlist[i]).stab_number:=0;
  1328. tdef(defnumberlist[i]).stab_state:=stab_state_unused;
  1329. end;
  1330. end;
  1331. defnumberlist.free;
  1332. defnumberlist:=nil;
  1333. stabsvarlist.free;
  1334. stabstypelist.free;
  1335. aktfilepos:=storefilepos;
  1336. end;
  1337. procedure tdebuginfostabs.insertlineinfo(list:taasmoutput);
  1338. var
  1339. currfileinfo,
  1340. lastfileinfo : tfileposinfo;
  1341. currfuncname : pstring;
  1342. currsectype : tasmsectiontype;
  1343. hlabel : tasmlabel;
  1344. hp : tai;
  1345. infile : tinputfile;
  1346. begin
  1347. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1348. currfuncname:=nil;
  1349. currsectype:=sec_code;
  1350. hp:=Tai(list.first);
  1351. while assigned(hp) do
  1352. begin
  1353. case hp.typ of
  1354. ait_section :
  1355. currsectype:=tai_section(hp).sectype;
  1356. ait_function_name :
  1357. currfuncname:=tai_function_name(hp).funcname;
  1358. ait_force_line :
  1359. lastfileinfo.line:=-1;
  1360. end;
  1361. if (currsectype=sec_code) and
  1362. (hp.typ=ait_instruction) then
  1363. begin
  1364. currfileinfo:=tailineinfo(hp).fileinfo;
  1365. { file changed ? (must be before line info) }
  1366. if (currfileinfo.fileindex<>0) and
  1367. (lastfileinfo.fileindex<>currfileinfo.fileindex) then
  1368. begin
  1369. infile:=current_module.sourcefiles.get_file(currfileinfo.fileindex);
  1370. if assigned(infile) then
  1371. begin
  1372. objectlibrary.getlabel(hlabel,alt_dbgfile);
  1373. { emit stabs }
  1374. if (infile.path^<>'') then
  1375. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_includefile)+
  1376. ',0,0,'+hlabel.name),hp);
  1377. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_includefile)+
  1378. ',0,0,'+hlabel.name),hp);
  1379. list.insertbefore(tai_label.create(hlabel),hp);
  1380. { force new line info }
  1381. lastfileinfo.line:=-1;
  1382. end;
  1383. end;
  1384. { line changed ? }
  1385. if (lastfileinfo.line<>currfileinfo.line) and (currfileinfo.line<>0) then
  1386. begin
  1387. if assigned(currfuncname) and
  1388. (tf_use_function_relative_addresses in target_info.flags) then
  1389. begin
  1390. objectlibrary.getlabel(hlabel,alt_dbgline);
  1391. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(n_textline)+',0,'+tostr(currfileinfo.line)+','+
  1392. hlabel.name+' - '+{$IFDEF POWERPC64}'.'+{$ENDIF POWERPC64}currfuncname^),hp);
  1393. list.insertbefore(tai_label.create(hlabel),hp);
  1394. end
  1395. else
  1396. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  1397. end;
  1398. lastfileinfo:=currfileinfo;
  1399. end;
  1400. hp:=tai(hp.next);
  1401. end;
  1402. end;
  1403. procedure tdebuginfostabs.insertmoduleinfo;
  1404. var
  1405. hlabel : tasmlabel;
  1406. infile : tinputfile;
  1407. templist : taasmoutput;
  1408. begin
  1409. { emit main source n_sourcefile for start of module }
  1410. objectlibrary.getlabel(hlabel,alt_dbgfile);
  1411. infile:=current_module.sourcefiles.get_file(1);
  1412. templist:=taasmoutput.create;
  1413. new_section(templist,sec_code,'',0);
  1414. if (infile.path^<>'') then
  1415. templist.concat(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_sourcefile)+
  1416. ',0,0,'+hlabel.name));
  1417. templist.concat(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_sourcefile)+
  1418. ',0,0,'+hlabel.name));
  1419. templist.concat(tai_label.create(hlabel));
  1420. asmlist[al_stabsstart].insertlist(templist);
  1421. templist.free;
  1422. { emit empty n_sourcefile for end of module }
  1423. objectlibrary.getlabel(hlabel,alt_dbgfile);
  1424. templist:=taasmoutput.create;
  1425. new_section(templist,sec_code,'',0);
  1426. templist.concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(n_sourcefile)+',0,0,'+hlabel.name));
  1427. templist.concat(tai_label.create(hlabel));
  1428. asmlist[al_stabsend].insertlist(templist);
  1429. templist.free;
  1430. end;
  1431. procedure tdebuginfostabs.referencesections(list:taasmoutput);
  1432. var
  1433. hp : tused_unit;
  1434. begin
  1435. { Reference all DEBUGINFO sections from the main .text section }
  1436. if (target_info.system <> system_powerpc_macos) then
  1437. begin
  1438. { include reference to all debuginfo sections of used units }
  1439. hp:=tused_unit(usedunits.first);
  1440. while assigned(hp) do
  1441. begin
  1442. If (hp.u.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1443. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.u.globalsymtable,''),AT_DATA,0));
  1444. hp:=tused_unit(hp.next);
  1445. end;
  1446. { include reference to debuginfo for this program }
  1447. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1448. end;
  1449. end;
  1450. const
  1451. dbg_stabs_info : tdbginfo =
  1452. (
  1453. id : dbg_stabs;
  1454. idtxt : 'STABS';
  1455. );
  1456. initialization
  1457. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  1458. end.