dbgstabs.pas 74 KB

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