dbgstabs.pas 61 KB

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