dbgstabs.pas 74 KB

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