2
0

dbgstabs.pas 73 KB

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