dbgstabs.pas 63 KB

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