2
0

dbgstabs.pas 73 KB

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