dbgstabs.pas 72 KB

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