dbgstabs.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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:TObject;arg:pointer);
  41. procedure method_add_stabstr(p:TObject;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. SysUtils,cutils,cfileutils,
  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 pshortstring;
  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[0];
  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]:=pshortstring(varptr);
  145. if varptr>@varvaluedata[maxdata] then
  146. internalerrorproc(200411152);
  147. pshortstring(varptr)^:=get_var_value(varname,get_var_value_arg);
  148. inc(len,length(pshortstring(varptr)^));
  149. inc(varptr,length(pshortstring(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.typ=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:TObject;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).vardef.size;
  294. { open arrays made overflows !! }
  295. if varsize>$fffffff then
  296. varsize:=$fffffff;
  297. newrec:=def_stabstr_evaluate(nil,'$1:$2,$3,$4;',[tfieldvarsym(p).name,
  298. spec+def_stab_number(tfieldvarsym(p).vardef),
  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. freemem(newrec);
  308. {This should be used for case !!}
  309. inc(state^.recoffset,Tfieldvarsym(p).vardef.size);
  310. end;
  311. end;
  312. procedure TDebugInfoStabs.method_add_stabstr(p:TObject;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.vardef.typ = 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.vardef.typesym) then
  365. begin
  366. arglength := length(Parasym.vardef.typesym.name);
  367. argnames := argnames + tostr(arglength)+Parasym.vardef.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;',[tsym(p).name,def_stab_number(pd),
  382. def_stab_number(pd.returndef),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. freemem(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.stringtype 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);
  412. bytest:=def_stab_number(u8inttype);
  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);
  419. bytest:=def_stab_number(u8inttype);
  420. longst:=def_stab_number(u32inttype);
  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);
  428. result:=strpnew('*'+charst);
  429. end;
  430. st_widestring:
  431. begin
  432. { looks like a pwidechar }
  433. charst:=def_stab_number(cwidechartype);
  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 current_settings.globalswitches then
  476. begin
  477. case def.ordtype of
  478. uvoid :
  479. result:=strpnew(def_stab_number(def));
  480. bool8bit,
  481. bool16bit,
  482. bool32bit,
  483. bool64bit :
  484. result:=def_stabstr_evaluate(def,'r${numberstring};0;255;',[]);
  485. u32bit,
  486. s64bit,
  487. u64bit :
  488. result:=def_stabstr_evaluate(def,'r${numberstring};0;-1;',[]);
  489. else
  490. result:=def_stabstr_evaluate(def,'r${numberstring};$1;$2;',[tostr(longint(def.low)),tostr(longint(def.high))]);
  491. end;
  492. end
  493. else
  494. begin
  495. case def.ordtype of
  496. uvoid :
  497. result:=strpnew(def_stab_number(def));
  498. uchar :
  499. result:=strpnew('-20;');
  500. uwidechar :
  501. result:=strpnew('-30;');
  502. bool8bit :
  503. result:=strpnew('-21;');
  504. bool16bit :
  505. result:=strpnew('-22;');
  506. bool32bit :
  507. result:=strpnew('-23;');
  508. bool64bit :
  509. { no clue if this is correct (FK) }
  510. result:=strpnew('-23;');
  511. u64bit :
  512. result:=strpnew('-32;');
  513. s64bit :
  514. result:=strpnew('-31;');
  515. {u32bit : result:=def_stab_number(s32inttype)+';0;-1;'); }
  516. else
  517. result:=def_stabstr_evaluate(def,'r${numberstring};$1;$2;',[tostr(longint(def.low)),tostr(longint(def.high))]);
  518. end;
  519. end;
  520. end;
  521. function floatdef_stabstr(def:tfloatdef):Pchar;
  522. begin
  523. case def.floattype of
  524. s32real,
  525. s64real,
  526. s80real:
  527. result:=def_stabstr_evaluate(def,'r$1;${savesize};0;',[def_stab_number(s32inttype)]);
  528. s64currency,
  529. s64comp:
  530. result:=def_stabstr_evaluate(def,'r$1;-${savesize};0;',[def_stab_number(s32inttype)]);
  531. else
  532. internalerror(200509261);
  533. end;
  534. end;
  535. function filedef_stabstr(def:tfiledef):pchar;
  536. begin
  537. {$ifdef cpu64bit}
  538. result:=def_stabstr_evaluate(def,'s${savesize}HANDLE:$1,0,32;MODE:$1,32,32;RECSIZE:$2,64,64;'+
  539. '_PRIVATE:ar$1;1;64;$3,128,256;USERDATA:ar$1;1;16;$3,384,128;'+
  540. 'NAME:ar$1;0;255;$4,512,2048;;',[def_stab_number(s32inttype),
  541. def_stab_number(s64inttype),
  542. def_stab_number(u8inttype),
  543. def_stab_number(cchartype)]);
  544. {$else cpu64bit}
  545. result:=def_stabstr_evaluate(def,'s${savesize}HANDLE:$1,0,32;MODE:$1,32,32;RECSIZE:$1,64,32;'+
  546. '_PRIVATE:ar$1;1;32;$3,96,256;USERDATA:ar$1;1;16;$2,352,128;'+
  547. 'NAME:ar$1;0;255;$3,480,2048;;',[def_stab_number(s32inttype),
  548. def_stab_number(u8inttype),
  549. def_stab_number(cchartype)]);
  550. {$endif cpu64bit}
  551. end;
  552. function procdef_stabstr(def:tprocdef):pchar;
  553. Var
  554. RType : Char;
  555. Obj,Info : String;
  556. stabsstr : string;
  557. p : pchar;
  558. begin
  559. obj := def.procsym.name;
  560. info := '';
  561. if (po_global in def.procoptions) then
  562. RType := 'F'
  563. else
  564. RType := 'f';
  565. if assigned(def.owner) then
  566. begin
  567. if (def.owner.symtabletype = objecTSymtable) then
  568. obj := def.owner.name^+'__'+def.procsym.name;
  569. if not(cs_gdb_valgrind in current_settings.globalswitches) and
  570. (def.owner.symtabletype=localsymtable) and
  571. assigned(def.owner.defowner) and
  572. assigned(tprocdef(def.owner.defowner).procsym) then
  573. info := ','+def.procsym.name+','+tprocdef(def.owner.defowner).procsym.name;
  574. end;
  575. stabsstr:=def.mangledname;
  576. getmem(p,length(stabsstr)+255);
  577. strpcopy(p,'"'+obj+':'+RType
  578. +def_stab_number(def.returndef)+info+'",'+tostr(n_function)
  579. +',0,'+
  580. tostr(def.fileinfo.line)
  581. +',');
  582. strpcopy(strend(p),stabsstr);
  583. getmem(result,strlen(p)+1);
  584. move(p^,result^,strlen(p)+1);
  585. freemem(p);
  586. end;
  587. function recorddef_stabstr(def:trecorddef):pchar;
  588. var
  589. state : Trecord_stabgen_state;
  590. begin
  591. getmem(state.stabstring,memsizeinc);
  592. state.staballoc:=memsizeinc;
  593. strpcopy(state.stabstring,'s'+tostr(def.size));
  594. state.recoffset:=0;
  595. state.stabsize:=strlen(state.stabstring);
  596. def.symtable.SymList.ForEachCall(@field_add_stabstr,@state);
  597. state.stabstring[state.stabsize]:=';';
  598. state.stabstring[state.stabsize+1]:=#0;
  599. reallocmem(state.stabstring,state.stabsize+2);
  600. result:=state.stabstring;
  601. end;
  602. function objectdef_stabstr(def:tobjectdef):pchar;
  603. var
  604. anc : tobjectdef;
  605. state :Trecord_stabgen_state;
  606. ts : string;
  607. begin
  608. { Write the invisible pointer for the class? }
  609. if (def.objecttype=odt_class) and
  610. (not def.writing_class_record_dbginfo) then
  611. begin
  612. result:=strpnew('*'+def_stab_classnumber(def));
  613. exit;
  614. end;
  615. state.staballoc:=memsizeinc;
  616. getmem(state.stabstring,state.staballoc);
  617. strpcopy(state.stabstring,'s'+tostr(tobjecTSymtable(def.symtable).datasize));
  618. if assigned(def.childof) then
  619. begin
  620. {only one ancestor not virtual, public, at base offset 0 }
  621. { !1 , 0 2 0 , }
  622. strpcopy(strend(state.stabstring),'!1,020,'+def_stab_classnumber(def.childof)+';');
  623. end;
  624. {virtual table to implement yet}
  625. state.recoffset:=0;
  626. state.stabsize:=strlen(state.stabstring);
  627. def.symtable.symList.ForEachCall(@field_add_stabstr,@state);
  628. if (oo_has_vmt in def.objectoptions) then
  629. if not assigned(def.childof) or not(oo_has_vmt in def.childof.objectoptions) then
  630. begin
  631. ts:='$vf'+def_stab_classnumber(def)+':'+def_stab_number(vmtarraytype)+','+tostr(def.vmt_offset*8)+';';
  632. strpcopy(state.stabstring+state.stabsize,ts);
  633. inc(state.stabsize,length(ts));
  634. end;
  635. def.symtable.symList.ForEachCall(@method_add_stabstr,@state);
  636. if (oo_has_vmt in def.objectoptions) then
  637. begin
  638. anc := def;
  639. while assigned(anc.childof) and (oo_has_vmt in anc.childof.objectoptions) do
  640. anc := anc.childof;
  641. { just in case anc = self }
  642. ts:=';~%'+def_stab_classnumber(anc)+';';
  643. end
  644. else
  645. ts:=';';
  646. strpcopy(state.stabstring+state.stabsize,ts);
  647. inc(state.stabsize,length(ts));
  648. reallocmem(state.stabstring,state.stabsize+1);
  649. result:=state.stabstring;
  650. end;
  651. begin
  652. result:=nil;
  653. case def.typ of
  654. stringdef :
  655. result:=stringdef_stabstr(tstringdef(def));
  656. enumdef :
  657. result:=enumdef_stabstr(tenumdef(def));
  658. orddef :
  659. result:=orddef_stabstr(torddef(def));
  660. floatdef :
  661. result:=floatdef_stabstr(tfloatdef(def));
  662. filedef :
  663. result:=filedef_stabstr(tfiledef(def));
  664. recorddef :
  665. result:=recorddef_stabstr(trecorddef(def));
  666. variantdef :
  667. result:=def_stabstr_evaluate(def,'formal${numberstring};',[]);
  668. pointerdef :
  669. result:=strpnew('*'+def_stab_number(tpointerdef(def).pointeddef));
  670. classrefdef :
  671. result:=strpnew(def_stab_number(pvmttype));
  672. setdef :
  673. result:=def_stabstr_evaluate(def,'@s$1;S$2',[tostr(def.size*8),def_stab_number(tsetdef(def).elementdef)]);
  674. formaldef :
  675. result:=def_stabstr_evaluate(def,'formal${numberstring};',[]);
  676. arraydef :
  677. if not is_packed_array(def) then
  678. result:=def_stabstr_evaluate(def,'ar$1;$2;$3;$4',[def_stab_number(tarraydef(def).rangedef),
  679. tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange),def_stab_number(tarraydef(def).elementdef)])
  680. else
  681. // will only show highrange-lowrange+1 bits in gdb
  682. result:=def_stabstr_evaluate(def,'@s$1;@S;S$2',
  683. [tostr(TConstExprInt(tarraydef(def).elepackedbitsize) * tarraydef(def).elecount),def_stabstr_evaluate(tarraydef(def).rangedef,'r${numberstring};$1;$2;',
  684. [tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange)
  685. ])]);
  686. // the @P seems to be ignored by gdb
  687. // result:=def_stabstr_evaluate(def,'ar$1;$2;$3;$4;@P;',[def_stab_number(tarraydef(def).rangedef),tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange),def_stab_number(tarraydef(def).elementdef)]);
  688. procdef :
  689. result:=procdef_stabstr(tprocdef(def));
  690. procvardef :
  691. result:=strpnew('*f'+def_stab_number(tprocvardef(def).returndef));
  692. objectdef :
  693. result:=objectdef_stabstr(tobjectdef(def));
  694. undefineddef :
  695. result:=def_stabstr_evaluate(def,'formal${numberstring};',[]);
  696. end;
  697. if result=nil then
  698. internalerror(200512203);
  699. end;
  700. procedure TDebugInfoStabs.write_def_stabstr(list:TAsmList;def:tdef);
  701. var
  702. stabchar : string[2];
  703. ss,st,su : pchar;
  704. begin
  705. { procdefs require a different stabs style without type prefix }
  706. if def.typ=procdef then
  707. begin
  708. st:=def_stabstr(def);
  709. { add to list }
  710. list.concat(Tai_stab.create(stab_stabs,st));
  711. end
  712. else
  713. begin
  714. { type prefix }
  715. if def.typ in tagtypes then
  716. stabchar := 'Tt'
  717. else
  718. stabchar := 't';
  719. { Here we maybe generate a type, so we have to use numberstring }
  720. if is_class(def) and
  721. tobjectdef(def).writing_class_record_dbginfo then
  722. st:=def_stabstr_evaluate(def,'"${sym_name}:$1$2=',[stabchar,def_stab_classnumber(tobjectdef(def))])
  723. else
  724. st:=def_stabstr_evaluate(def,'"${sym_name}:$1$2=',[stabchar,def_stab_number(def)]);
  725. ss:=def_stabstr(def);
  726. reallocmem(st,strlen(ss)+512);
  727. { line info is set to 0 for all defs, because the def can be in an other
  728. unit and then the linenumber is invalid in the current sourcefile }
  729. su:=def_stabstr_evaluate(def,'",${N_LSYM},0,0,0',[]);
  730. strcopy(strecopy(strend(st),ss),su);
  731. reallocmem(st,strlen(st)+1);
  732. freemem(ss);
  733. freemem(su);
  734. { add to list }
  735. list.concat(Tai_stab.create(stab_stabs,st));
  736. end;
  737. end;
  738. procedure TDebugInfoStabs.insertdef(list:TAsmList;def:tdef);
  739. var
  740. anc : tobjectdef;
  741. oldtypesym : tsym;
  742. begin
  743. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  744. exit;
  745. { never write generic template defs }
  746. if df_generic in def.defoptions then
  747. begin
  748. def.dbg_state:=dbg_state_written;
  749. exit;
  750. end;
  751. { to avoid infinite loops }
  752. def.dbg_state := dbg_state_writing;
  753. { write dependencies first }
  754. case def.typ of
  755. stringdef :
  756. begin
  757. if tstringdef(def).stringtype=st_widestring then
  758. insertdef(list,cwidechartype)
  759. else
  760. begin
  761. insertdef(list,cchartype);
  762. insertdef(list,u8inttype);
  763. end;
  764. end;
  765. floatdef :
  766. insertdef(list,s32inttype);
  767. filedef :
  768. begin
  769. insertdef(list,s32inttype);
  770. {$ifdef cpu64bit}
  771. insertdef(list,s64inttype);
  772. {$endif cpu64bit}
  773. insertdef(list,u8inttype);
  774. insertdef(list,cchartype);
  775. end;
  776. classrefdef :
  777. insertdef(list,pvmttype);
  778. pointerdef :
  779. insertdef(list,tpointerdef(def).pointeddef);
  780. setdef :
  781. insertdef(list,tsetdef(def).elementdef);
  782. procvardef,
  783. procdef :
  784. insertdef(list,tabstractprocdef(def).returndef);
  785. arraydef :
  786. begin
  787. insertdef(list,tarraydef(def).rangedef);
  788. insertdef(list,tarraydef(def).elementdef);
  789. end;
  790. recorddef :
  791. trecorddef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  792. enumdef :
  793. if assigned(tenumdef(def).basedef) then
  794. insertdef(list,tenumdef(def).basedef);
  795. objectdef :
  796. begin
  797. insertdef(list,vmtarraytype);
  798. { first the parents }
  799. anc:=tobjectdef(def);
  800. while assigned(anc.childof) do
  801. begin
  802. anc:=anc.childof;
  803. insertdef(list,anc);
  804. end;
  805. tobjectdef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  806. tobjectdef(def).symtable.symList.ForEachCall(@method_write_defs,list);
  807. end;
  808. end;
  809. case def.typ of
  810. objectdef :
  811. begin
  812. { classes require special code to write the record and the invisible pointer }
  813. if is_class(def) then
  814. begin
  815. { Write the record class itself }
  816. tobjectdef(def).writing_class_record_dbginfo:=true;
  817. write_def_stabstr(list,def);
  818. tobjectdef(def).writing_class_record_dbginfo:=false;
  819. { Write the invisible pointer class }
  820. oldtypesym:=def.typesym;
  821. def.typesym:=nil;
  822. write_def_stabstr(list,def);
  823. def.typesym:=oldtypesym;
  824. end
  825. else
  826. write_def_stabstr(list,def);
  827. { VMT symbol }
  828. if (oo_has_vmt in tobjectdef(def).objectoptions) and
  829. assigned(def.owner) and
  830. assigned(def.owner.name) then
  831. list.concat(Tai_stab.create(stab_stabs,strpnew('"vmt_'+def.owner.name^+tobjectdef(def).objname^+':S'+
  832. def_stab_number(vmttype)+'",'+tostr(N_STSYM)+',0,0,'+tobjectdef(def).vmt_mangledname)));
  833. end;
  834. procdef :
  835. begin
  836. { procdefs are handled separatly }
  837. end;
  838. else
  839. write_def_stabstr(list,def);
  840. end;
  841. def.dbg_state := dbg_state_written;
  842. end;
  843. procedure TDebugInfoStabs.write_symtable_defs(list:TAsmList;st:TSymtable);
  844. procedure dowritestabs(list:TAsmList;st:TSymtable);
  845. var
  846. def : tdef;
  847. i : longint;
  848. begin
  849. for i:=0 to st.DefList.Count-1 do
  850. begin
  851. def:=tdef(st.DefList[i]);
  852. if not(df_deleted in def.defoptions) and
  853. (def.dbg_state=dbg_state_used) then
  854. insertdef(list,def);
  855. end;
  856. end;
  857. var
  858. old_writing_def_stabs : boolean;
  859. begin
  860. case st.symtabletype of
  861. staticsymtable :
  862. list.concat(tai_comment.Create(strpnew('Defs - Begin Staticsymtable')));
  863. globalsymtable :
  864. list.concat(tai_comment.Create(strpnew('Defs - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  865. end;
  866. old_writing_def_stabs:=writing_def_stabs;
  867. writing_def_stabs:=true;
  868. dowritestabs(list,st);
  869. writing_def_stabs:=old_writing_def_stabs;
  870. case st.symtabletype of
  871. staticsymtable :
  872. list.concat(tai_comment.Create(strpnew('Defs - End Staticsymtable')));
  873. globalsymtable :
  874. list.concat(tai_comment.Create(strpnew('Defs - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  875. end;
  876. end;
  877. procedure TDebugInfoStabs.write_procdef(list:TAsmList;pd:tprocdef);
  878. var
  879. templist : TAsmList;
  880. stabsendlabel : tasmlabel;
  881. mangled_length : longint;
  882. p,p1 : pchar;
  883. hs : string;
  884. begin
  885. if assigned(pd.procstarttai) then
  886. begin
  887. templist:=TAsmList.create;
  888. { end of procedure }
  889. current_asmdata.getlabel(stabsendlabel,alt_dbgtype);
  890. templist.concat(tai_label.create(stabsendlabel));
  891. current_asmdata.asmlists[al_procedures].insertlistbefore(pd.procendtai,templist);
  892. if assigned(pd.funcretsym) and
  893. (tabstractnormalvarsym(pd.funcretsym).refs>0) then
  894. begin
  895. if tabstractnormalvarsym(pd.funcretsym).localloc.loc=LOC_REFERENCE then
  896. begin
  897. {$warning Need to add gdb support for ret in param register calling}
  898. if paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  899. hs:='X*'
  900. else
  901. hs:='X';
  902. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  903. '"'+pd.procsym.name+':'+hs+def_stab_number(pd.returndef)+'",'+
  904. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(pd.funcretsym).localloc.reference.offset))));
  905. if (m_result in current_settings.modeswitches) then
  906. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  907. '"RESULT:'+hs+def_stab_number(pd.returndef)+'",'+
  908. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(pd.funcretsym).localloc.reference.offset))));
  909. end;
  910. end;
  911. mangled_length:=length(pd.mangledname);
  912. getmem(p,2*mangled_length+50);
  913. strpcopy(p,tostr(N_LBRAC)+',0,0,');
  914. {$IFDEF POWERPC64}strpcopy(strend(p), '.');{$ENDIF POWERPC64}
  915. strpcopy(strend(p),pd.mangledname);
  916. if (tf_use_function_relative_addresses in target_info.flags) then
  917. begin
  918. strpcopy(strend(p),'-');
  919. {$IFDEF POWERPC64}strpcopy(strend(p), '.');{$ENDIF POWERPC64}
  920. strpcopy(strend(p),pd.mangledname);
  921. end;
  922. getmem(p1,strlen(p)+1);
  923. move(p^,p1^,strlen(p)+1);
  924. templist.concat(Tai_stab.Create(stab_stabn,p1));
  925. strpcopy(p,tostr(N_RBRAC)+',0,0,'+stabsendlabel.name);
  926. if (tf_use_function_relative_addresses in target_info.flags) then
  927. begin
  928. strpcopy(strend(p),'-');
  929. {$IFDEF POWERPC64}strpcopy(strend(p), '.');{$ENDIF POWERPC64}
  930. strpcopy(strend(p),pd.mangledname);
  931. end;
  932. getmem(p1,strlen(p)+1);
  933. move(p^,p1^,strlen(p)+1);
  934. templist.concat(Tai_stab.Create(stab_stabn,p1));
  935. freemem(p,2*mangled_length+50);
  936. current_asmdata.asmlists[al_procedures].insertlistafter(pd.procendtai,templist);
  937. { "The stab representing a procedure is located immediately
  938. following the code of the procedure. This stab is in turn
  939. directly followed by a group of other stabs describing
  940. elements of the procedure. These other stabs describe the
  941. procedure's parameters, its block local variables, and its
  942. block structure." (stab docs) }
  943. { this is however incorrect in case "include source" statements }
  944. { appear in the block, in that case the procedure stab must }
  945. { appear before this include stabs (and we generate such an }
  946. { stabs for all functions) (JM) }
  947. { procdef }
  948. write_def_stabstr(templist,pd);
  949. current_asmdata.asmlists[al_procedures].insertlistbefore(pd.procstarttai,templist);
  950. { para types }
  951. if assigned(pd.parast) then
  952. write_symtable_syms(templist,pd.parast);
  953. { local type defs and vars should not be written
  954. inside the main proc stab }
  955. if assigned(pd.localst) and
  956. (pd.localst.symtabletype=localsymtable) then
  957. write_symtable_syms(templist,pd.localst);
  958. { after the endtai, because the ".size" must come before it }
  959. current_asmdata.asmlists[al_procedures].insertlistafter(pd.procendtai,templist);
  960. templist.free;
  961. end;
  962. end;
  963. {****************************************************************************
  964. TSym support
  965. ****************************************************************************}
  966. function TDebugInfoStabs.sym_var_value(const s:string;arg:pointer):string;
  967. var
  968. sym : tsym;
  969. begin
  970. sym:=tsym(arg);
  971. result:='';
  972. if s='name' then
  973. result:=sym.name
  974. else if s='mangledname' then
  975. result:=sym.mangledname
  976. else if s='ownername' then
  977. result:=sym.owner.name^
  978. else if s='line' then
  979. result:=tostr(sym.fileinfo.line)
  980. else if s='N_LSYM' then
  981. result:=tostr(N_LSYM)
  982. else if s='N_LCSYM' then
  983. result:=tostr(N_LCSYM)
  984. else if s='N_RSYM' then
  985. result:=tostr(N_RSYM)
  986. else if s='N_TSYM' then
  987. result:=tostr(N_TSYM)
  988. else if s='N_STSYM' then
  989. result:=tostr(N_STSYM)
  990. else if s='N_FUNCTION' then
  991. result:=tostr(N_FUNCTION)
  992. else
  993. internalerror(200401152);
  994. end;
  995. function TDebugInfoStabs.sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):Pchar;
  996. begin
  997. result:=string_evaluate(s,@sym_var_value,sym,vars);
  998. end;
  999. procedure TDebugInfoStabs.insertsym(list:TAsmList;sym:tsym);
  1000. function fieldvarsym_stabstr(sym:tfieldvarsym):Pchar;
  1001. begin
  1002. result:=nil;
  1003. if (sym.owner.symtabletype=objecTSymtable) and
  1004. (sp_static in sym.symoptions) then
  1005. result:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",${N_LCSYM},0,${line},${mangledname}',
  1006. [def_stab_number(sym.vardef)]);
  1007. end;
  1008. function globalvarsym_stabstr(sym:tglobalvarsym):Pchar;
  1009. var
  1010. st : string;
  1011. threadvaroffset : string;
  1012. regidx : Tregisterindex;
  1013. begin
  1014. result:=nil;
  1015. { external symbols can't be resolved at link time, so we
  1016. can't generate stabs for them }
  1017. if vo_is_external in sym.varoptions then
  1018. exit;
  1019. st:=def_stab_number(sym.vardef);
  1020. case sym.localloc.loc of
  1021. LOC_REGISTER,
  1022. LOC_CREGISTER,
  1023. LOC_MMREGISTER,
  1024. LOC_CMMREGISTER,
  1025. LOC_FPUREGISTER,
  1026. LOC_CFPUREGISTER :
  1027. begin
  1028. regidx:=findreg_by_number(sym.localloc.register);
  1029. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1030. { this is the register order for GDB}
  1031. if regidx<>0 then
  1032. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1033. end;
  1034. else
  1035. begin
  1036. if (vo_is_thread_var in sym.varoptions) then
  1037. threadvaroffset:='+'+tostr(sizeof(aint))
  1038. else
  1039. threadvaroffset:='';
  1040. { Here we used S instead of
  1041. because with G GDB doesn't look at the address field
  1042. but searches the same name or with a leading underscore
  1043. but these names don't exist in pascal !}
  1044. st:='S'+st;
  1045. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_LCSYM},0,${line},${mangledname}$2',[st,threadvaroffset]);
  1046. end;
  1047. end;
  1048. end;
  1049. function localvarsym_stabstr(sym:tlocalvarsym):Pchar;
  1050. var
  1051. st : string;
  1052. regidx : Tregisterindex;
  1053. begin
  1054. result:=nil;
  1055. { There is no space allocated for not referenced locals }
  1056. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1057. exit;
  1058. st:=def_stab_number(sym.vardef);
  1059. case sym.localloc.loc of
  1060. LOC_REGISTER,
  1061. LOC_CREGISTER,
  1062. LOC_MMREGISTER,
  1063. LOC_CMMREGISTER,
  1064. LOC_FPUREGISTER,
  1065. LOC_CFPUREGISTER :
  1066. begin
  1067. regidx:=findreg_by_number(sym.localloc.register);
  1068. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1069. { this is the register order for GDB}
  1070. if regidx<>0 then
  1071. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1072. end;
  1073. LOC_REFERENCE :
  1074. { offset to ebp => will not work if the framepointer is esp
  1075. so some optimizing will make things harder to debug }
  1076. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1077. else
  1078. internalerror(2003091814);
  1079. end;
  1080. end;
  1081. function paravarsym_stabstr(sym:tparavarsym):Pchar;
  1082. var
  1083. st : string;
  1084. regidx : Tregisterindex;
  1085. c : char;
  1086. begin
  1087. result:=nil;
  1088. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1089. { while stabs aren't adapted for regvars yet }
  1090. if (vo_is_self in sym.varoptions) then
  1091. begin
  1092. case sym.localloc.loc of
  1093. LOC_REGISTER,
  1094. LOC_CREGISTER:
  1095. regidx:=findreg_by_number(sym.localloc.register);
  1096. LOC_REFERENCE: ;
  1097. else
  1098. internalerror(2003091815);
  1099. end;
  1100. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1101. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1102. begin
  1103. if (sym.localloc.loc=LOC_REFERENCE) then
  1104. result:=sym_stabstr_evaluate(sym,'"pvmt:p$1",${N_TSYM},0,0,$2',
  1105. [def_stab_number(pvmttype),tostr(sym.localloc.reference.offset)]);
  1106. (* else
  1107. result:=sym_stabstr_evaluate(sym,'"pvmt:r$1",${N_RSYM},0,0,$2',
  1108. [def_stab_number(pvmttype),tostr(regstabs_table[regidx])]) *)
  1109. end
  1110. else
  1111. begin
  1112. if not(is_class(tprocdef(sym.owner.defowner)._class)) then
  1113. c:='v'
  1114. else
  1115. c:='p';
  1116. if (sym.localloc.loc=LOC_REFERENCE) then
  1117. result:=sym_stabstr_evaluate(sym,'"$$t:$1",${N_TSYM},0,0,$2',
  1118. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(sym.localloc.reference.offset)]);
  1119. (* else
  1120. result:=sym_stabstr_evaluate(sym,'"$$t:r$1",${N_RSYM},0,0,$2',
  1121. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(regstabs_table[regidx])]); *)
  1122. end;
  1123. end
  1124. else
  1125. begin
  1126. st:=def_stab_number(sym.vardef);
  1127. if paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1128. not(vo_has_local_copy in sym.varoptions) and
  1129. not is_open_string(sym.vardef) then
  1130. st := 'v'+st { should be 'i' but 'i' doesn't work }
  1131. else
  1132. st := 'p'+st;
  1133. case sym.localloc.loc of
  1134. LOC_REGISTER,
  1135. LOC_CREGISTER,
  1136. LOC_MMREGISTER,
  1137. LOC_CMMREGISTER,
  1138. LOC_FPUREGISTER,
  1139. LOC_CFPUREGISTER :
  1140. begin
  1141. regidx:=findreg_by_number(sym.localloc.register);
  1142. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1143. { this is the register order for GDB}
  1144. if regidx<>0 then
  1145. result:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(longint(regstabs_table[regidx]))]);
  1146. end;
  1147. LOC_REFERENCE :
  1148. { offset to ebp => will not work if the framepointer is esp
  1149. so some optimizing will make things harder to debug }
  1150. result:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1151. else
  1152. internalerror(2003091814);
  1153. end;
  1154. end;
  1155. end;
  1156. function constsym_stabstr(sym:tconstsym):Pchar;
  1157. var
  1158. st : string;
  1159. begin
  1160. result:=nil;
  1161. { Don't write info for default parameter values, the N_Func breaks
  1162. the N_Func for the function itself.
  1163. Valgrind does not support constants }
  1164. if (sym.owner.symtabletype=parasymtable) or
  1165. (cs_gdb_valgrind in current_settings.globalswitches) then
  1166. exit;
  1167. case sym.consttyp of
  1168. conststring:
  1169. begin
  1170. if sym.value.len<200 then
  1171. st:='s'''+backspace_quote(octal_quote(strpas(pchar(sym.value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+''''
  1172. else
  1173. st:='<constant string too long>';
  1174. end;
  1175. constord:
  1176. st:='i'+tostr(sym.value.valueord);
  1177. constpointer:
  1178. st:='i'+tostr(sym.value.valueordptr);
  1179. constreal:
  1180. begin
  1181. system.str(pbestreal(sym.value.valueptr)^,st);
  1182. st := 'r'+st;
  1183. end;
  1184. else
  1185. begin
  1186. { if we don't know just put zero !! }
  1187. st:='i0';
  1188. end;
  1189. end;
  1190. result:=sym_stabstr_evaluate(sym,'"${name}:c=$1;",${N_FUNCTION},0,${line},0',[st]);
  1191. end;
  1192. function typesym_stabstr(sym:ttypesym) : pchar;
  1193. var
  1194. stabchar : string[2];
  1195. begin
  1196. result:=nil;
  1197. if not assigned(sym.typedef) then
  1198. internalerror(200509262);
  1199. if sym.typedef.typ in tagtypes then
  1200. stabchar:='Tt'
  1201. else
  1202. stabchar:='t';
  1203. result:=sym_stabstr_evaluate(sym,'"${name}:$1$2",${N_LSYM},0,${line},0',[stabchar,def_stab_number(sym.typedef)]);
  1204. end;
  1205. function procsym_stabstr(sym:tprocsym) : pchar;
  1206. var
  1207. i : longint;
  1208. begin
  1209. result:=nil;
  1210. for i:=1 to sym.procdef_count do
  1211. write_procdef(list,sym.procdef[i]);
  1212. end;
  1213. var
  1214. stabstr : Pchar;
  1215. begin
  1216. stabstr:=nil;
  1217. case sym.typ of
  1218. labelsym :
  1219. stabstr:=sym_stabstr_evaluate(sym,'"${name}",${N_LSYM},0,${line},0',[]);
  1220. fieldvarsym :
  1221. stabstr:=fieldvarsym_stabstr(tfieldvarsym(sym));
  1222. globalvarsym :
  1223. stabstr:=globalvarsym_stabstr(tglobalvarsym(sym));
  1224. localvarsym :
  1225. stabstr:=localvarsym_stabstr(tlocalvarsym(sym));
  1226. paravarsym :
  1227. stabstr:=paravarsym_stabstr(tparavarsym(sym));
  1228. typedconstsym :
  1229. stabstr:=sym_stabstr_evaluate(sym,'"${name}:S$1",${N_STSYM},0,${line},${mangledname}',
  1230. [def_stab_number(ttypedconstsym(sym).typedconstdef)]);
  1231. constsym :
  1232. stabstr:=constsym_stabstr(tconstsym(sym));
  1233. typesym :
  1234. stabstr:=typesym_stabstr(ttypesym(sym));
  1235. procsym :
  1236. stabstr:=procsym_stabstr(tprocsym(sym));
  1237. end;
  1238. if stabstr<>nil then
  1239. list.concat(Tai_stab.create(stab_stabs,stabstr));
  1240. { For object types write also the symtable entries }
  1241. if (sym.typ=typesym) and (ttypesym(sym).typedef.typ=objectdef) then
  1242. write_symtable_syms(list,tobjectdef(ttypesym(sym).typedef).symtable);
  1243. sym.isdbgwritten:=true;
  1244. end;
  1245. procedure TDebugInfoStabs.write_symtable_syms(list:TAsmList;st:TSymtable);
  1246. var
  1247. sym : tsym;
  1248. i : longint;
  1249. begin
  1250. case st.symtabletype of
  1251. staticsymtable :
  1252. list.concat(tai_comment.Create(strpnew('Syms - Begin Staticsymtable')));
  1253. globalsymtable :
  1254. list.concat(tai_comment.Create(strpnew('Syms - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  1255. end;
  1256. for i:=0 to st.SymList.Count-1 do
  1257. begin
  1258. sym:=tsym(st.SymList[i]);
  1259. if not(sp_hidden in sym.symoptions) and
  1260. (not sym.isdbgwritten) then
  1261. insertsym(list,sym);
  1262. end;
  1263. case st.symtabletype of
  1264. staticsymtable :
  1265. list.concat(tai_comment.Create(strpnew('Syms - End Staticsymtable')));
  1266. globalsymtable :
  1267. list.concat(tai_comment.Create(strpnew('Syms - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  1268. end;
  1269. end;
  1270. {****************************************************************************
  1271. Proc/Module support
  1272. ****************************************************************************}
  1273. procedure tdebuginfostabs.inserttypeinfo;
  1274. var
  1275. stabsvarlist,
  1276. stabstypelist : TAsmList;
  1277. storefilepos : tfileposinfo;
  1278. i : longint;
  1279. begin
  1280. storefilepos:=current_filepos;
  1281. current_filepos:=current_module.mainfilepos;
  1282. global_stab_number:=0;
  1283. defnumberlist:=TFPObjectlist.create(false);
  1284. stabsvarlist:=TAsmList.create;
  1285. stabstypelist:=TAsmList.create;
  1286. { include symbol that will be referenced from the main to be sure to
  1287. include this debuginfo .o file }
  1288. current_module.flags:=current_module.flags or uf_has_debuginfo;
  1289. new_section(current_asmdata.asmlists[al_stabs],sec_data,current_module.localsymtable.name^,0);
  1290. current_asmdata.asmlists[al_stabs].concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1291. { first write all global/local symbols. This will flag all required tdefs }
  1292. if assigned(current_module.globalsymtable) then
  1293. write_symtable_syms(stabsvarlist,current_module.globalsymtable);
  1294. if assigned(current_module.localsymtable) then
  1295. write_symtable_syms(stabsvarlist,current_module.localsymtable);
  1296. { reset unit type info flag }
  1297. reset_unit_type_info;
  1298. { write used types from the used units }
  1299. write_used_unit_type_info(stabstypelist,current_module);
  1300. { last write the types from this unit }
  1301. if assigned(current_module.globalsymtable) then
  1302. write_symtable_defs(stabstypelist,current_module.globalsymtable);
  1303. if assigned(current_module.localsymtable) then
  1304. write_symtable_defs(stabstypelist,current_module.localsymtable);
  1305. current_asmdata.asmlists[al_stabs].concatlist(stabstypelist);
  1306. current_asmdata.asmlists[al_stabs].concatlist(stabsvarlist);
  1307. { reset stab numbers }
  1308. for i:=0 to defnumberlist.count-1 do
  1309. begin
  1310. if assigned(defnumberlist[i]) then
  1311. begin
  1312. tdef(defnumberlist[i]).stab_number:=0;
  1313. tdef(defnumberlist[i]).dbg_state:=dbg_state_unused;
  1314. end;
  1315. end;
  1316. defnumberlist.free;
  1317. defnumberlist:=nil;
  1318. stabsvarlist.free;
  1319. stabstypelist.free;
  1320. current_filepos:=storefilepos;
  1321. end;
  1322. procedure tdebuginfostabs.insertlineinfo(list:TAsmList);
  1323. var
  1324. currfileinfo,
  1325. lastfileinfo : tfileposinfo;
  1326. currfuncname : pshortstring;
  1327. currsectype : TAsmSectiontype;
  1328. hlabel : tasmlabel;
  1329. hp : tai;
  1330. infile : tinputfile;
  1331. begin
  1332. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1333. currfuncname:=nil;
  1334. currsectype:=sec_code;
  1335. hp:=Tai(list.first);
  1336. while assigned(hp) do
  1337. begin
  1338. case hp.typ of
  1339. ait_section :
  1340. currsectype:=tai_section(hp).sectype;
  1341. ait_function_name :
  1342. currfuncname:=tai_function_name(hp).funcname;
  1343. ait_force_line :
  1344. lastfileinfo.line:=-1;
  1345. end;
  1346. if (currsectype=sec_code) and
  1347. (hp.typ=ait_instruction) then
  1348. begin
  1349. currfileinfo:=tailineinfo(hp).fileinfo;
  1350. { file changed ? (must be before line info) }
  1351. if (currfileinfo.fileindex<>0) and
  1352. (lastfileinfo.fileindex<>currfileinfo.fileindex) then
  1353. begin
  1354. infile:=current_module.sourcefiles.get_file(currfileinfo.fileindex);
  1355. if assigned(infile) then
  1356. begin
  1357. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1358. { emit stabs }
  1359. if (infile.path^<>'') then
  1360. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_includefile)+
  1361. ',0,0,'+hlabel.name),hp);
  1362. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_includefile)+
  1363. ',0,0,'+hlabel.name),hp);
  1364. list.insertbefore(tai_label.create(hlabel),hp);
  1365. { force new line info }
  1366. lastfileinfo.line:=-1;
  1367. end;
  1368. end;
  1369. { line changed ? }
  1370. if (currfileinfo.line>lastfileinfo.line) and (currfileinfo.line<>0) then
  1371. begin
  1372. if assigned(currfuncname) and
  1373. (tf_use_function_relative_addresses in target_info.flags) then
  1374. begin
  1375. current_asmdata.getlabel(hlabel,alt_dbgline);
  1376. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(n_textline)+',0,'+tostr(currfileinfo.line)+','+
  1377. hlabel.name+' - '+{$IFDEF POWERPC64}'.'+{$ENDIF POWERPC64}currfuncname^),hp);
  1378. list.insertbefore(tai_label.create(hlabel),hp);
  1379. end
  1380. else
  1381. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  1382. end;
  1383. lastfileinfo:=currfileinfo;
  1384. end;
  1385. hp:=tai(hp.next);
  1386. end;
  1387. end;
  1388. procedure tdebuginfostabs.insertmoduleinfo;
  1389. var
  1390. hlabel : tasmlabel;
  1391. infile : tinputfile;
  1392. begin
  1393. { emit main source n_sourcefile for start of module }
  1394. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1395. infile:=current_module.sourcefiles.get_file(1);
  1396. new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),0,secorder_begin);
  1397. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(make_mangledname('DEBUGSTART',current_module.localsymtable,''),AT_DATA,0));
  1398. if (infile.path^<>'') then
  1399. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_sourcefile)+
  1400. ',0,0,'+hlabel.name));
  1401. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_sourcefile)+
  1402. ',0,0,'+hlabel.name));
  1403. current_asmdata.asmlists[al_start].concat(tai_label.create(hlabel));
  1404. { emit empty n_sourcefile for end of module }
  1405. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1406. new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),0,secorder_end);
  1407. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(make_mangledname('DEBUGEND',current_module.localsymtable,''),AT_DATA,0));
  1408. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(n_sourcefile)+',0,0,'+hlabel.name));
  1409. current_asmdata.asmlists[al_end].concat(tai_label.create(hlabel));
  1410. end;
  1411. procedure tdebuginfostabs.referencesections(list:TAsmList);
  1412. var
  1413. hp : tmodule;
  1414. dbgtable : tai_symbol;
  1415. begin
  1416. { Reference all DEBUGINFO sections from the main .text section }
  1417. if (target_info.system=system_powerpc_macos) then
  1418. exit;
  1419. list.concat(Tai_section.create(sec_data,'',0));
  1420. { make sure the debuginfo doesn't get stripped out }
  1421. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1422. begin
  1423. dbgtable:=tai_symbol.createname('DEBUGINFOTABLE',AT_DATA,0);
  1424. list.concat(tai_directive.create(asd_no_dead_strip,dbgtable.sym.name));
  1425. list.concat(dbgtable);
  1426. end;
  1427. { include reference to all debuginfo sections of used units }
  1428. hp:=tmodule(loaded_units.first);
  1429. while assigned(hp) do
  1430. begin
  1431. If (hp.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1432. begin
  1433. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.localsymtable,''),0));
  1434. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  1435. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  1436. end;
  1437. hp:=tmodule(hp.next);
  1438. end;
  1439. end;
  1440. const
  1441. dbg_stabs_info : tdbginfo =
  1442. (
  1443. id : dbg_stabs;
  1444. idtxt : 'STABS';
  1445. );
  1446. initialization
  1447. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  1448. end.