dbgstabs.pas 74 KB

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