dbgstabs.pas 60 KB

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