dbgstabs.pas 71 KB

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