dbgstabs.pas 65 KB

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