dbgstabs.pas 64 KB

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