dbgstabs.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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,
  732. ss : ansistring;
  733. begin
  734. if not is_packed_array(def) then
  735. ss:=def_stabstr_evaluate(def,'ar$1;$2;$3;$4',[def_stab_number(tarraydef(def).rangedef),
  736. tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange),def_stab_number(tarraydef(def).elementdef)])
  737. else
  738. begin
  739. // the @P seems to be ignored by gdb
  740. tempstr:=def_stabstr_evaluate(tarraydef(def).rangedef,'r${numberstring};$1;$2;',
  741. [tostr(tarraydef(def).lowrange),tostr(tarraydef(def).highrange)]);
  742. // will only show highrange-lowrange+1 bits in gdb
  743. ss:=def_stabstr_evaluate(def,'@s$1;@S;S$2',
  744. [tostr(TConstExprInt(tarraydef(def).elepackedbitsize) * tarraydef(def).elecount),tempstr]);
  745. end;
  746. write_def_stabstr(list,def,ss);
  747. end;
  748. procedure TDebugInfoStabs.appenddef_procvar(list:TAsmList;def:tprocvardef);
  749. var
  750. ss : ansistring;
  751. begin
  752. ss:='*f'+def_stab_number(tprocvardef(def).returndef);
  753. write_def_stabstr(list,def,ss);
  754. end;
  755. procedure TDebugInfoStabs.appenddef_undefined(list:TAsmList;def:tundefineddef);
  756. var
  757. ss : ansistring;
  758. begin
  759. ss:=def_stabstr_evaluate(def,'${numberstring};',[]);
  760. write_def_stabstr(list,def,ss);
  761. end;
  762. procedure TDebugInfoStabs.beforeappenddef(list:TAsmList;def:tdef);
  763. var
  764. anc : tobjectdef;
  765. i : longint;
  766. begin
  767. { write dependencies first }
  768. case def.typ of
  769. stringdef :
  770. begin
  771. if tstringdef(def).stringtype in [st_widestring,st_unicodestring] then
  772. appenddef(list,cwidechartype)
  773. else
  774. begin
  775. appenddef(list,cchartype);
  776. appenddef(list,u8inttype);
  777. end;
  778. end;
  779. floatdef :
  780. appenddef(list,s32inttype);
  781. filedef :
  782. begin
  783. appenddef(list,s32inttype);
  784. {$ifdef cpu64bitaddr}
  785. appenddef(list,s64inttype);
  786. {$endif cpu64bitaddr}
  787. appenddef(list,u8inttype);
  788. appenddef(list,cchartype);
  789. end;
  790. classrefdef :
  791. appenddef(list,pvmttype);
  792. pointerdef :
  793. appenddef(list,tpointerdef(def).pointeddef);
  794. setdef :
  795. appenddef(list,tsetdef(def).elementdef);
  796. procvardef :
  797. begin
  798. appenddef(list,tprocvardef(def).returndef);
  799. if assigned(tprocvardef(def).parast) then
  800. write_symtable_defs(list,tprocvardef(def).parast);
  801. end;
  802. procdef :
  803. begin
  804. appenddef(list,tprocdef(def).returndef);
  805. if assigned(tprocdef(def).parast) then
  806. write_symtable_defs(list,tprocdef(def).parast);
  807. if assigned(tprocdef(def).localst) and
  808. (tprocdef(def).localst.symtabletype=localsymtable) then
  809. write_symtable_defs(list,tprocdef(def).localst);
  810. end;
  811. arraydef :
  812. begin
  813. appenddef(list,tarraydef(def).rangedef);
  814. appenddef(list,tarraydef(def).elementdef);
  815. end;
  816. recorddef :
  817. trecorddef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  818. enumdef :
  819. if assigned(tenumdef(def).basedef) then
  820. appenddef(list,tenumdef(def).basedef);
  821. objectdef :
  822. begin
  823. { make sure we don't write child classdefs before their parent }
  824. { classdefs, because this crashes gdb }
  825. anc:=tobjectdef(def);
  826. while assigned(anc.childof) do
  827. begin
  828. anc:=anc.childof;
  829. if (anc.dbg_state=dbg_state_writing) then
  830. { happens in case a field of a parent is of the (forward }
  831. { defined) child type }
  832. begin
  833. { We don't explicitly requeue it, but the fact that }
  834. { a child type was used in a parent before the child }
  835. { type was fully defined means that it was forward }
  836. { declared, and will still be encountered later (it }
  837. { cannot have been declared in another unit, because }
  838. { then this and that other unit would depend on }
  839. { eachother's interface) }
  840. { Setting the state to queued however allows us to }
  841. { get the def number already without an IE }
  842. def.dbg_state:=dbg_state_queued;
  843. exit;
  844. end;
  845. end;
  846. appenddef(list,vmtarraytype);
  847. if assigned(tobjectdef(def).ImplementedInterfaces) then
  848. for i:=0 to tobjectdef(def).ImplementedInterfaces.Count-1 do
  849. appenddef(list,TImplementedInterface(tobjectdef(def).ImplementedInterfaces[i]).IntfDef);
  850. { first the parents }
  851. anc:=tobjectdef(def);
  852. while assigned(anc.childof) do
  853. begin
  854. anc:=anc.childof;
  855. appenddef(list,anc);
  856. if assigned(anc.ImplementedInterfaces) then
  857. for i:=0 to anc.ImplementedInterfaces.Count-1 do
  858. appenddef(list,TImplementedInterface(anc.ImplementedInterfaces[i]).IntfDef);
  859. end;
  860. tobjectdef(def).symtable.symList.ForEachCall(@field_write_defs,list);
  861. end;
  862. end;
  863. end;
  864. procedure TDebugInfoStabs.appendprocdef(list:TAsmList;def:tprocdef);
  865. var
  866. templist : TAsmList;
  867. stabsendlabel : tasmlabel;
  868. p : pchar;
  869. RType : Char;
  870. Obj,Info : String;
  871. hs : string;
  872. ss : ansistring;
  873. begin
  874. if not assigned(def.procstarttai) then
  875. exit;
  876. { mark as used so the local type defs also be written }
  877. def.dbg_state:=dbg_state_used;
  878. templist:=TAsmList.create;
  879. { end of procedure }
  880. current_asmdata.getlabel(stabsendlabel,alt_dbgtype);
  881. if assigned(def.funcretsym) and
  882. (tabstractnormalvarsym(def.funcretsym).refs>0) then
  883. begin
  884. if tabstractnormalvarsym(def.funcretsym).localloc.loc=LOC_REFERENCE then
  885. begin
  886. { TODO: Need to add gdb support for ret in param register calling}
  887. if paramanager.ret_in_param(def.returndef,def.proccalloption) then
  888. hs:='X*'
  889. else
  890. hs:='X';
  891. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  892. '"'+GetSymName(def.procsym)+':'+hs+def_stab_number(def.returndef)+'",'+
  893. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(def.funcretsym).localloc.reference.offset))));
  894. if (m_result in current_settings.modeswitches) then
  895. templist.concat(Tai_stab.create(stab_stabs,strpnew(
  896. '"RESULT:'+hs+def_stab_number(def.returndef)+'",'+
  897. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(def.funcretsym).localloc.reference.offset))));
  898. end;
  899. end;
  900. // LBRAC
  901. ss:=tostr(N_LBRAC)+',0,0,';
  902. if target_info.cpu=cpu_powerpc64 then
  903. ss:=ss+'.';
  904. ss:=ss+def.mangledname;
  905. if (tf_use_function_relative_addresses in target_info.flags) then
  906. begin
  907. ss:=ss+'-';
  908. if target_info.cpu=cpu_powerpc64 then
  909. ss:=ss+'.';
  910. ss:=ss+def.mangledname;
  911. end;
  912. getmem(p,length(ss)+1);
  913. move(pchar(ss)^,p^,length(ss)+1);
  914. templist.concat(Tai_stab.Create(stab_stabn,p));
  915. // RBRAC
  916. ss:=tostr(N_RBRAC)+',0,0,'+stabsendlabel.name;
  917. if (tf_use_function_relative_addresses in target_info.flags) then
  918. begin
  919. ss:=ss+'-';
  920. if target_info.cpu=cpu_powerpc64 then
  921. ss:=ss+'.';
  922. ss:=ss+def.mangledname;
  923. end;
  924. getmem(p,length(ss)+1);
  925. move(pchar(ss)^,p^,length(ss)+1);
  926. templist.concat(Tai_stab.Create(stab_stabn,p));
  927. { the stabsendlabel must come after all other stabs for this }
  928. { function }
  929. templist.concat(tai_label.create(stabsendlabel));
  930. { Add a "size" stab as described in the last paragraph of 2.5 at }
  931. { http://sourceware.org/gdb/current/onlinedocs/stabs_2.html#SEC12 }
  932. { This works at least on Darwin (and is needed on Darwin to get }
  933. { correct smartlinking of stabs), but I don't know which binutils }
  934. { version is required on other platforms }
  935. { This stab must come after all other stabs for the procedure, }
  936. { including the LBRAC/RBRAC ones }
  937. if (target_info.system in systems_darwin) then
  938. templist.concat(Tai_stab.create(stab_stabs,
  939. strpnew('"",'+tostr(N_FUNCTION)+',0,0,'+stabsendlabel.name+'-'+def.mangledname)));
  940. current_asmdata.asmlists[al_procedures].insertlistafter(def.procendtai,templist);
  941. { "The stab representing a procedure is located immediately
  942. following the code of the procedure. This stab is in turn
  943. directly followed by a group of other stabs describing
  944. elements of the procedure. These other stabs describe the
  945. procedure's parameters, its block local variables, and its
  946. block structure." (stab docs) }
  947. { this is however incorrect in case "include source" statements }
  948. { appear in the block, in that case the procedure stab must }
  949. { appear before this include stabs (and we generate such an }
  950. { stabs for all functions) (JM) }
  951. { FUNC stabs }
  952. obj := GetSymName(def.procsym);
  953. info := '';
  954. if (po_global in def.procoptions) then
  955. RType := 'F'
  956. else
  957. RType := 'f';
  958. if assigned(def.owner) then
  959. begin
  960. if (def.owner.symtabletype = objecTSymtable) then
  961. obj := GetSymTableName(def.owner)+'__'+GetSymName(def.procsym);
  962. if not(cs_gdb_valgrind in current_settings.globalswitches) and
  963. (def.owner.symtabletype=localsymtable) and
  964. assigned(def.owner.defowner) and
  965. assigned(tprocdef(def.owner.defowner).procsym) then
  966. info := ','+GetSymName(def.procsym)+','+GetSymName(tprocdef(def.owner.defowner).procsym);
  967. end;
  968. ss:='"'+ansistring(obj)+':'+RType+def_stab_number(def.returndef)+info+'",'+tostr(n_function)+',0,'+tostr(def.fileinfo.line)+','+ansistring(def.mangledname);
  969. getmem(p,length(ss)+1);
  970. move(pchar(ss)^,p^,length(ss)+1);
  971. templist.concat(Tai_stab.Create(stab_stabs,p));
  972. current_asmdata.asmlists[al_procedures].insertlistbefore(def.procstarttai,templist);
  973. { para types }
  974. if assigned(def.parast) then
  975. write_symtable_syms(templist,def.parast);
  976. { local type defs and vars should not be written
  977. inside the main proc stab }
  978. if assigned(def.localst) and
  979. (def.localst.symtabletype=localsymtable) then
  980. write_symtable_syms(templist,def.localst);
  981. current_asmdata.asmlists[al_procedures].insertlistbefore(def.procstarttai,templist);
  982. templist.free;
  983. end;
  984. {****************************************************************************
  985. TSym support
  986. ****************************************************************************}
  987. function TDebugInfoStabs.sym_var_value(const s:string;arg:pointer):string;
  988. var
  989. sym : tsym absolute arg;
  990. begin
  991. result:='';
  992. if s='name' then
  993. result:=GetSymName(sym)
  994. else if s='mangledname' then
  995. result:=sym.mangledname
  996. else if s='ownername' then
  997. result:=GetSymTableName(sym.owner)
  998. else if s='line' then
  999. result:=tostr(sym.fileinfo.line)
  1000. else if s='N_LSYM' then
  1001. result:=tostr(N_LSYM)
  1002. else if s='N_LCSYM' then
  1003. result:=tostr(N_LCSYM)
  1004. else if s='N_RSYM' then
  1005. result:=tostr(N_RSYM)
  1006. else if s='N_TSYM' then
  1007. result:=tostr(N_TSYM)
  1008. else if s='N_STSYM' then
  1009. result:=tostr(N_STSYM)
  1010. else if s='N_FUNCTION' then
  1011. result:=tostr(N_FUNCTION)
  1012. else
  1013. internalerror(200401152);
  1014. end;
  1015. function TDebugInfoStabs.sym_stabstr_evaluate(sym:tsym;const s:string;const vars:array of string):ansistring;
  1016. begin
  1017. result:=string_evaluate(s,@sym_var_value,sym,vars);
  1018. end;
  1019. procedure TDebugInfoStabs.write_sym_stabstr(list:TAsmList;sym:tsym;const ss:ansistring);
  1020. var
  1021. p : pchar;
  1022. begin
  1023. if ss='' then
  1024. exit;
  1025. { add to list }
  1026. getmem(p,length(ss)+1);
  1027. move(pchar(ss)^,p^,length(ss)+1);
  1028. list.concat(Tai_stab.create(stab_stabs,p));
  1029. end;
  1030. procedure TDebugInfoStabs.appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);
  1031. var
  1032. ss : ansistring;
  1033. begin
  1034. ss:='';
  1035. if (sym.owner.symtabletype=objecTSymtable) and
  1036. (sp_static in sym.symoptions) then
  1037. ss:=sym_stabstr_evaluate(sym,'"${ownername}__${name}:S$1",${N_LCSYM},0,${line},${mangledname}',
  1038. [def_stab_number(sym.vardef)]);
  1039. write_sym_stabstr(list,sym,ss);
  1040. end;
  1041. procedure TDebugInfoStabs.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1042. var
  1043. ss : ansistring;
  1044. st : string;
  1045. threadvaroffset : string;
  1046. regidx : Tregisterindex;
  1047. nsym : string[7];
  1048. begin
  1049. { external symbols can't be resolved at link time, so we
  1050. can't generate stabs for them }
  1051. if vo_is_external in sym.varoptions then
  1052. exit;
  1053. ss:='';
  1054. st:=def_stab_number(sym.vardef);
  1055. case sym.localloc.loc of
  1056. LOC_REGISTER,
  1057. LOC_CREGISTER,
  1058. LOC_MMREGISTER,
  1059. LOC_CMMREGISTER,
  1060. LOC_FPUREGISTER,
  1061. LOC_CFPUREGISTER :
  1062. begin
  1063. regidx:=findreg_by_number(sym.localloc.register);
  1064. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1065. { this is the register order for GDB}
  1066. if regidx<>0 then
  1067. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1068. end;
  1069. else
  1070. begin
  1071. if (vo_is_thread_var in sym.varoptions) then
  1072. threadvaroffset:='+'+tostr(sizeof(pint))
  1073. else
  1074. threadvaroffset:='';
  1075. if (vo_is_typed_const in sym.varoptions) then
  1076. nsym:='N_STSYM'
  1077. else
  1078. nsym:='N_LCSYM';
  1079. { Here we used S instead of
  1080. because with G GDB doesn't look at the address field
  1081. but searches the same name or with a leading underscore
  1082. but these names don't exist in pascal !}
  1083. st:='S'+st;
  1084. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${'+nsym+'},0,${line},${mangledname}$2',[st,threadvaroffset]);
  1085. end;
  1086. end;
  1087. write_sym_stabstr(list,sym,ss);
  1088. end;
  1089. procedure TDebugInfoStabs.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  1090. var
  1091. ss : ansistring;
  1092. st : string;
  1093. regidx : Tregisterindex;
  1094. begin
  1095. { There is no space allocated for not referenced locals }
  1096. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1097. exit;
  1098. ss:='';
  1099. st:=def_stab_number(sym.vardef);
  1100. case sym.localloc.loc of
  1101. LOC_REGISTER,
  1102. LOC_CREGISTER,
  1103. LOC_MMREGISTER,
  1104. LOC_CMMREGISTER,
  1105. LOC_FPUREGISTER,
  1106. LOC_CFPUREGISTER :
  1107. begin
  1108. regidx:=findreg_by_number(sym.localloc.register);
  1109. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1110. { this is the register order for GDB}
  1111. if regidx<>0 then
  1112. ss:=sym_stabstr_evaluate(sym,'"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1113. end;
  1114. LOC_REFERENCE :
  1115. { offset to ebp => will not work if the framepointer is esp
  1116. so some optimizing will make things harder to debug }
  1117. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1118. else
  1119. internalerror(2003091814);
  1120. end;
  1121. write_sym_stabstr(list,sym,ss);
  1122. end;
  1123. procedure TDebugInfoStabs.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  1124. var
  1125. ss : ansistring;
  1126. st : string;
  1127. regidx : Tregisterindex;
  1128. c : char;
  1129. begin
  1130. ss:='';
  1131. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1132. { while stabs aren't adapted for regvars yet }
  1133. if (vo_is_self in sym.varoptions) then
  1134. begin
  1135. case sym.localloc.loc of
  1136. LOC_REGISTER,
  1137. LOC_CREGISTER:
  1138. regidx:=findreg_by_number(sym.localloc.register);
  1139. LOC_REFERENCE: ;
  1140. else
  1141. internalerror(2003091815);
  1142. end;
  1143. if (po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) or
  1144. (po_staticmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  1145. begin
  1146. if (sym.localloc.loc=LOC_REFERENCE) then
  1147. ss:=sym_stabstr_evaluate(sym,'"pvmt:p$1",${N_TSYM},0,0,$2',
  1148. [def_stab_number(pvmttype),tostr(sym.localloc.reference.offset)])
  1149. else
  1150. begin
  1151. regidx:=findreg_by_number(sym.localloc.register);
  1152. ss:=sym_stabstr_evaluate(sym,'"pvmt:r$1",${N_RSYM},0,0,$2',
  1153. [def_stab_number(pvmttype),tostr(regstabs_table[regidx])]);
  1154. end
  1155. end
  1156. else
  1157. begin
  1158. if not(is_class(tprocdef(sym.owner.defowner)._class)) then
  1159. c:='v'
  1160. else
  1161. c:='p';
  1162. if (sym.localloc.loc=LOC_REFERENCE) then
  1163. ss:=sym_stabstr_evaluate(sym,'"$$t:$1",${N_TSYM},0,0,$2',
  1164. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(sym.localloc.reference.offset)])
  1165. else
  1166. begin
  1167. regidx:=findreg_by_number(sym.localloc.register);
  1168. ss:=sym_stabstr_evaluate(sym,'"$$t:r$1",${N_RSYM},0,0,$2',
  1169. [c+def_stab_number(tprocdef(sym.owner.defowner)._class),tostr(regstabs_table[regidx])]);
  1170. end
  1171. end;
  1172. end
  1173. else
  1174. begin
  1175. st:=def_stab_number(sym.vardef);
  1176. if paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1177. not(vo_has_local_copy in sym.varoptions) and
  1178. not is_open_string(sym.vardef) then
  1179. c:='v' { should be 'i' but 'i' doesn't work }
  1180. else
  1181. c:='p';
  1182. case sym.localloc.loc of
  1183. LOC_REGISTER,
  1184. LOC_CREGISTER,
  1185. LOC_MMREGISTER,
  1186. LOC_CMMREGISTER,
  1187. LOC_FPUREGISTER,
  1188. LOC_CFPUREGISTER :
  1189. begin
  1190. if c='p' then
  1191. c:='R'
  1192. else
  1193. c:='a';
  1194. st:=c+st;
  1195. regidx:=findreg_by_number(sym.localloc.register);
  1196. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1197. { this is the register order for GDB}
  1198. if regidx<>0 then
  1199. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_RSYM},0,${line},$2',[st,tostr(longint(regstabs_table[regidx]))]);
  1200. end;
  1201. LOC_REFERENCE :
  1202. begin
  1203. st:=c+st;
  1204. { offset to ebp => will not work if the framepointer is esp
  1205. so some optimizing will make things harder to debug }
  1206. ss:=sym_stabstr_evaluate(sym,'"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(sym.localloc.reference.offset)])
  1207. end;
  1208. else
  1209. internalerror(2003091814);
  1210. end;
  1211. end;
  1212. write_sym_stabstr(list,sym,ss);
  1213. end;
  1214. procedure TDebugInfoStabs.appendsym_const(list:TAsmList;sym:tconstsym);
  1215. var
  1216. st : string;
  1217. ss : ansistring;
  1218. begin
  1219. ss:='';
  1220. { Don't write info for default parameter values, the N_Func breaks
  1221. the N_Func for the function itself.
  1222. Valgrind does not support constants }
  1223. if (sym.owner.symtabletype=parasymtable) or
  1224. (cs_gdb_valgrind in current_settings.globalswitches) then
  1225. exit;
  1226. case sym.consttyp of
  1227. conststring:
  1228. begin
  1229. if sym.value.len<200 then
  1230. st:='s'''+backspace_quote(octal_quote(strpas(pchar(sym.value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+''''
  1231. else
  1232. st:='<constant string too long>';
  1233. end;
  1234. constord:
  1235. st:='i'+tostr(sym.value.valueord);
  1236. constpointer:
  1237. st:='i'+tostr(sym.value.valueordptr);
  1238. constreal:
  1239. begin
  1240. system.str(pbestreal(sym.value.valueptr)^,st);
  1241. st := 'r'+st;
  1242. end;
  1243. else
  1244. begin
  1245. { if we don't know just put zero !! }
  1246. st:='i0';
  1247. end;
  1248. end;
  1249. ss:=sym_stabstr_evaluate(sym,'"${name}:c=$1;",${N_FUNCTION},0,${line},0',[st]);
  1250. write_sym_stabstr(list,sym,ss);
  1251. end;
  1252. procedure TDebugInfoStabs.appendsym_type(list:TAsmList;sym:ttypesym);
  1253. var
  1254. ss : ansistring;
  1255. stabchar : string[2];
  1256. begin
  1257. ss:='';
  1258. if not assigned(sym.typedef) then
  1259. internalerror(200509262);
  1260. if sym.typedef.typ in tagtypes then
  1261. stabchar:='Tt'
  1262. else
  1263. stabchar:='t';
  1264. ss:=sym_stabstr_evaluate(sym,'"${name}:$1$2",${N_LSYM},0,${line},0',[stabchar,def_stab_number(sym.typedef)]);
  1265. write_sym_stabstr(list,sym,ss);
  1266. end;
  1267. procedure TDebugInfoStabs.appendsym_label(list:TAsmList;sym:tlabelsym);
  1268. var
  1269. ss : ansistring;
  1270. begin
  1271. ss:=sym_stabstr_evaluate(sym,'"${name}",${N_LSYM},0,${line},0',[]);
  1272. write_sym_stabstr(list,sym,ss);
  1273. end;
  1274. {****************************************************************************
  1275. Proc/Module support
  1276. ****************************************************************************}
  1277. procedure tdebuginfostabs.inserttypeinfo;
  1278. var
  1279. stabsvarlist,
  1280. stabstypelist : TAsmList;
  1281. storefilepos : tfileposinfo;
  1282. i : longint;
  1283. begin
  1284. storefilepos:=current_filepos;
  1285. current_filepos:=current_module.mainfilepos;
  1286. global_stab_number:=0;
  1287. defnumberlist:=TFPObjectlist.create(false);
  1288. stabsvarlist:=TAsmList.create;
  1289. stabstypelist:=TAsmList.create;
  1290. { include symbol that will be referenced from the main to be sure to
  1291. include this debuginfo .o file }
  1292. current_module.flags:=current_module.flags or uf_has_debuginfo;
  1293. if not(target_info.system in systems_darwin) then
  1294. begin
  1295. new_section(current_asmdata.asmlists[al_stabs],sec_data,GetSymTableName(current_module.localsymtable),0);
  1296. current_asmdata.asmlists[al_stabs].concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1297. end
  1298. else
  1299. new_section(current_asmdata.asmlists[al_stabs],sec_code,GetSymTableName(current_module.localsymtable),0);
  1300. { write all global/local variables. This will flag all required tdefs }
  1301. if assigned(current_module.globalsymtable) then
  1302. write_symtable_syms(stabsvarlist,current_module.globalsymtable);
  1303. if assigned(current_module.localsymtable) then
  1304. write_symtable_syms(stabsvarlist,current_module.localsymtable);
  1305. { write all procedures and methods. This will flag all required tdefs }
  1306. if assigned(current_module.globalsymtable) then
  1307. write_symtable_procdefs(stabsvarlist,current_module.globalsymtable);
  1308. if assigned(current_module.localsymtable) then
  1309. write_symtable_procdefs(stabsvarlist,current_module.localsymtable);
  1310. { reset unit type info flag }
  1311. reset_unit_type_info;
  1312. { write used types from the used units }
  1313. write_used_unit_type_info(stabstypelist,current_module);
  1314. { last write the types from this unit }
  1315. if assigned(current_module.globalsymtable) then
  1316. write_symtable_defs(stabstypelist,current_module.globalsymtable);
  1317. if assigned(current_module.localsymtable) then
  1318. write_symtable_defs(stabstypelist,current_module.localsymtable);
  1319. current_asmdata.asmlists[al_stabs].concatlist(stabstypelist);
  1320. current_asmdata.asmlists[al_stabs].concatlist(stabsvarlist);
  1321. { reset stab numbers }
  1322. for i:=0 to defnumberlist.count-1 do
  1323. begin
  1324. if assigned(defnumberlist[i]) then
  1325. begin
  1326. tdef(defnumberlist[i]).stab_number:=0;
  1327. tdef(defnumberlist[i]).dbg_state:=dbg_state_unused;
  1328. end;
  1329. end;
  1330. defnumberlist.free;
  1331. defnumberlist:=nil;
  1332. stabsvarlist.free;
  1333. stabstypelist.free;
  1334. current_filepos:=storefilepos;
  1335. end;
  1336. procedure tdebuginfostabs.insertlineinfo(list:TAsmList);
  1337. var
  1338. currfileinfo,
  1339. lastfileinfo : tfileposinfo;
  1340. currfuncname : pshortstring;
  1341. currsectype : TAsmSectiontype;
  1342. hlabel : tasmlabel;
  1343. hp : tai;
  1344. infile : tinputfile;
  1345. begin
  1346. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  1347. currfuncname:=nil;
  1348. currsectype:=sec_code;
  1349. hp:=Tai(list.first);
  1350. while assigned(hp) do
  1351. begin
  1352. case hp.typ of
  1353. ait_section :
  1354. currsectype:=tai_section(hp).sectype;
  1355. ait_function_name :
  1356. currfuncname:=tai_function_name(hp).funcname;
  1357. ait_force_line :
  1358. lastfileinfo.line:=-1;
  1359. end;
  1360. if (currsectype=sec_code) and
  1361. (hp.typ=ait_instruction) then
  1362. begin
  1363. currfileinfo:=tailineinfo(hp).fileinfo;
  1364. { file changed ? (must be before line info) }
  1365. if (currfileinfo.fileindex<>0) and
  1366. ((lastfileinfo.fileindex<>currfileinfo.fileindex) or
  1367. (lastfileinfo.moduleindex<>currfileinfo.moduleindex)) then
  1368. begin
  1369. infile:=get_module(currfileinfo.moduleindex).sourcefiles.get_file(currfileinfo.fileindex);
  1370. if assigned(infile) then
  1371. begin
  1372. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1373. { emit stabs }
  1374. if (infile.path^<>'') then
  1375. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_includefile)+
  1376. ',0,0,'+hlabel.name),hp);
  1377. list.insertbefore(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_includefile)+
  1378. ',0,0,'+hlabel.name),hp);
  1379. list.insertbefore(tai_label.create(hlabel),hp);
  1380. { force new line info }
  1381. lastfileinfo.line:=-1;
  1382. end;
  1383. end;
  1384. { line changed ? }
  1385. if (currfileinfo.line>lastfileinfo.line) and (currfileinfo.line<>0) then
  1386. begin
  1387. if assigned(currfuncname) and
  1388. (tf_use_function_relative_addresses in target_info.flags) then
  1389. begin
  1390. current_asmdata.getlabel(hlabel,alt_dbgline);
  1391. list.insertbefore(Tai_stab.Create_str(stab_stabn,tostr(n_textline)+',0,'+tostr(currfileinfo.line)+','+
  1392. hlabel.name+' - '+{$IFDEF POWERPC64}'.'+{$ENDIF POWERPC64}currfuncname^),hp);
  1393. list.insertbefore(tai_label.create(hlabel),hp);
  1394. end
  1395. else
  1396. list.insertbefore(Tai_stab.Create_str(stab_stabd,tostr(n_textline)+',0,'+tostr(currfileinfo.line)),hp);
  1397. end;
  1398. lastfileinfo:=currfileinfo;
  1399. end;
  1400. hp:=tai(hp.next);
  1401. end;
  1402. end;
  1403. procedure tdebuginfostabs.insertmoduleinfo;
  1404. var
  1405. hlabel : tasmlabel;
  1406. infile : tinputfile;
  1407. begin
  1408. { emit main source n_sourcefile for start of module }
  1409. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1410. infile:=current_module.sourcefiles.get_file(1);
  1411. new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),0,secorder_begin);
  1412. if not(target_info.system in systems_darwin) then
  1413. current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(make_mangledname('DEBUGSTART',current_module.localsymtable,''),AT_DATA,0));
  1414. if (infile.path^<>'') then
  1415. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_sourcefile)+
  1416. ',0,0,'+hlabel.name));
  1417. current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+FixFileName(infile.name^)+'",'+tostr(n_sourcefile)+
  1418. ',0,0,'+hlabel.name));
  1419. current_asmdata.asmlists[al_start].concat(tai_label.create(hlabel));
  1420. { for darwin, you need a "module marker" too to work around }
  1421. { either some assembler or gdb bug (radar 4386531 according to a }
  1422. { comment in dbxout.c of Apple's gcc) }
  1423. if (target_info.system in systems_darwin) then
  1424. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(N_OSO)+',0,0,0'));
  1425. { emit empty n_sourcefile for end of module }
  1426. current_asmdata.getlabel(hlabel,alt_dbgfile);
  1427. new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),0,secorder_end);
  1428. if not(target_info.system in systems_darwin) then
  1429. current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(make_mangledname('DEBUGEND',current_module.localsymtable,''),AT_DATA,0));
  1430. current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(n_sourcefile)+',0,0,'+hlabel.name));
  1431. current_asmdata.asmlists[al_end].concat(tai_label.create(hlabel));
  1432. end;
  1433. procedure tdebuginfostabs.referencesections(list:TAsmList);
  1434. var
  1435. hp : tmodule;
  1436. dbgtable : tai_symbol;
  1437. begin
  1438. { Reference all DEBUGINFO sections from the main .fpc section }
  1439. if (target_info.system in ([system_powerpc_macos]+systems_darwin)) then
  1440. exit;
  1441. list.concat(Tai_section.create(sec_fpc,'links',0));
  1442. { make sure the debuginfo doesn't get stripped out }
  1443. if (target_info.system in systems_darwin) then
  1444. begin
  1445. dbgtable:=tai_symbol.createname('DEBUGINFOTABLE',AT_DATA,0);
  1446. list.concat(tai_directive.create(asd_no_dead_strip,dbgtable.sym.name));
  1447. list.concat(dbgtable);
  1448. end;
  1449. { include reference to all debuginfo sections of used units }
  1450. hp:=tmodule(loaded_units.first);
  1451. while assigned(hp) do
  1452. begin
  1453. If (hp.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1454. begin
  1455. list.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.localsymtable,''),0));
  1456. list.concat(Tai_const.Createname(make_mangledname('DEBUGSTART',hp.localsymtable,''),0));
  1457. list.concat(Tai_const.Createname(make_mangledname('DEBUGEND',hp.localsymtable,''),0));
  1458. end;
  1459. hp:=tmodule(hp.next);
  1460. end;
  1461. end;
  1462. const
  1463. dbg_stabs_info : tdbginfo =
  1464. (
  1465. id : dbg_stabs;
  1466. idtxt : 'STABS';
  1467. );
  1468. initialization
  1469. RegisterDebugInfo(dbg_stabs_info,TDebugInfoStabs);
  1470. end.