dbgstabs.pas 60 KB

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