dbgstabs.pas 73 KB

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