dbgstabs.pas 73 KB

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