dbgstabs.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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,cfileutl,
  98. systems,globals,globtype,verbose,constexp,
  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_unicodestring,
  494. st_widestring:
  495. begin
  496. { looks like a pwidechar }
  497. ss:='*'+def_stab_number(cwidechartype);
  498. end;
  499. end;
  500. write_def_stabstr(list,def,ss);
  501. end;
  502. procedure TDebugInfoStabs.appenddef_enum(list:TAsmList;def:tenumdef);
  503. var
  504. st : ansistring;
  505. p : Tenumsym;
  506. begin
  507. { we can specify the size with @s<size>; prefix PM }
  508. if def.size <> std_param_align then
  509. st:='@s'+tostr(def.size*8)+';e'
  510. else
  511. st:='e';
  512. p := tenumsym(def.firstenum);
  513. while assigned(p) do
  514. begin
  515. st:=st+GetSymName(p)+':'+tostr(p.value)+',';
  516. p:=p.nextenum;
  517. end;
  518. { the final ',' is required to have a valid stabs }
  519. st:=st+';';
  520. write_def_stabstr(list,def,st);
  521. end;
  522. procedure TDebugInfoStabs.appenddef_ord(list:TAsmList;def:torddef);
  523. var
  524. ss : ansistring;
  525. begin
  526. ss:='';
  527. if cs_gdb_valgrind in current_settings.globalswitches then
  528. begin
  529. case def.ordtype of
  530. uvoid :
  531. ss:=strpnew(def_stab_number(def));
  532. pasbool,
  533. bool8bit,
  534. bool16bit,
  535. bool32bit,
  536. bool64bit :
  537. ss:=def_stabstr_evaluate(def,'r${numberstring};0;255;',[]);
  538. u32bit,
  539. s64bit,
  540. u64bit :
  541. ss:=def_stabstr_evaluate(def,'r${numberstring};0;-1;',[]);
  542. else
  543. ss:=def_stabstr_evaluate(def,'r${numberstring};$1;$2;',[tostr(longint(def.low.svalue)),tostr(longint(def.high.svalue))]);
  544. end;
  545. end
  546. else
  547. begin
  548. case def.ordtype of
  549. uvoid :
  550. ss:=strpnew(def_stab_number(def));
  551. uchar :
  552. ss:=strpnew('-20;');
  553. uwidechar :
  554. ss:=strpnew('-30;');
  555. pasbool,
  556. bool8bit :
  557. ss:=strpnew('-21;');
  558. bool16bit :
  559. ss:=strpnew('-22;');
  560. bool32bit :
  561. ss:=strpnew('-23;');
  562. bool64bit :
  563. { no clue if this is correct (FK) }
  564. ss:=strpnew('-23;');
  565. u64bit :
  566. ss:=strpnew('-32;');
  567. s64bit :
  568. ss:=strpnew('-31;');
  569. {u32bit : result:=def_stab_number(s32inttype)+';0;-1;'); }
  570. else
  571. ss:=def_stabstr_evaluate(def,'r${numberstring};$1;$2;',[tostr(longint(def.low.svalue)),tostr(longint(def.high.svalue))]);
  572. end;
  573. end;
  574. write_def_stabstr(list,def,ss);
  575. end;
  576. procedure TDebugInfoStabs.appenddef_float(list:TAsmList;def:tfloatdef);
  577. var
  578. ss : ansistring;
  579. begin
  580. ss:='';
  581. case def.floattype of
  582. s32real,
  583. s64real,
  584. s80real:
  585. ss:=def_stabstr_evaluate(def,'r$1;${savesize};0;',[def_stab_number(s32inttype)]);
  586. s64currency,
  587. s64comp:
  588. ss:=def_stabstr_evaluate(def,'r$1;-${savesize};0;',[def_stab_number(s32inttype)]);
  589. else
  590. internalerror(200509261);
  591. end;
  592. write_def_stabstr(list,def,ss);
  593. end;
  594. procedure TDebugInfoStabs.appenddef_file(list:TAsmList;def:tfiledef);
  595. var
  596. ss : ansistring;
  597. begin
  598. {$ifdef cpu64bitaddr}
  599. ss:=def_stabstr_evaluate(def,'s${savesize}HANDLE:$1,0,32;MODE:$1,32,32;RECSIZE:$2,64,64;'+
  600. '_PRIVATE:ar$1;1;64;$3,128,256;USERDATA:ar$1;1;16;$3,384,128;'+
  601. 'NAME:ar$1;0;255;$4,512,2048;;',[def_stab_number(s32inttype),
  602. def_stab_number(s64inttype),
  603. def_stab_number(u8inttype),
  604. def_stab_number(cchartype)]);
  605. {$else cpu64bitaddr}
  606. ss:=def_stabstr_evaluate(def,'s${savesize}HANDLE:$1,0,32;MODE:$1,32,32;RECSIZE:$1,64,32;'+
  607. '_PRIVATE:ar$1;1;32;$3,96,256;USERDATA:ar$1;1;16;$2,352,128;'+
  608. 'NAME:ar$1;0;255;$3,480,2048;;',[def_stab_number(s32inttype),
  609. def_stab_number(u8inttype),
  610. def_stab_number(cchartype)]);
  611. {$endif cpu64bitaddr}
  612. write_def_stabstr(list,def,ss);
  613. end;
  614. procedure TDebugInfoStabs.appenddef_record(list:TAsmList;def:trecorddef);
  615. var
  616. ss : ansistring;
  617. begin
  618. ss:='s'+tostr(def.size);
  619. def.symtable.SymList.ForEachCall(@field_add_stabstr,@ss);
  620. ss[length(ss)]:=';';
  621. write_def_stabstr(list,def,ss);
  622. end;
  623. procedure TDebugInfoStabs.appenddef_object(list:TAsmList;def:tobjectdef);
  624. procedure do_write_object(list:TAsmList;def:tobjectdef);
  625. var
  626. ss : ansistring;
  627. anc : tobjectdef;
  628. begin
  629. ss:='';
  630. { Write the invisible pointer for the class? }
  631. if (def.objecttype=odt_class) and
  632. (not def.writing_class_record_dbginfo) then
  633. begin
  634. ss:='*'+def_stab_classnumber(def);
  635. write_def_stabstr(list,def,ss);
  636. exit;
  637. end;
  638. ss:='s'+tostr(tobjecTSymtable(def.symtable).datasize);
  639. if assigned(def.childof) then
  640. begin
  641. {only one ancestor not virtual, public, at base offset 0 }
  642. { !1 , 0 2 0 , }
  643. ss:=ss+'!1,020,'+def_stab_classnumber(def.childof)+';';
  644. end;
  645. {virtual table to implement yet}
  646. def.symtable.symList.ForEachCall(@field_add_stabstr,@ss);
  647. if (oo_has_vmt in def.objectoptions) and
  648. (
  649. not assigned(def.childof) or
  650. not(oo_has_vmt in def.childof.objectoptions)
  651. ) then
  652. ss:=ss+'$vf'+def_stab_classnumber(def)+':'+def_stab_number(vmtarraytype)+','+tostr(def.vmt_offset*8)+';';
  653. def.symtable.symList.ForEachCall(@method_add_stabstr,@ss);
  654. if (oo_has_vmt in def.objectoptions) then
  655. begin
  656. anc := def;
  657. while assigned(anc.childof) and (oo_has_vmt in anc.childof.objectoptions) do
  658. anc := anc.childof;
  659. { just in case anc = self }
  660. ss:=ss+';~%'+def_stab_classnumber(anc)+';';
  661. end
  662. else
  663. ss:=ss+';';
  664. write_def_stabstr(list,def,ss);
  665. end;
  666. var
  667. oldtypesym : tsym;
  668. begin
  669. { classes require special code to write the record and the invisible pointer }
  670. if is_class(def) then
  671. begin
  672. { Write the record class itself }
  673. tobjectdef(def).writing_class_record_dbginfo:=true;
  674. do_write_object(list,def);
  675. tobjectdef(def).writing_class_record_dbginfo:=false;
  676. { Write the invisible pointer class }
  677. oldtypesym:=def.typesym;
  678. def.typesym:=nil;
  679. do_write_object(list,def);
  680. def.typesym:=oldtypesym;
  681. end
  682. else
  683. do_write_object(list,def);
  684. { VMT symbol }
  685. if (oo_has_vmt in tobjectdef(def).objectoptions) and
  686. assigned(def.owner) and
  687. assigned(def.owner.name) then
  688. list.concat(Tai_stab.create(stab_stabs,strpnew('"vmt_'+GetSymTableName(def.owner)+tobjectdef(def).objname^+':S'+
  689. def_stab_number(vmttype)+'",'+tostr(N_STSYM)+',0,0,'+tobjectdef(def).vmt_mangledname)));
  690. end;
  691. procedure TDebugInfoStabs.appenddef_variant(list:TAsmList;def:tvariantdef);
  692. var
  693. ss : ansistring;
  694. begin
  695. ss:=def_stabstr_evaluate(def,'${numberstring};',[]);
  696. write_def_stabstr(list,def,ss);
  697. end;
  698. procedure TDebugInfoStabs.appenddef_pointer(list:TAsmList;def:tpointerdef);
  699. var
  700. ss : ansistring;
  701. begin
  702. ss:='*'+def_stab_number(tpointerdef(def).pointeddef);
  703. write_def_stabstr(list,def,ss);
  704. end;
  705. procedure TDebugInfoStabs.appenddef_set(list:TAsmList;def:tsetdef);
  706. var
  707. ss : ansistring;
  708. begin
  709. ss:=def_stabstr_evaluate(def,'@s$1;S$2',[tostr(def.size*8),def_stab_number(tsetdef(def).elementdef)]);
  710. write_def_stabstr(list,def,ss);
  711. end;
  712. procedure TDebugInfoStabs.appenddef_formal(list:TAsmList;def:tformaldef);
  713. var
  714. ss : ansistring;
  715. begin
  716. ss:=def_stabstr_evaluate(def,'${numberstring};',[]);
  717. write_def_stabstr(list,def,ss);
  718. end;
  719. procedure TDebugInfoStabs.appenddef_array(list:TAsmList;def:tarraydef);
  720. var
  721. tempstr,
  722. ss : ansistring;
  723. begin
  724. if not is_packed_array(def) then
  725. ss:=def_stabstr_evaluate(def,'ar$1;$2;$3;$4',[def_stab_number(tarraydef(def).rangedef),
  726. tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange),def_stab_number(tarraydef(def).elementdef)])
  727. else
  728. begin
  729. // the @P seems to be ignored by gdb
  730. tempstr:=def_stabstr_evaluate(tarraydef(def).rangedef,'r${numberstring};$1;$2;',
  731. [tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange)]);
  732. // will only show highrange-lowrange+1 bits in gdb
  733. ss:=def_stabstr_evaluate(def,'@s$1;@S;S$2',
  734. [tostr(TConstExprInt(tarraydef(def).elepackedbitsize) * tarraydef(def).elecount),tempstr]);
  735. end;
  736. write_def_stabstr(list,def,ss);
  737. end;
  738. procedure TDebugInfoStabs.appenddef_procvar(list:TAsmList;def:tprocvardef);
  739. var
  740. ss : ansistring;
  741. begin
  742. ss:='*f'+def_stab_number(tprocvardef(def).returndef);
  743. write_def_stabstr(list,def,ss);
  744. end;
  745. procedure TDebugInfoStabs.appenddef_undefined(list:TAsmList;def:tundefineddef);
  746. var
  747. ss : ansistring;
  748. begin
  749. ss:=def_stabstr_evaluate(def,'${numberstring};',[]);
  750. write_def_stabstr(list,def,ss);
  751. end;
  752. procedure TDebugInfoStabs.beforeappenddef(list:TAsmList;def:tdef);
  753. var
  754. anc : tobjectdef;
  755. i : longint;
  756. begin
  757. { write dependencies first }
  758. case def.typ of
  759. stringdef :
  760. begin
  761. if tstringdef(def).stringtype in [st_widestring,st_unicodestring] then
  762. appenddef(list,cwidechartype)
  763. else
  764. begin
  765. appenddef(list,cchartype);
  766. appenddef(list,u8inttype);
  767. end;
  768. end;
  769. floatdef :
  770. appenddef(list,s32inttype);
  771. filedef :
  772. begin
  773. appenddef(list,s32inttype);
  774. {$ifdef cpu64bitaddr}
  775. appenddef(list,s64inttype);
  776. {$endif cpu64bitaddr}
  777. appenddef(list,u8inttype);
  778. appenddef(list,cchartype);
  779. end;
  780. classrefdef :
  781. appenddef(list,pvmttype);
  782. pointerdef :
  783. appenddef(list,tpointerdef(def).pointeddef);
  784. setdef :
  785. appenddef(list,tsetdef(def).elementdef);
  786. procvardef :
  787. begin
  788. appenddef(list,tprocvardef(def).returndef);
  789. if assigned(tprocvardef(def).parast) then
  790. write_symtable_defs(list,tprocvardef(def).parast);
  791. end;
  792. procdef :
  793. begin
  794. appenddef(list,tprocdef(def).returndef);
  795. if assigned(tprocdef(def).parast) then
  796. write_symtable_defs(list,tprocdef(def).parast);
  797. if assigned(tprocdef(def).localst) and
  798. (tprocdef(def).localst.symtabletype=localsymtable) then
  799. write_symtable_defs(list,tprocdef(def).localst);
  800. end;
  801. arraydef :
  802. begin
  803. appenddef(list,tarraydef(def).rangedef);
  804. appenddef(list,tarraydef(def).elementdef);
  805. end;
  806. recorddef :
  807. trecorddef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  808. enumdef :
  809. if assigned(tenumdef(def).basedef) then
  810. appenddef(list,tenumdef(def).basedef);
  811. objectdef :
  812. begin
  813. { make sure we don't write child classdefs before their parent }
  814. { classdefs, because this crashes gdb }
  815. anc:=tobjectdef(def);
  816. while assigned(anc.childof) do
  817. begin
  818. anc:=anc.childof;
  819. if (anc.dbg_state=dbg_state_writing) then
  820. { happens in case a field of a parent is of the (forward }
  821. { defined) child type }
  822. begin
  823. { We don't explicitly requeue it, but the fact that }
  824. { a child type was used in a parent before the child }
  825. { type was fully defined means that it was forward }
  826. { declared, and will still be encountered later (it }
  827. { cannot have been declared in another unit, because }
  828. { then this and that other unit would depend on }
  829. { eachother's interface) }
  830. { Setting the state to queued however allows us to }
  831. { get the def number already without an IE }
  832. def.dbg_state:=dbg_state_queued;
  833. exit;
  834. end;
  835. end;
  836. appenddef(list,vmtarraytype);
  837. if assigned(tobjectdef(def).ImplementedInterfaces) then
  838. for i:=0 to tobjectdef(def).ImplementedInterfaces.Count-1 do
  839. appenddef(list,TImplementedInterface(tobjectdef(def).ImplementedInterfaces[i]).IntfDef);
  840. { first the parents }
  841. anc:=tobjectdef(def);
  842. while assigned(anc.childof) do
  843. begin
  844. anc:=anc.childof;
  845. appenddef(list,anc);
  846. if assigned(anc.ImplementedInterfaces) then
  847. for i:=0 to anc.ImplementedInterfaces.Count-1 do
  848. appenddef(list,TImplementedInterface(anc.ImplementedInterfaces[i]).IntfDef);
  849. end;
  850. tobjectdef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  851. end;
  852. end;
  853. end;
  854. procedure TDebugInfoStabs.appendprocdef(list:TAsmList;def:tprocdef);
  855. var
  856. templist : TAsmList;
  857. stabsendlabel : tasmlabel;
  858. p : pchar;
  859. RType : Char;
  860. Obj,Info : String;
  861. hs : string;
  862. ss : ansistring;
  863. begin
  864. if not assigned(def.procstarttai) then
  865. exit;
  866. { mark as used so the local type defs also be written }
  867. def.dbg_state:=dbg_state_used;
  868. templist:=TAsmList.create;
  869. { end of procedure }
  870. current_asmdata.getlabel(stabsendlabel,alt_dbgtype);
  871. if assigned(def.funcretsym) and
  872. (tabstractnormalvarsym(def.funcretsym).refs>0) then
  873. begin
  874. if tabstractnormalvarsym(def.funcretsym).localloc.loc=LOC_REFERENCE then
  875. begin
  876. {$warning Need to add gdb support for ret in param register calling}
  877. if paramanager.ret_in_param(def.returndef,def.proccalloption) then
  878. hs:='X*'
  879. else
  880. hs:='X';
  881. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  882. '"'+GetSymName(def.procsym)+':'+hs+def_stab_number(def.returndef)+'",'+
  883. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(def.funcretsym).localloc.reference.offset))));
  884. if (m_result in current_settings.modeswitches) then
  885. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  886. '"RESULT:'+hs+def_stab_number(def.returndef)+'",'+
  887. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(def.funcretsym).localloc.reference.offset))));
  888. end;
  889. end;
  890. // LBRAC
  891. ss:=tostr(N_LBRAC)+',0,0,';
  892. if target_info.cpu=cpu_powerpc64 then
  893. ss:=ss+'.';
  894. ss:=ss+def.mangledname;
  895. if (tf_use_function_relative_addresses in target_info.flags) then
  896. begin
  897. ss:=ss+'-';
  898. if target_info.cpu=cpu_powerpc64 then
  899. ss:=ss+'.';
  900. ss:=ss+def.mangledname;
  901. end;
  902. getmem(p,length(ss)+1);
  903. move(pchar(ss)^,p^,length(ss)+1);
  904. templist.concat(Tai_stab.Create(stab_stabn,p));
  905. // RBRAC
  906. ss:=tostr(N_RBRAC)+',0,0,'+stabsendlabel.name;
  907. if (tf_use_function_relative_addresses in target_info.flags) then
  908. begin
  909. ss:=ss+'-';
  910. if target_info.cpu=cpu_powerpc64 then
  911. ss:=ss+'.';
  912. ss:=ss+def.mangledname;
  913. end;
  914. getmem(p,length(ss)+1);
  915. move(pchar(ss)^,p^,length(ss)+1);
  916. templist.concat(Tai_stab.Create(stab_stabn,p));
  917. { the stabsendlabel must come after all other stabs for this }
  918. { function }
  919. templist.concat(tai_label.create(stabsendlabel));
  920. { Add a "size" stab as described in the last paragraph of 2.5 at }
  921. { http://sourceware.org/gdb/current/onlinedocs/stabs_2.html#SEC12 }
  922. { This works at least on Darwin (and is needed on Darwin to get }
  923. { correct smartlinking of stabs), but I don't know which binutils }
  924. { version is required on other platforms }
  925. { This stab must come after all other stabs for the procedure, }
  926. { including the LBRAC/RBRAC ones }
  927. if (target_info.system in systems_darwin) then
  928. templist.concat(Tai_stab.create(stab_stabs,
  929. strpnew('"",'+tostr(N_FUNCTION)+',0,0,'+stabsendlabel.name+'-'+def.mangledname)));
  930. current_asmdata.asmlists[al_procedures].insertlistafter(def.procendtai,templist);
  931. { "The stab representing a procedure is located immediately
  932. following the code of the procedure. This stab is in turn
  933. directly followed by a group of other stabs describing
  934. elements of the procedure. These other stabs describe the
  935. procedure's parameters, its block local variables, and its
  936. block structure." (stab docs) }
  937. { this is however incorrect in case "include source" statements }
  938. { appear in the block, in that case the procedure stab must }
  939. { appear before this include stabs (and we generate such an }
  940. { stabs for all functions) (JM) }
  941. { FUNC stabs }
  942. obj := GetSymName(def.procsym);
  943. info := '';
  944. if (po_global in def.procoptions) then
  945. RType := 'F'
  946. else
  947. RType := 'f';
  948. if assigned(def.owner) then
  949. begin
  950. if (def.owner.symtabletype = objecTSymtable) then
  951. obj := GetSymTableName(def.owner)+'__'+GetSymName(def.procsym);
  952. if not(cs_gdb_valgrind in current_settings.globalswitches) and
  953. (def.owner.symtabletype=localsymtable) and
  954. assigned(def.owner.defowner) and
  955. assigned(tprocdef(def.owner.defowner).procsym) then
  956. info := ','+GetSymName(def.procsym)+','+GetSymName(tprocdef(def.owner.defowner).procsym);
  957. end;
  958. ss:='"'+ansistring(obj)+':'+RType+def_stab_number(def.returndef)+info+'",'+tostr(n_function)+',0,'+tostr(def.fileinfo.line)+','+ansistring(def.mangledname);
  959. getmem(p,length(ss)+1);
  960. move(pchar(ss)^,p^,length(ss)+1);
  961. templist.concat(Tai_stab.Create(stab_stabs,p));
  962. current_asmdata.asmlists[al_procedures].insertlistbefore(def.procstarttai,templist);
  963. { para types }
  964. if assigned(def.parast) then
  965. write_symtable_syms(templist,def.parast);
  966. { local type defs and vars should not be written
  967. inside the main proc stab }
  968. if assigned(def.localst) and
  969. (def.localst.symtabletype=localsymtable) then
  970. write_symtable_syms(templist,def.localst);
  971. current_asmdata.asmlists[al_procedures].insertlistbefore(def.procstarttai,templist);
  972. templist.free;
  973. end;
  974. {****************************************************************************
  975. TSym support
  976. ****************************************************************************}
  977. function TDebugInfoStabs.sym_var_value(const s:string;arg:pointer):string;
  978. var
  979. sym : tsym absolute arg;
  980. begin
  981. result:='';
  982. if s='name' then
  983. result:=GetSymName(sym)
  984. else if s='mangledname' then
  985. result:=sym.mangledname
  986. else if s='ownername' then
  987. result:=GetSymTableName(sym.owner)
  988. else if s='line' then
  989. result:=tostr(sym.fileinfo.line)
  990. else if s='N_LSYM' then
  991. result:=tostr(N_LSYM)
  992. else if s='N_LCSYM' then
  993. result:=tostr(N_LCSYM)
  994. else if s='N_RSYM' then
  995. result:=tostr(N_RSYM)
  996. else if s='N_TSYM' then
  997. result:=tostr(N_TSYM)
  998. else if s='N_STSYM' then
  999. result:=tostr(N_STSYM)
  1000. else if s='N_FUNCTION' then
  1001. result:=tostr(N_FUNCTION)
  1002. else
  1003. internalerror(200401152);
  1004. end;
  1005. function TDebugInfoStabs.sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):ansistring;
  1006. begin
  1007. result:=string_evaluate(s,@sym_var_value,sym,vars);
  1008. end;
  1009. procedure TDebugInfoStabs.write_sym_stabstr(list:TAsmList;sym:tsym;const ss:ansistring);
  1010. var
  1011. p : pchar;
  1012. begin
  1013. if ss='' then
  1014. exit;
  1015. { add to list }
  1016. getmem(p,length(ss)+1);
  1017. move(pchar(ss)^,p^,length(ss)+1);
  1018. list.concat(Tai_stab.create(stab_stabs,p));
  1019. end;
  1020. procedure TDebugInfoStabs.appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);
  1021. var
  1022. ss : ansistring;
  1023. begin
  1024. ss:='';
  1025. if (sym.owner.symtabletype=objecTSymtable) and
  1026. (sp_static in sym.symoptions) then
  1027. ss:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",${N_LCSYM},0,${line},${mangledname}',
  1028. [def_stab_number(sym.vardef)]);
  1029. write_sym_stabstr(list,sym,ss);
  1030. end;
  1031. procedure TDebugInfoStabs.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1032. var
  1033. ss : ansistring;
  1034. st : string;
  1035. threadvaroffset : string;
  1036. regidx : Tregisterindex;
  1037. nsym : string[7];
  1038. begin
  1039. { external symbols can't be resolved at link time, so we
  1040. can't generate stabs for them }
  1041. if vo_is_external in sym.varoptions then
  1042. exit;
  1043. ss:='';
  1044. st:=def_stab_number(sym.vardef);
  1045. case sym.localloc.loc of
  1046. LOC_REGISTER,
  1047. LOC_CREGISTER,
  1048. LOC_MMREGISTER,
  1049. LOC_CMMREGISTER,
  1050. LOC_FPUREGISTER,
  1051. LOC_CFPUREGISTER :
  1052. begin
  1053. regidx:=findreg_by_number(sym.localloc.register);
  1054. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1055. { this is the register order for GDB}
  1056. if regidx<>0 then
  1057. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1058. end;
  1059. else
  1060. begin
  1061. if (vo_is_thread_var in sym.varoptions) then
  1062. threadvaroffset:='+'+tostr(sizeof(pint))
  1063. else
  1064. threadvaroffset:='';
  1065. if (vo_is_typed_const in sym.varoptions) then
  1066. nsym:='N_STSYM'
  1067. else
  1068. nsym:='N_LCSYM';
  1069. { Here we used S instead of
  1070. because with G GDB doesn't look at the address field
  1071. but searches the same name or with a leading underscore
  1072. but these names don't exist in pascal !}
  1073. st:='S'+st;
  1074. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${'+nsym+'},0,${line},${mangledname}$2',[st,threadvaroffset]);
  1075. end;
  1076. end;
  1077. write_sym_stabstr(list,sym,ss);
  1078. end;
  1079. procedure TDebugInfoStabs.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  1080. var
  1081. ss : ansistring;
  1082. st : string;
  1083. regidx : Tregisterindex;
  1084. begin
  1085. { There is no space allocated for not referenced locals }
  1086. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1087. exit;
  1088. ss:='';
  1089. st:=def_stab_number(sym.vardef);
  1090. case sym.localloc.loc of
  1091. LOC_REGISTER,
  1092. LOC_CREGISTER,
  1093. LOC_MMREGISTER,
  1094. LOC_CMMREGISTER,
  1095. LOC_FPUREGISTER,
  1096. LOC_CFPUREGISTER :
  1097. begin
  1098. regidx:=findreg_by_number(sym.localloc.register);
  1099. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1100. { this is the register order for GDB}
  1101. if regidx<>0 then
  1102. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1103. end;
  1104. LOC_REFERENCE :
  1105. { offset to ebp => will not work if the framepointer is esp
  1106. so some optimizing will make things harder to debug }
  1107. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1108. else
  1109. internalerror(2003091814);
  1110. end;
  1111. write_sym_stabstr(list,sym,ss);
  1112. end;
  1113. procedure TDebugInfoStabs.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  1114. var
  1115. ss : ansistring;
  1116. st : string;
  1117. regidx : Tregisterindex;
  1118. c : char;
  1119. begin
  1120. ss:='';
  1121. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1122. { while stabs aren't adapted for regvars yet }
  1123. if (vo_is_self in sym.varoptions) then
  1124. begin
  1125. case sym.localloc.loc of
  1126. LOC_REGISTER,
  1127. LOC_CREGISTER:
  1128. regidx:=findreg_by_number(sym.localloc.register);
  1129. LOC_REFERENCE: ;
  1130. else
  1131. internalerror(2003091815);
  1132. end;
  1133. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1134. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1135. begin
  1136. if (sym.localloc.loc=LOC_REFERENCE) then
  1137. ss:=sym_stabstr_evaluate(sym,'"pvmt:p$1",${N_TSYM},0,0,$2',
  1138. [def_stab_number(pvmttype),tostr(sym.localloc.reference.offset)])
  1139. else
  1140. begin
  1141. regidx:=findreg_by_number(sym.localloc.register);
  1142. ss:=sym_stabstr_evaluate(sym,'"pvmt:r$1",${N_RSYM},0,0,$2',
  1143. [def_stab_number(pvmttype),tostr(regstabs_table[regidx])]);
  1144. end
  1145. end
  1146. else
  1147. begin
  1148. if not(is_class(tprocdef(sym.owner.defowner)._class)) then
  1149. c:='v'
  1150. else
  1151. c:='p';
  1152. if (sym.localloc.loc=LOC_REFERENCE) then
  1153. ss:=sym_stabstr_evaluate(sym,'"$$t:$1",${N_TSYM},0,0,$2',
  1154. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(sym.localloc.reference.offset)])
  1155. else
  1156. begin
  1157. regidx:=findreg_by_number(sym.localloc.register);
  1158. ss:=sym_stabstr_evaluate(sym,'"$$t:r$1",${N_RSYM},0,0,$2',
  1159. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(regstabs_table[regidx])]);
  1160. end
  1161. end;
  1162. end
  1163. else
  1164. begin
  1165. st:=def_stab_number(sym.vardef);
  1166. if paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1167. not(vo_has_local_copy in sym.varoptions) and
  1168. not is_open_string(sym.vardef) then
  1169. c:='v' { should be 'i' but 'i' doesn't work }
  1170. else
  1171. c:='p';
  1172. case sym.localloc.loc of
  1173. LOC_REGISTER,
  1174. LOC_CREGISTER,
  1175. LOC_MMREGISTER,
  1176. LOC_CMMREGISTER,
  1177. LOC_FPUREGISTER,
  1178. LOC_CFPUREGISTER :
  1179. begin
  1180. if c='p' then
  1181. c:='R'
  1182. else
  1183. c:='a';
  1184. st:=c+st;
  1185. regidx:=findreg_by_number(sym.localloc.register);
  1186. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1187. { this is the register order for GDB}
  1188. if regidx<>0 then
  1189. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_RSYM},0,${line},$2',[st,tostr(longint(regstabs_table[regidx]))]);
  1190. end;
  1191. LOC_REFERENCE :
  1192. begin
  1193. st:=c+st;
  1194. { offset to ebp => will not work if the framepointer is esp
  1195. so some optimizing will make things harder to debug }
  1196. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1197. end;
  1198. else
  1199. internalerror(2003091814);
  1200. end;
  1201. end;
  1202. write_sym_stabstr(list,sym,ss);
  1203. end;
  1204. procedure TDebugInfoStabs.appendsym_const(list:TAsmList;sym:tconstsym);
  1205. var
  1206. st : string;
  1207. ss : ansistring;
  1208. begin
  1209. ss:='';
  1210. { Don't write info for default parameter values, the N_Func breaks
  1211. the N_Func for the function itself.
  1212. Valgrind does not support constants }
  1213. if (sym.owner.symtabletype=parasymtable) or
  1214. (cs_gdb_valgrind in current_settings.globalswitches) then
  1215. exit;
  1216. case sym.consttyp of
  1217. conststring:
  1218. begin
  1219. if sym.value.len<200 then
  1220. st:='s'''+backspace_quote(octal_quote(strpas(pchar(sym.value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+''''
  1221. else
  1222. st:='<constant string too long>';
  1223. end;
  1224. constord:
  1225. st:='i'+tostr(sym.value.valueord);
  1226. constpointer:
  1227. st:='i'+tostr(sym.value.valueordptr);
  1228. constreal:
  1229. begin
  1230. system.str(pbestreal(sym.value.valueptr)^,st);
  1231. st := 'r'+st;
  1232. end;
  1233. else
  1234. begin
  1235. { if we don't know just put zero !! }
  1236. st:='i0';
  1237. end;
  1238. end;
  1239. ss:=sym_stabstr_evaluate(sym,'"${name}:c=$1;",${N_FUNCTION},0,${line},0',[st]);
  1240. write_sym_stabstr(list,sym,ss);
  1241. end;
  1242. procedure TDebugInfoStabs.appendsym_type(list:TAsmList;sym:ttypesym);
  1243. var
  1244. ss : ansistring;
  1245. stabchar : string[2];
  1246. begin
  1247. ss:='';
  1248. if not assigned(sym.typedef) then
  1249. internalerror(200509262);
  1250. if sym.typedef.typ in tagtypes then
  1251. stabchar:='Tt'
  1252. else
  1253. stabchar:='t';
  1254. ss:=sym_stabstr_evaluate(sym,'"${name}:$1$2",${N_LSYM},0,${line},0',[stabchar,def_stab_number(sym.typedef)]);
  1255. write_sym_stabstr(list,sym,ss);
  1256. end;
  1257. procedure TDebugInfoStabs.appendsym_label(list:TAsmList;sym:tlabelsym);
  1258. var
  1259. ss : ansistring;
  1260. begin
  1261. ss:=sym_stabstr_evaluate(sym,'"${name}",${N_LSYM},0,${line},0',[]);
  1262. write_sym_stabstr(list,sym,ss);
  1263. end;
  1264. {****************************************************************************
  1265. Proc/Module support
  1266. ****************************************************************************}
  1267. procedure tdebuginfostabs.inserttypeinfo;
  1268. var
  1269. stabsvarlist,
  1270. stabstypelist : TAsmList;
  1271. storefilepos : tfileposinfo;
  1272. i : longint;
  1273. begin
  1274. storefilepos:=current_filepos;
  1275. current_filepos:=current_module.mainfilepos;
  1276. global_stab_number:=0;
  1277. defnumberlist:=TFPObjectlist.create(false);
  1278. stabsvarlist:=TAsmList.create;
  1279. stabstypelist:=TAsmList.create;
  1280. { include symbol that will be referenced from the main to be sure to
  1281. include this debuginfo .o file }
  1282. current_module.flags:=current_module.flags or uf_has_debuginfo;
  1283. if not(target_info.system in systems_darwin) then
  1284. begin
  1285. new_section(current_asmdata.asmlists[al_stabs],sec_data,GetSymTableName(current_module.localsymtable),0);
  1286. current_asmdata.asmlists[al_stabs].concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1287. end
  1288. else
  1289. new_section(current_asmdata.asmlists[al_stabs],sec_code,GetSymTableName(current_module.localsymtable),0);
  1290. { write all global/local variables. This will flag all required tdefs }
  1291. if assigned(current_module.globalsymtable) then
  1292. write_symtable_syms(stabsvarlist,current_module.globalsymtable);
  1293. if assigned(current_module.localsymtable) then
  1294. write_symtable_syms(stabsvarlist,current_module.localsymtable);
  1295. { write all procedures and methods. This will flag all required tdefs }
  1296. if assigned(current_module.globalsymtable) then
  1297. write_symtable_procdefs(stabsvarlist,current_module.globalsymtable);
  1298. if assigned(current_module.localsymtable) then
  1299. write_symtable_procdefs(stabsvarlist,current_module.localsymtable);
  1300. { reset unit type info flag }
  1301. reset_unit_type_info;
  1302. { write used types from the used units }
  1303. write_used_unit_type_info(stabstypelist,current_module);
  1304. { last write the types from this unit }
  1305. if assigned(current_module.globalsymtable) then
  1306. write_symtable_defs(stabstypelist,current_module.globalsymtable);
  1307. if assigned(current_module.localsymtable) then
  1308. write_symtable_defs(stabstypelist,current_module.localsymtable);
  1309. current_asmdata.asmlists[al_stabs].concatlist(stabstypelist);
  1310. current_asmdata.asmlists[al_stabs].concatlist(stabsvarlist);
  1311. { reset stab numbers }
  1312. for i:=0 to defnumberlist.count-1 do
  1313. begin
  1314. if assigned(defnumberlist[i]) then
  1315. begin
  1316. tdef(defnumberlist[i]).stab_number:=0;
  1317. tdef(defnumberlist[i]).dbg_state:=dbg_state_unused;
  1318. end;
  1319. end;
  1320. defnumberlist.free;
  1321. defnumberlist:=nil;
  1322. stabsvarlist.free;
  1323. stabstypelist.free;
  1324. current_filepos:=storefilepos;
  1325. end;
  1326. procedure tdebuginfostabs.insertlineinfo(list:TAsmList);
  1327. var
  1328. currfileinfo,
  1329. lastfileinfo : tfileposinfo;
  1330. currfuncname : pshortstring;
  1331. currsectype : TAsmSectiontype;
  1332. hlabel : tasmlabel;
  1333. hp : tai;
  1334. infile : tinputfile;
  1335. begin
  1336. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1337. currfuncname:=nil;
  1338. currsectype:=sec_code;
  1339. hp:=Tai(list.first);
  1340. while assigned(hp) do
  1341. begin
  1342. case hp.typ of
  1343. ait_section :
  1344. currsectype:=tai_section(hp).sectype;
  1345. ait_function_name :
  1346. currfuncname:=tai_function_name(hp).funcname;
  1347. ait_force_line :
  1348. lastfileinfo.line:=-1;
  1349. end;
  1350. if (currsectype=sec_code) and
  1351. (hp.typ=ait_instruction) then
  1352. begin
  1353. currfileinfo:=tailineinfo(hp).fileinfo;
  1354. { file changed ? (must be before line info) }
  1355. if (currfileinfo.fileindex<>0) and
  1356. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  1357. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  1358. begin
  1359. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  1360. if assigned(infile) then
  1361. begin
  1362. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1363. { emit stabs }
  1364. if (infile.path^<>'') then
  1365. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_includefile)+
  1366. ',0,0,'+hlabel.name),hp);
  1367. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_includefile)+
  1368. ',0,0,'+hlabel.name),hp);
  1369. list.insertbefore(tai_label.create(hlabel),hp);
  1370. { force new line info }
  1371. lastfileinfo.line:=-1;
  1372. end;
  1373. end;
  1374. { line changed ? }
  1375. if (currfileinfo.line>lastfileinfo.line) and (currfileinfo.line<>0) then
  1376. begin
  1377. if assigned(currfuncname) and
  1378. (tf_use_function_relative_addresses in target_info.flags) then
  1379. begin
  1380. current_asmdata.getlabel(hlabel,alt_dbgline);
  1381. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(n_textline)+',0,'+tostr(currfileinfo.line)+','+
  1382. hlabel.name+' - '+{$IFDEF POWERPC64}'.'+{$ENDIF POWERPC64}currfuncname^),hp);
  1383. list.insertbefore(tai_label.create(hlabel),hp);
  1384. end
  1385. else
  1386. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  1387. end;
  1388. lastfileinfo:=currfileinfo;
  1389. end;
  1390. hp:=tai(hp.next);
  1391. end;
  1392. end;
  1393. procedure tdebuginfostabs.insertmoduleinfo;
  1394. var
  1395. hlabel : tasmlabel;
  1396. infile : tinputfile;
  1397. begin
  1398. { emit main source n_sourcefile for start of module }
  1399. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1400. infile:=current_module.sourcefiles.get_file(1);
  1401. new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),0,secorder_begin);
  1402. if not(target_info.system in systems_darwin) then
  1403. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(make_mangledname('DEBUGSTART',current_module.localsymtable,''),AT_DATA,0));
  1404. if (infile.path^<>'') then
  1405. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_sourcefile)+
  1406. ',0,0,'+hlabel.name));
  1407. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_sourcefile)+
  1408. ',0,0,'+hlabel.name));
  1409. current_asmdata.asmlists[al_start].concat(tai_label.create(hlabel));
  1410. { for darwin, you need a "module marker" too to work around }
  1411. { either some assembler or gdb bug (radar 4386531 according to a }
  1412. { comment in dbxout.c of Apple's gcc) }
  1413. if (target_info.system in systems_darwin) then
  1414. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(N_OSO)+',0,0,0'));
  1415. { emit empty n_sourcefile for end of module }
  1416. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1417. new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),0,secorder_end);
  1418. if not(target_info.system in systems_darwin) then
  1419. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(make_mangledname('DEBUGEND',current_module.localsymtable,''),AT_DATA,0));
  1420. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(n_sourcefile)+',0,0,'+hlabel.name));
  1421. current_asmdata.asmlists[al_end].concat(tai_label.create(hlabel));
  1422. end;
  1423. procedure tdebuginfostabs.referencesections(list:TAsmList);
  1424. var
  1425. hp : tmodule;
  1426. dbgtable : tai_symbol;
  1427. begin
  1428. { Reference all DEBUGINFO sections from the main .fpc section }
  1429. if (target_info.system in ([system_powerpc_macos]+systems_darwin)) then
  1430. exit;
  1431. list.concat(Tai_section.create(sec_fpc,'links',0));
  1432. { make sure the debuginfo doesn't get stripped out }
  1433. if (target_info.system in systems_darwin) then
  1434. begin
  1435. dbgtable:=tai_symbol.createname('DEBUGINFOTABLE',AT_DATA,0);
  1436. list.concat(tai_directive.create(asd_no_dead_strip,dbgtable.sym.name));
  1437. list.concat(dbgtable);
  1438. end;
  1439. { include reference to all debuginfo sections of used units }
  1440. hp:=tmodule(loaded_units.first);
  1441. while assigned(hp) do
  1442. begin
  1443. If (hp.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1444. begin
  1445. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.localsymtable,''),0));
  1446. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  1447. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  1448. end;
  1449. hp:=tmodule(hp.next);
  1450. end;
  1451. end;
  1452. const
  1453. dbg_stabs_info : tdbginfo =
  1454. (
  1455. id : dbg_stabs;
  1456. idtxt : 'STABS';
  1457. );
  1458. initialization
  1459. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  1460. end.