dbgstabs.pas 61 KB

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