PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/dbgstabs.pas

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