dbgstabs.pas 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. current_procdef:=prev_procdef;
  1078. end;
  1079. {****************************************************************************
  1080. TSym support
  1081. ****************************************************************************}
  1082. function TDebugInfoStabs.sym_var_value(const s:string;arg:pointer):string;
  1083. var
  1084. sym : tsym absolute arg;
  1085. begin
  1086. result:='';
  1087. if s='name' then
  1088. result:=GetSymName(sym)
  1089. else if s='mangledname' then
  1090. result:=ApplyAsmSymbolRestrictions(sym.mangledname)
  1091. else if s='ownername' then
  1092. result:=GetSymTableName(sym.owner)
  1093. else if s='line' then
  1094. result:=tostr(sym.fileinfo.line)
  1095. else
  1096. internalerror(200401152);
  1097. end;
  1098. function TDebugInfoStabs.sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):ansistring;
  1099. begin
  1100. result:=string_evaluate(s,@sym_var_value,sym,vars);
  1101. end;
  1102. procedure TDebugInfoStabs.write_sym_stabstr(list:TAsmList;sym:tsym;const ss:ansistring);
  1103. begin
  1104. if ss='' then
  1105. exit;
  1106. { add to list }
  1107. list.concat(Tai_stab.create_ansistr(stabsdir,ss));
  1108. end;
  1109. function TDebugInfoStabs.staticvarsym_mangled_name(sym: tstaticvarsym): string;
  1110. begin
  1111. result:=ApplyAsmSymbolRestrictions(sym.mangledname);
  1112. end;
  1113. procedure TDebugInfoStabs.maybe_add_vmt_sym(list: TAsmList; def: tobjectdef);
  1114. begin
  1115. if (oo_has_vmt in def.objectoptions) and
  1116. assigned(def.owner) and
  1117. assigned(def.owner.name) then
  1118. list.concat(Tai_stab.create_ansistr(stabsdir,ansistring('"vmt_')+GetSymTableName(def.owner)+tobjectdef(def).objname^+':S'+
  1119. def_stab_number(vmttype)+'",'+
  1120. base_stabs_str(globalvarsym_inited_stab,'0','0',ApplyAsmSymbolRestrictions(tobjectdef(def).vmt_mangledname))));
  1121. end;
  1122. procedure TDebugInfoStabs.appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);
  1123. var
  1124. ss : ansistring;
  1125. begin
  1126. ss:='';
  1127. if (sym.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1128. (sp_static in sym.symoptions) then
  1129. ss:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",'+base_stabs_str(globalvarsym_uninited_stab,'0','${line}','${mangledname}'),
  1130. [def_stab_number(sym.vardef)]);
  1131. write_sym_stabstr(list,sym,ss);
  1132. end;
  1133. procedure TDebugInfoStabs.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1134. var
  1135. ss : ansistring;
  1136. st : string;
  1137. threadvaroffset : string;
  1138. regidx : Tregisterindex;
  1139. nsym : byte;
  1140. begin
  1141. { external symbols can't be resolved at link time, so we
  1142. can't generate stabs for them }
  1143. if vo_is_external in sym.varoptions then
  1144. exit;
  1145. ss:='';
  1146. st:=def_stab_number(sym.vardef);
  1147. case sym.localloc.loc of
  1148. LOC_REGISTER,
  1149. LOC_CREGISTER,
  1150. LOC_MMREGISTER,
  1151. LOC_CMMREGISTER,
  1152. LOC_FPUREGISTER,
  1153. LOC_CFPUREGISTER :
  1154. begin
  1155. regidx:=findreg_by_number(sym.localloc.register);
  1156. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1157. { this is the register order for GDB}
  1158. if regidx<>0 then
  1159. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",'+base_stabs_str(regvar_stab,'0','${line}','$2'),[st,tostr(regstabs_table[regidx])]);
  1160. end;
  1161. else
  1162. begin
  1163. if (vo_is_thread_var in sym.varoptions) then
  1164. threadvaroffset:='+'+tostr(sizeof(pint))
  1165. else
  1166. threadvaroffset:='';
  1167. if (vo_is_typed_const in sym.varoptions) then
  1168. if vo_is_public in sym.varoptions then
  1169. nsym:=globalvarsym_inited_stab
  1170. else
  1171. nsym:=staticvarsym_inited_stab
  1172. else if vo_is_public in sym.varoptions then
  1173. nsym:=globalvarsym_uninited_stab
  1174. else
  1175. nsym:=staticvarsym_uninited_stab;
  1176. { Here we used S instead of
  1177. because with G GDB doesn't look at the address field
  1178. but searches the same name or with a leading underscore
  1179. but these names don't exist in pascal !}
  1180. st:='S'+st;
  1181. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(nsym,'0','${line}','$2$3'),[st,staticvarsym_mangled_name(sym),threadvaroffset]);
  1182. end;
  1183. end;
  1184. write_sym_stabstr(list,sym,ss);
  1185. end;
  1186. procedure TDebugInfoStabs.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  1187. var
  1188. ss : ansistring;
  1189. st : string;
  1190. regidx : Tregisterindex;
  1191. begin
  1192. { There is no space allocated for not referenced locals }
  1193. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1194. exit;
  1195. ss:='';
  1196. st:=def_stab_number(sym.vardef);
  1197. case sym.localloc.loc of
  1198. LOC_REGISTER,
  1199. LOC_CREGISTER,
  1200. LOC_MMREGISTER,
  1201. LOC_CMMREGISTER,
  1202. LOC_FPUREGISTER,
  1203. LOC_CFPUREGISTER :
  1204. begin
  1205. regidx:=findreg_by_number(sym.localloc.register);
  1206. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1207. { this is the register order for GDB}
  1208. if regidx<>0 then
  1209. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",'+base_stabs_str(regvar_stab,'0','${line}','$2'),[st,tostr(regstabs_table[regidx])]);
  1210. end;
  1211. LOC_REFERENCE :
  1212. { offset to ebp => will not work if the framepointer is esp
  1213. so some optimizing will make things harder to debug }
  1214. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(localvarsymref_stab,'0','${line}','$2'),[st,getoffsetstr(sym.localloc.reference)])
  1215. else
  1216. internalerror(2003091814);
  1217. end;
  1218. write_sym_stabstr(list,sym,ss);
  1219. end;
  1220. function TDebugInfoStabs.get_appendsym_paravar_reg(sym:tparavarsym;const typ,stabstr:string;reg: tregister): ansistring;
  1221. var
  1222. ltyp: string[1];
  1223. regidx : Tregisterindex;
  1224. begin
  1225. result:='';
  1226. if typ='p' then
  1227. ltyp:='R'
  1228. else
  1229. ltyp:='a';
  1230. regidx:=findreg_by_number(reg);
  1231. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1232. { this is the register order for GDB}
  1233. if regidx<>0 then
  1234. result:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(regvar_stab,'0','${line}','$2'),[ltyp+stabstr,tostr(longint(regstabs_table[regidx]))]);
  1235. end;
  1236. function TDebugInfoStabs.base_stabs_str(typ: longint; const other, desc, value: ansistring): ansistring;
  1237. begin
  1238. result:=base_stabs_str(tostr(typ),other,desc,value);
  1239. end;
  1240. function TDebugInfoStabs.base_stabs_str(const typ, other, desc, value: ansistring): ansistring;
  1241. begin
  1242. result:=typ+','+other+','+desc+','+value
  1243. end;
  1244. function TDebugInfoStabs.gen_procdef_startsym_stabs(def: tprocdef): TAsmList;
  1245. var
  1246. RType : Char;
  1247. Obj,Info,
  1248. mangledname: ansistring;
  1249. begin
  1250. result:=TAsmList.create;
  1251. { "The stab representing a procedure is located immediately
  1252. following the code of the procedure. This stab is in turn
  1253. directly followed by a group of other stabs describing
  1254. elements of the procedure. These other stabs describe the
  1255. procedure's parameters, its block local variables, and its
  1256. block structure." (stab docs) }
  1257. { this is however incorrect in case "include source" statements }
  1258. { appear in the block, in that case the procedure stab must }
  1259. { appear before this include stabs (and we generate such an }
  1260. { stabs for all functions) (JM) }
  1261. obj := GetSymName(def.procsym);
  1262. info := '';
  1263. if (po_global in def.procoptions) then
  1264. RType := 'F'
  1265. else
  1266. RType := 'f';
  1267. if assigned(def.owner) then
  1268. begin
  1269. if (def.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1270. obj := GetSymTableName(def.owner)+'__'+GetSymName(def.procsym);
  1271. if not(cs_gdb_valgrind in current_settings.globalswitches) and
  1272. (def.owner.symtabletype=localsymtable) and
  1273. assigned(def.owner.defowner) and
  1274. assigned(tprocdef(def.owner.defowner).procsym) then
  1275. info := ','+GetSymName(def.procsym)+','+GetSymName(tprocdef(def.owner.defowner).procsym);
  1276. end;
  1277. mangledname:=ApplyAsmSymbolRestrictions(def.mangledname);
  1278. if target_info.system in systems_dotted_function_names then
  1279. mangledname:='.'+mangledname;
  1280. result.concat(Tai_stab.Create_ansistr(stabsdir,'"'+obj+':'+RType+def_stab_number(def.returndef)+info+'",'+
  1281. base_stabs_str(procdef_stab,'0',tostr(def.fileinfo.line),mangledname)));
  1282. end;
  1283. function TDebugInfoStabs.gen_procdef_endsym_stabs(def: tprocdef): TAsmList;
  1284. var
  1285. ss, mangledname: ansistring;
  1286. stabsendlabel: tasmlabel;
  1287. begin
  1288. result:=TAsmList.create;
  1289. { end of procedure }
  1290. current_asmdata.getlabel(stabsendlabel,alt_dbgtype);
  1291. if dbgtype<>dbg_stabx then
  1292. begin
  1293. mangledname:=def.mangledname;
  1294. if target_info.system in systems_dotted_function_names then
  1295. mangledname:='.'+mangledname;
  1296. // LBRAC
  1297. if af_stabs_use_function_absolute_addresses in target_asm.flags then
  1298. ss:=tostr(STABS_N_LBRAC)+',0,0,'+mangledname
  1299. else
  1300. ss:=tostr(STABS_N_LBRAC)+',0,0,0';
  1301. result.concat(Tai_stab.Create_ansistr(stab_stabn,ss));
  1302. // RBRAC
  1303. ss:=tostr(STABS_N_RBRAC)+',0,0,'+stabsendlabel.name;
  1304. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1305. ss:=ss+'-'+mangledname;
  1306. result.concat(Tai_stab.Create_ansistr(stab_stabn,ss));
  1307. { the stabsendlabel must come after all other stabs for this }
  1308. { function }
  1309. result.concat(tai_label.create(stabsendlabel));
  1310. { Add a "size" stab as described in the last paragraph of 2.5 at }
  1311. { http://sourceware.org/gdb/current/onlinedocs/stabs_2.html#SEC12 }
  1312. { This works at least on Darwin (and is needed on Darwin to get }
  1313. { correct smartlinking of stabs), but I don't know which binutils }
  1314. { version is required on other platforms }
  1315. { This stab must come after all other stabs for the procedure, }
  1316. { including the LBRAC/RBRAC ones }
  1317. if (target_info.system in systems_darwin) then
  1318. result.concat(Tai_stab.create(stabsdir,
  1319. strpnew('"",'+base_stabs_str(procdef_stab,'0','0',stabsendlabel.name+'-'+mangledname))));
  1320. end;
  1321. end;
  1322. procedure TDebugInfoStabs.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  1323. var
  1324. ss : ansistring;
  1325. c : string[1];
  1326. st : string;
  1327. regidx : Tregisterindex;
  1328. begin
  1329. ss:='';
  1330. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1331. { while stabs aren't adapted for regvars yet }
  1332. if (vo_is_self in sym.varoptions) then
  1333. begin
  1334. case sym.localloc.loc of
  1335. LOC_REGISTER,
  1336. LOC_CREGISTER:
  1337. regidx:=findreg_by_number(sym.localloc.register);
  1338. LOC_REFERENCE: ;
  1339. else
  1340. internalerror(2003091815);
  1341. end;
  1342. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1343. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1344. begin
  1345. if (sym.localloc.loc=LOC_REFERENCE) then
  1346. ss:=sym_stabstr_evaluate(sym,'"pvmt:p$1",'+base_stabs_str(localvarsymref_stab,'0','0','$2'),
  1347. [def_stab_number(pvmttype),getoffsetstr(sym.localloc.reference)])
  1348. else
  1349. begin
  1350. regidx:=findreg_by_number(sym.localloc.register);
  1351. ss:=sym_stabstr_evaluate(sym,'"pvmt:r$1",'+base_stabs_str(regvar_stab,'0','0','$2'),
  1352. [def_stab_number(pvmttype),tostr(regstabs_table[regidx])]);
  1353. end
  1354. end
  1355. else
  1356. begin
  1357. if not(is_class(tprocdef(sym.owner.defowner).struct)) then
  1358. c:='v'
  1359. else
  1360. c:='p';
  1361. if (sym.localloc.loc=LOC_REFERENCE) then
  1362. ss:=sym_stabstr_evaluate(sym,'"$$t:$1",'+base_stabs_str(localvarsymref_stab,'0','0','$2'),
  1363. [c+def_stab_number(tprocdef(sym.owner.defowner).struct),getoffsetstr(sym.localloc.reference)])
  1364. else
  1365. begin
  1366. if (c='p') then
  1367. c:='R'
  1368. else
  1369. c:='a';
  1370. regidx:=findreg_by_number(sym.localloc.register);
  1371. ss:=sym_stabstr_evaluate(sym,'"$$t:$1",'+base_stabs_str(regvar_stab,'0','0','$2'),
  1372. [c+def_stab_number(tprocdef(sym.owner.defowner).struct),tostr(regstabs_table[regidx])]);
  1373. end
  1374. end;
  1375. end
  1376. else
  1377. begin
  1378. st:=def_stab_number(sym.vardef);
  1379. if paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1380. not(vo_has_local_copy in sym.varoptions) and
  1381. not is_open_string(sym.vardef) then
  1382. c:='v' { should be 'i' but 'i' doesn't work }
  1383. else
  1384. c:='p';
  1385. case sym.localloc.loc of
  1386. LOC_REGISTER,
  1387. LOC_CREGISTER,
  1388. LOC_MMREGISTER,
  1389. LOC_CMMREGISTER,
  1390. LOC_FPUREGISTER,
  1391. LOC_CFPUREGISTER :
  1392. begin
  1393. ss:=get_appendsym_paravar_reg(sym,c,st,sym.localloc.register);
  1394. end;
  1395. LOC_REFERENCE :
  1396. begin
  1397. { When the *value* of a parameter (so not its address!) is
  1398. copied into a local variable, you have to generate two
  1399. stabs: one for the parmeter, and one for the local copy.
  1400. Not doing this breaks debugging under e.g. SPARC. Doc:
  1401. http://sourceware.org/gdb/current/onlinedocs/stabs_4.html#SEC26
  1402. }
  1403. if (target_dbg.id<>dbg_stabx) and
  1404. (c='p') and
  1405. not is_open_string(sym.vardef) and
  1406. ((sym.paraloc[calleeside].location^.loc<>sym.localloc.loc) or
  1407. ((sym.localloc.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  1408. ((sym.paraloc[calleeside].location^.reference.index<>sym.localloc.reference.base) or
  1409. (sym.paraloc[calleeside].location^.reference.offset<>sym.localloc.reference.offset))) or
  1410. ((sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  1411. (sym.localloc.register<>sym.paraloc[calleeside].location^.register))) then
  1412. begin
  1413. if not(sym.paraloc[calleeside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1414. ss:=get_appendsym_paravar_reg(sym,c,st,sym.paraloc[calleeside].location^.register)
  1415. else
  1416. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(localvarsymref_stab,'0','${line}','$2'),
  1417. [c+st,getparaoffsetstr(sym.paraloc[calleeside].location^.reference)]);
  1418. write_sym_stabstr(list,sym,ss);
  1419. { second stab has no parameter specifier }
  1420. c:='';
  1421. end;
  1422. { offset to ebp => will not work if the framepointer is esp
  1423. so some optimizing will make things harder to debug }
  1424. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",'+base_stabs_str(paravarsymref_stab,'0','${line}','$2'),[c+st,getoffsetstr(sym.localloc.reference)])
  1425. end;
  1426. else
  1427. internalerror(2003091805);
  1428. end;
  1429. end;
  1430. write_sym_stabstr(list,sym,ss);
  1431. end;
  1432. function stabx_quote_const(const s: string): string;
  1433. var
  1434. i:byte;
  1435. begin
  1436. stabx_quote_const:='';
  1437. for i:=1 to length(s) do
  1438. begin
  1439. case s[i] of
  1440. #10:
  1441. stabx_quote_const:=stabx_quote_const+'\n';
  1442. #13:
  1443. stabx_quote_const:=stabx_quote_const+'\r';
  1444. { stabx strings cannot deal with embedded quotes }
  1445. '"':
  1446. stabx_quote_const:=stabx_quote_const+' ';
  1447. else
  1448. stabx_quote_const:=stabx_quote_const+s[i];
  1449. end;
  1450. end;
  1451. end;
  1452. procedure TDebugInfoStabs.appendsym_const(list:TAsmList;sym:tconstsym);
  1453. var
  1454. st : string;
  1455. ss : ansistring;
  1456. i : longint;
  1457. begin
  1458. ss:='';
  1459. { Don't write info for default parameter values, the N_Func breaks
  1460. the N_Func for the function itself.
  1461. Valgrind does not support constants }
  1462. if (sym.owner.symtabletype=parasymtable) or
  1463. (cs_gdb_valgrind in current_settings.globalswitches) then
  1464. exit;
  1465. case sym.consttyp of
  1466. conststring:
  1467. begin
  1468. if sym.value.len<200 then
  1469. begin
  1470. setlength(ss,sym.value.len);
  1471. for i:=0 to sym.value.len-1 do
  1472. ss[i+1]:=pchar(sym.value.valueptr)[i];
  1473. if target_dbg.id=dbg_stabs then
  1474. st:='s'''+backspace_quote(octal_quote(ss,[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+''''
  1475. else
  1476. st:='s'''+stabx_quote_const(octal_quote(ss,[#0..#9,#11,#12,#14..#31,'''']))+'''';
  1477. end
  1478. else
  1479. st:='<constant string too long>';
  1480. end;
  1481. constord:
  1482. st:='i'+tostr(sym.value.valueord);
  1483. constpointer:
  1484. st:='i'+tostr(sym.value.valueordptr);
  1485. constreal:
  1486. begin
  1487. system.str(pbestreal(sym.value.valueptr)^,st);
  1488. st := 'r'+st;
  1489. end;
  1490. else
  1491. begin
  1492. { if we don't know just put zero !! }
  1493. st:='i0';
  1494. end;
  1495. end;
  1496. ss:=sym_stabstr_evaluate(sym,'"${name}:c=$1;",'+base_stabs_str(constsym_stab,'0','${line}','0'),[st]);
  1497. write_sym_stabstr(list,sym,ss);
  1498. end;
  1499. procedure TDebugInfoStabs.appendsym_type(list:TAsmList;sym:ttypesym);
  1500. var
  1501. ss : ansistring;
  1502. stabchar : string[2];
  1503. begin
  1504. ss:='';
  1505. if not assigned(sym.typedef) then
  1506. internalerror(200509262);
  1507. if use_tag_prefix(sym.typedef) then
  1508. stabchar:=tagtypeprefix
  1509. else
  1510. stabchar:='t';
  1511. ss:=sym_stabstr_evaluate(sym,'"${name}:$1$2",'+base_stabs_str(typesym_stab,'0','${line}','0'),[stabchar,def_stab_number(sym.typedef)]);
  1512. write_sym_stabstr(list,sym,ss);
  1513. end;
  1514. procedure TDebugInfoStabs.appendsym_label(list:TAsmList;sym:tlabelsym);
  1515. var
  1516. ss : ansistring;
  1517. begin
  1518. ss:=sym_stabstr_evaluate(sym,'"${name}",'+base_stabs_str(localvarsymref_stab,'0','${line}','0'),[]);
  1519. write_sym_stabstr(list,sym,ss);
  1520. end;
  1521. {****************************************************************************
  1522. Proc/Module support
  1523. ****************************************************************************}
  1524. procedure TDebugInfoStabs.inserttypeinfo;
  1525. var
  1526. stabsvarlist,
  1527. stabstypelist : TAsmList;
  1528. storefilepos : tfileposinfo;
  1529. i : longint;
  1530. vardatatype : ttypesym;
  1531. begin
  1532. storefilepos:=current_filepos;
  1533. current_filepos:=current_module.mainfilepos;
  1534. global_stab_number:=0;
  1535. defnumberlist:=TFPObjectlist.create(false);
  1536. deftowritelist:=TFPObjectlist.create(false);
  1537. stabsvarlist:=TAsmList.create;
  1538. stabstypelist:=TAsmList.create;
  1539. vardatatype:=try_search_system_type('TVARDATA');
  1540. if assigned(vardatatype) then
  1541. vardatadef:=trecorddef(vardatatype.typedef);
  1542. { include symbol that will be referenced from the main to be sure to
  1543. include this debuginfo .o file }
  1544. include(current_module.moduleflags,mf_has_stabs_debuginfo);
  1545. if not(target_info.system in systems_darwin) then
  1546. begin
  1547. new_section(current_asmdata.asmlists[al_stabs],sec_data,GetSymTableName(current_module.localsymtable),sizeof(pint));
  1548. current_asmdata.asmlists[al_stabs].concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_METADATA,0,voidpointertype));
  1549. end
  1550. else
  1551. new_section(current_asmdata.asmlists[al_stabs],sec_code,GetSymTableName(current_module.localsymtable),sizeof(pint));
  1552. { write all global/local variables. This will flag all required tdefs }
  1553. if assigned(current_module.globalsymtable) then
  1554. write_symtable_syms(stabsvarlist,current_module.globalsymtable);
  1555. if assigned(current_module.localsymtable) then
  1556. write_symtable_syms(stabsvarlist,current_module.localsymtable);
  1557. { write all procedures and methods. This will flag all required tdefs }
  1558. if assigned(current_module.globalsymtable) then
  1559. write_symtable_procdefs(stabsvarlist,current_module.globalsymtable);
  1560. if assigned(current_module.localsymtable) then
  1561. write_symtable_procdefs(stabsvarlist,current_module.localsymtable);
  1562. { reset unit type info flag }
  1563. reset_unit_type_info;
  1564. { write used types from the used units }
  1565. write_used_unit_type_info(stabstypelist,current_module);
  1566. { last write the types from this unit }
  1567. if assigned(current_module.globalsymtable) then
  1568. write_symtable_defs(stabstypelist,current_module.globalsymtable);
  1569. if assigned(current_module.localsymtable) then
  1570. write_symtable_defs(stabstypelist,current_module.localsymtable);
  1571. write_remaining_defs_to_write(stabstypelist);
  1572. current_asmdata.asmlists[al_stabs].concatlist(stabstypelist);
  1573. current_asmdata.asmlists[al_stabs].concatlist(stabsvarlist);
  1574. { reset stab numbers }
  1575. for i:=0 to defnumberlist.count-1 do
  1576. begin
  1577. if assigned(defnumberlist[i]) then
  1578. begin
  1579. tdef(defnumberlist[i]).stab_number:=0;
  1580. tdef(defnumberlist[i]).dbg_state:=dbg_state_unused;
  1581. end;
  1582. end;
  1583. defnumberlist.free;
  1584. defnumberlist:=nil;
  1585. deftowritelist.free;
  1586. deftowritelist:=nil;
  1587. stabsvarlist.free;
  1588. stabstypelist.free;
  1589. current_filepos:=storefilepos;
  1590. end;
  1591. procedure TDebugInfoStabs.insertlineinfo(list: TAsmList);
  1592. var
  1593. currfileinfo,
  1594. lastfileinfo : tfileposinfo;
  1595. currfuncname : pshortstring;
  1596. currsectype : TAsmSectiontype;
  1597. hlabel : tasmlabel;
  1598. hp : tai;
  1599. infile : tinputfile;
  1600. begin
  1601. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1602. currfuncname:=nil;
  1603. currsectype:=sec_code;
  1604. hp:=Tai(list.first);
  1605. while assigned(hp) do
  1606. begin
  1607. case hp.typ of
  1608. ait_section :
  1609. currsectype:=tai_section(hp).sectype;
  1610. ait_function_name :
  1611. currfuncname:=tai_function_name(hp).funcname;
  1612. ait_force_line :
  1613. lastfileinfo.line:=-1;
  1614. else
  1615. ;
  1616. end;
  1617. if (currsectype=sec_code) and
  1618. (hp.typ=ait_instruction) then
  1619. begin
  1620. currfileinfo:=tailineinfo(hp).fileinfo;
  1621. { file changed ? (must be before line info) }
  1622. if (currfileinfo.fileindex<>0) and
  1623. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  1624. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  1625. begin
  1626. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  1627. if assigned(infile) then
  1628. begin
  1629. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1630. { emit stabs }
  1631. if not(ds_stabs_abs_include_files in current_settings.debugswitches) or
  1632. path_absolute(infile.path) then
  1633. list.insertbefore(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(infile.path,false))+FixFileName(infile.name)+'",'+tostr(stabs_n_includefile)+
  1634. ',0,0,'+hlabel.name),hp)
  1635. else
  1636. list.insertbefore(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(getcurrentdir,false)+FixPath(infile.path,false))+FixFileName(infile.name)+'",'+tostr(stabs_n_includefile)+
  1637. ',0,0,'+hlabel.name),hp);
  1638. list.insertbefore(tai_label.create(hlabel),hp);
  1639. { force new line info }
  1640. lastfileinfo.line:=-1;
  1641. end;
  1642. end;
  1643. { line changed ? }
  1644. if (currfileinfo.line>lastfileinfo.line) and (currfileinfo.line<>0) then
  1645. begin
  1646. if assigned(currfuncname) and
  1647. not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1648. begin
  1649. current_asmdata.getlabel(hlabel,alt_dbgline);
  1650. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(stabs_n_textline)+',0,'+tostr(currfileinfo.line)+','+
  1651. hlabel.name+' - '+{$IFDEF POWERPC64}'.'+{$ENDIF POWERPC64}currfuncname^),hp);
  1652. list.insertbefore(tai_label.create(hlabel),hp);
  1653. end
  1654. else
  1655. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(stabs_n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  1656. end;
  1657. lastfileinfo:=currfileinfo;
  1658. end;
  1659. hp:=tai(hp.next);
  1660. end;
  1661. end;
  1662. procedure TDebugInfoStabs.insertmoduleinfo;
  1663. var
  1664. hlabel : tasmlabel;
  1665. infile : tinputfile;
  1666. begin
  1667. { emit main source n_sourcefile for start of module }
  1668. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1669. infile:=current_module.sourcefiles.get_file(1);
  1670. new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),sizeof(pint),secorder_begin);
  1671. if not(target_info.system in systems_darwin) then
  1672. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(make_mangledname('DEBUGSTART',current_module.localsymtable,''),AT_METADATA,0,voidpointertype));
  1673. {$ifdef MIPS}
  1674. { at least mipsel needs an explicit '.set nomips16' before any reference to
  1675. procedure/function, see bug report 32138 }
  1676. current_asmdata.asmlists[al_start].concat(Taicpu.op_none(A_P_SET_NOMIPS16));
  1677. {$endif MIPS}
  1678. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(getcurrentdir,false))+'",'+
  1679. base_stabs_str(stabs_n_sourcefile,'0','0',hlabel.name)));
  1680. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stabsdir,'"'+BsToSlash(FixPath(infile.path,false))+FixFileName(infile.name)+'",'+
  1681. base_stabs_str(stabs_n_sourcefile,'0','0',hlabel.name)));
  1682. current_asmdata.asmlists[al_start].concat(tai_label.create(hlabel));
  1683. { for darwin, you need a "module marker" too to work around }
  1684. { either some assembler or gdb bug (radar 4386531 according to a }
  1685. { comment in dbxout.c of Apple's gcc) }
  1686. if (target_info.system in systems_darwin) then
  1687. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stabsdir,'"",'+base_stabs_str(STABS_N_OSO,'0','0','0')));
  1688. { emit empty n_sourcefile for end of module }
  1689. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1690. new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),sizeof(pint),secorder_end);
  1691. if not(target_info.system in systems_darwin) then
  1692. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(make_mangledname('DEBUGEND',current_module.localsymtable,''),AT_METADATA,0,voidpointertype));
  1693. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stabsdir,'"",'+base_stabs_str(stabs_n_sourcefile,'0','0',hlabel.name)));
  1694. current_asmdata.asmlists[al_end].concat(tai_label.create(hlabel));
  1695. end;
  1696. procedure TDebugInfoStabs.referencesections(list: TAsmList);
  1697. var
  1698. hp : tmodule;
  1699. dbgtable : tai_symbol;
  1700. begin
  1701. { Reference all DEBUGINFO sections from the main .fpc section }
  1702. if (target_info.system in ([system_powerpc_macosclassic]+systems_darwin)) then
  1703. exit;
  1704. new_section(list,sec_fpc,'links',0);
  1705. { make sure the debuginfo doesn't get stripped out }
  1706. if (target_info.system in systems_darwin) then
  1707. begin
  1708. dbgtable:=tai_symbol.createname('DEBUGINFOTABLE',AT_METADATA,0,voidpointertype);
  1709. list.concat(tai_directive.create(asd_no_dead_strip,dbgtable.sym.name));
  1710. list.concat(dbgtable);
  1711. end;
  1712. { include reference to all debuginfo sections of used units }
  1713. hp:=tmodule(loaded_units.first);
  1714. while assigned(hp) do
  1715. begin
  1716. If (mf_has_stabs_debuginfo in hp.moduleflags) and not assigned(hp.package) then
  1717. begin
  1718. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.localsymtable,''),0));
  1719. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  1720. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  1721. end;
  1722. hp:=tmodule(hp.next);
  1723. end;
  1724. end;
  1725. constructor TDebugInfoStabs.Create;
  1726. begin
  1727. inherited Create;
  1728. dbgtype:=dbg_stabs;
  1729. stabsdir:=stab_stabs;
  1730. def_stab:=STABS_N_LSYM;
  1731. regvar_stab:=STABS_N_RSYM;
  1732. procdef_stab:=STABS_N_Function;
  1733. constsym_stab:=STABS_N_Function;
  1734. typesym_stab:=STABS_N_LSYM;
  1735. globalvarsym_uninited_stab:=STABS_N_STSYM;
  1736. globalvarsym_inited_stab:=STABS_N_LCSYM;
  1737. staticvarsym_uninited_stab:=STABS_N_STSYM;
  1738. staticvarsym_inited_stab:=STABS_N_LCSYM;
  1739. localvarsymref_stab:=STABS_N_TSYM;
  1740. paravarsymref_stab:=STABS_N_TSYM;
  1741. tagtypeprefix:='Tt';
  1742. vardatadef:=nil;
  1743. end;
  1744. const
  1745. dbg_stabs_info : tdbginfo =
  1746. (
  1747. id : dbg_stabs;
  1748. idtxt : 'STABS';
  1749. );
  1750. initialization
  1751. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  1752. end.