dbgstabs.pas 60 KB

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