dbgstabs.pas 61 KB

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