dbgbase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. {
  2. Copyright (c) 2003-2006 by Peter Vreman and Florian Klaempfl
  3. This units contains the base class for 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 dbgbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. systems,
  23. parabase,
  24. symconst,symbase,symdef,symtype,symsym,symtable,
  25. fmodule,
  26. aasmtai,aasmdata;
  27. type
  28. TDebugInfo=class
  29. protected
  30. { definitions }
  31. { collect all defs in one list so we can reset them easily }
  32. defnumberlist : TFPObjectList;
  33. deftowritelist : TFPObjectList;
  34. procedure appenddef(list:TAsmList;def:tdef);
  35. procedure beforeappenddef(list:TAsmList;def:tdef);virtual;
  36. procedure afterappenddef(list:TAsmList;def:tdef);virtual;
  37. procedure appenddef_ord(list:TAsmList;def:torddef);virtual;
  38. procedure appenddef_float(list:TAsmList;def:tfloatdef);virtual;
  39. procedure appenddef_file(list:TAsmList;def:tfiledef);virtual;
  40. procedure appenddef_enum(list:TAsmList;def:tenumdef);virtual;
  41. procedure appenddef_array(list:TAsmList;def:tarraydef);virtual;
  42. procedure appenddef_record(list:TAsmList;def:trecorddef);virtual;
  43. procedure appenddef_object(list:TAsmList;def:tobjectdef);virtual;
  44. procedure appenddef_classref(list:TAsmList;def: tclassrefdef);virtual;
  45. procedure appenddef_pointer(list:TAsmList;def:tpointerdef);virtual;
  46. procedure appenddef_string(list:TAsmList;def:tstringdef);virtual;
  47. procedure appenddef_procvar(list:TAsmList;def:tprocvardef);virtual;
  48. procedure appenddef_variant(list:TAsmList;def:tvariantdef);virtual;
  49. procedure appenddef_set(list:TAsmList;def:tsetdef);virtual;
  50. procedure appenddef_formal(list:TAsmList;def:tformaldef);virtual;
  51. procedure appenddef_undefined(list:TAsmList;def: tundefineddef);virtual;
  52. procedure appendprocdef(list:TAsmList;def:tprocdef);virtual;
  53. procedure write_remaining_defs_to_write(list:TAsmList);
  54. { symbols }
  55. procedure appendsym(list:TAsmList;sym:tsym);
  56. procedure beforeappendsym(list:TAsmList;sym:tsym);virtual;
  57. procedure afterappendsym(list:TAsmList;sym:tsym);virtual;
  58. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);virtual;
  59. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);virtual;
  60. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);virtual;
  61. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);virtual;
  62. procedure appendsym_unit(list:TAsmList;sym:tunitsym);virtual;
  63. procedure appendsym_const(list:TAsmList;sym:tconstsym);virtual;
  64. procedure appendsym_type(list:TAsmList;sym:ttypesym);virtual;
  65. procedure appendsym_label(list:TAsmList;sym:tlabelsym);virtual;
  66. procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);virtual;
  67. procedure appendsym_property(list:TAsmList;sym:tpropertysym);virtual;
  68. { symtable }
  69. procedure write_symtable_parasyms(list:TAsmList;paras: tparalist);
  70. procedure write_symtable_syms(list:TAsmList;st:TSymtable);
  71. procedure write_symtable_defs(list:TAsmList;st:TSymtable);
  72. procedure write_symtable_procdefs(list:TAsmList;st:TSymtable);
  73. procedure reset_unit_type_info;
  74. procedure write_used_unit_type_info(list:TAsmList;hp:tmodule);
  75. public
  76. constructor Create;virtual;
  77. procedure inserttypeinfo;virtual;
  78. procedure insertmoduleinfo;virtual;
  79. procedure insertlineinfo(list:TAsmList);virtual;
  80. procedure referencesections(list:TAsmList);virtual;
  81. end;
  82. TDebugInfoClass=class of TDebugInfo;
  83. var
  84. CDebugInfo : array[tdbg] of TDebugInfoClass;
  85. current_debuginfo : tdebuginfo;
  86. procedure InitDebugInfo(hp:tmodule);
  87. procedure DoneDebugInfo(hp:tmodule);
  88. procedure RegisterDebugInfo(const r:tdbginfo;c:TDebugInfoClass);
  89. implementation
  90. uses
  91. cutils,
  92. verbose;
  93. constructor TDebugInfo.Create;
  94. begin
  95. end;
  96. procedure TDebugInfo.insertmoduleinfo;
  97. begin
  98. end;
  99. procedure TDebugInfo.inserttypeinfo;
  100. begin
  101. end;
  102. procedure TDebugInfo.insertlineinfo(list:TAsmList);
  103. begin
  104. end;
  105. procedure TDebugInfo.referencesections(list:TAsmList);
  106. begin
  107. end;
  108. {**************************************
  109. Definition
  110. **************************************}
  111. procedure TDebugInfo.appendprocdef(list:TAsmList;def:tprocdef);
  112. begin
  113. end;
  114. procedure TDebugInfo.beforeappenddef(list:TAsmList;def:tdef);
  115. begin
  116. end;
  117. procedure TDebugInfo.afterappenddef(list:TAsmList;def:tdef);
  118. begin
  119. end;
  120. procedure TDebugInfo.appenddef_ord(list:TAsmList;def:torddef);
  121. begin
  122. end;
  123. procedure TDebugInfo.appenddef_float(list:TAsmList;def:tfloatdef);
  124. begin
  125. end;
  126. procedure TDebugInfo.appenddef_formal(list:TAsmList;def: tformaldef);
  127. begin
  128. end;
  129. procedure TDebugInfo.appenddef_undefined(list:TAsmList;def: tundefineddef);
  130. begin
  131. end;
  132. procedure TDebugInfo.appenddef_set(list:TAsmList;def: tsetdef);
  133. begin
  134. end;
  135. procedure TDebugInfo.appenddef_object(list:TAsmList;def: tobjectdef);
  136. begin
  137. end;
  138. procedure TDebugInfo.appenddef_classref(list:TAsmList;def: tclassrefdef);
  139. begin
  140. appenddef_pointer(list,tpointerdef(pvmttype));
  141. end;
  142. procedure TDebugInfo.appenddef_variant(list:TAsmList;def: tvariantdef);
  143. begin
  144. end;
  145. procedure TDebugInfo.appenddef_enum(list:TAsmList;def:tenumdef);
  146. begin
  147. end;
  148. procedure TDebugInfo.appenddef_file(list:TAsmList;def: tfiledef);
  149. begin
  150. end;
  151. procedure TDebugInfo.appenddef_array(list:TAsmList;def:tarraydef);
  152. begin
  153. end;
  154. procedure TDebugInfo.appenddef_record(list:TAsmList;def:trecorddef);
  155. begin
  156. end;
  157. procedure TDebugInfo.appenddef_pointer(list:TAsmList;def:tpointerdef);
  158. begin
  159. end;
  160. procedure TDebugInfo.appenddef_string(list:TAsmList;def:tstringdef);
  161. begin
  162. end;
  163. procedure TDebugInfo.appenddef_procvar(list:TAsmList;def:tprocvardef);
  164. begin
  165. end;
  166. procedure TDebugInfo.appenddef(list:TAsmList;def:tdef);
  167. begin
  168. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  169. exit;
  170. { never write generic template defs }
  171. if df_generic in def.defoptions then
  172. begin
  173. def.dbg_state:=dbg_state_written;
  174. exit;
  175. end;
  176. { to avoid infinite loops }
  177. def.dbg_state := dbg_state_writing;
  178. beforeappenddef(list,def);
  179. { queued defs have to be written later }
  180. if (def.dbg_state=dbg_state_queued) then
  181. exit;
  182. case def.typ of
  183. stringdef :
  184. appenddef_string(list,tstringdef(def));
  185. enumdef :
  186. appenddef_enum(list,tenumdef(def));
  187. orddef :
  188. appenddef_ord(list,torddef(def));
  189. pointerdef :
  190. appenddef_pointer(list,tpointerdef(def));
  191. floatdef :
  192. appenddef_float(list,tfloatdef(def));
  193. filedef :
  194. appenddef_file(list,tfiledef(def));
  195. recorddef :
  196. appenddef_record(list,trecorddef(def));
  197. variantdef :
  198. appenddef_variant(list,tvariantdef(def));
  199. classrefdef :
  200. appenddef_classref(list,tclassrefdef(def));
  201. setdef :
  202. appenddef_set(list,tsetdef(def));
  203. formaldef :
  204. appenddef_formal(list,tformaldef(def));
  205. arraydef :
  206. appenddef_array(list,tarraydef(def));
  207. procvardef :
  208. appenddef_procvar(list,tprocvardef(def));
  209. objectdef :
  210. appenddef_object(list,tobjectdef(def));
  211. undefineddef :
  212. appenddef_undefined(list,tundefineddef(def));
  213. procdef :
  214. begin
  215. { procdefs are already written in a separate step. procdef
  216. support in appenddef is only needed for beforeappenddef to
  217. write all local type defs }
  218. end;
  219. else
  220. internalerror(200601281);
  221. end;
  222. afterappenddef(list,def);
  223. def.dbg_state := dbg_state_written;
  224. end;
  225. procedure TDebugInfo.write_remaining_defs_to_write(list:TAsmList);
  226. var
  227. n : integer;
  228. looplist,
  229. templist: TFPObjectList;
  230. def : tdef;
  231. begin
  232. templist := TFPObjectList.Create(False);
  233. looplist := deftowritelist;
  234. while looplist.count > 0 do
  235. begin
  236. deftowritelist := templist;
  237. for n := 0 to looplist.count - 1 do
  238. begin
  239. def := tdef(looplist[n]);
  240. case def.dbg_state of
  241. dbg_state_written:
  242. continue;
  243. dbg_state_writing:
  244. internalerror(200610052);
  245. dbg_state_unused:
  246. internalerror(200610053);
  247. dbg_state_used:
  248. appenddef(list,def);
  249. else
  250. internalerror(200610054);
  251. end;
  252. end;
  253. looplist.clear;
  254. templist := looplist;
  255. looplist := deftowritelist;
  256. end;
  257. templist.free;
  258. end;
  259. {**************************************
  260. Symbols
  261. **************************************}
  262. procedure TDebugInfo.beforeappendsym(list:TAsmList;sym:tsym);
  263. begin
  264. end;
  265. procedure TDebugInfo.afterappendsym(list:TAsmList;sym:tsym);
  266. begin
  267. end;
  268. procedure TDebugInfo.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  269. begin
  270. end;
  271. procedure TDebugInfo.appendsym_paravar(list:TAsmList;sym: tparavarsym);
  272. begin
  273. end;
  274. procedure TDebugInfo.appendsym_localvar(list:TAsmList;sym: tlocalvarsym);
  275. begin
  276. end;
  277. procedure TDebugInfo.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  278. begin
  279. end;
  280. procedure TDebugInfo.appendsym_const(list:TAsmList;sym:tconstsym);
  281. begin
  282. end;
  283. procedure TDebugInfo.appendsym_label(list:TAsmList;sym: tlabelsym);
  284. begin
  285. end;
  286. procedure TDebugInfo.appendsym_property(list:TAsmList;sym: tpropertysym);
  287. begin
  288. end;
  289. procedure TDebugInfo.appendsym_type(list:TAsmList;sym: ttypesym);
  290. begin
  291. end;
  292. procedure TDebugInfo.appendsym_unit(list:TAsmList;sym: tunitsym);
  293. begin
  294. end;
  295. procedure TDebugInfo.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  296. begin
  297. end;
  298. procedure TDebugInfo.appendsym(list:TAsmList;sym:tsym);
  299. begin
  300. if sym.isdbgwritten then
  301. exit;
  302. beforeappendsym(list,sym);
  303. case sym.typ of
  304. staticvarsym :
  305. appendsym_staticvar(list,tstaticvarsym(sym));
  306. unitsym:
  307. appendsym_unit(list,tunitsym(sym));
  308. labelsym :
  309. appendsym_label(list,tlabelsym(sym));
  310. localvarsym :
  311. appendsym_localvar(list,tlocalvarsym(sym));
  312. paravarsym :
  313. appendsym_paravar(list,tparavarsym(sym));
  314. constsym :
  315. appendsym_const(list,tconstsym(sym));
  316. typesym :
  317. appendsym_type(list,ttypesym(sym));
  318. enumsym :
  319. { ignore enum syms, they are written by the owner }
  320. ;
  321. syssym :
  322. { ignore sys syms, they are only of internal use }
  323. ;
  324. procsym :
  325. { ignore proc syms, they are written by procdefs }
  326. ;
  327. absolutevarsym :
  328. appendsym_absolute(list,tabsolutevarsym(sym));
  329. propertysym :
  330. appendsym_property(list,tpropertysym(sym));
  331. else
  332. internalerror(200601242);
  333. end;
  334. afterappendsym(list,sym);
  335. sym.isdbgwritten:=true;
  336. end;
  337. {**************************************
  338. Symtables
  339. **************************************}
  340. procedure TDebugInfo.write_symtable_defs(list:TAsmList;st:TSymtable);
  341. var
  342. def : tdef;
  343. i : longint;
  344. nonewadded : boolean;
  345. begin
  346. case st.symtabletype of
  347. staticsymtable :
  348. list.concat(tai_comment.Create(strpnew('Defs - Begin Staticsymtable')));
  349. globalsymtable :
  350. list.concat(tai_comment.Create(strpnew('Defs - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  351. end;
  352. repeat
  353. nonewadded:=true;
  354. for i:=0 to st.DefList.Count-1 do
  355. begin
  356. def:=tdef(st.DefList[i]);
  357. if (def.dbg_state in [dbg_state_used,dbg_state_queued]) then
  358. begin
  359. appenddef(list,def);
  360. nonewadded:=false;
  361. end;
  362. end;
  363. until nonewadded;
  364. case st.symtabletype of
  365. staticsymtable :
  366. list.concat(tai_comment.Create(strpnew('Defs - End Staticsymtable')));
  367. globalsymtable :
  368. list.concat(tai_comment.Create(strpnew('Defs - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  369. end;
  370. end;
  371. procedure TDebugInfo.write_symtable_parasyms(list:TAsmList;paras: tparalist);
  372. var
  373. i : longint;
  374. sym : tsym;
  375. begin
  376. for i:=0 to paras.Count-1 do
  377. begin
  378. sym:=tsym(paras[i]);
  379. if (sym.visibility<>vis_hidden) then
  380. begin
  381. appendsym(list,sym);
  382. { if we ever write this procdef again for some reason (this
  383. can happen with DWARF), then we want to write all the
  384. parasyms again as well. }
  385. sym.isdbgwritten:=false;
  386. end;
  387. end;
  388. end;
  389. procedure TDebugInfo.write_symtable_syms(list:TAsmList;st:TSymtable);
  390. var
  391. i : longint;
  392. sym : tsym;
  393. begin
  394. case st.symtabletype of
  395. staticsymtable :
  396. list.concat(tai_comment.Create(strpnew('Syms - Begin Staticsymtable')));
  397. globalsymtable :
  398. list.concat(tai_comment.Create(strpnew('Syms - Begin unit '+st.name^+' has index '+tostr(st.moduleid))));
  399. end;
  400. for i:=0 to st.SymList.Count-1 do
  401. begin
  402. sym:=tsym(st.SymList[i]);
  403. if (sym.visibility<>vis_hidden) and
  404. (not sym.isdbgwritten) then
  405. appendsym(list,sym);
  406. end;
  407. case st.symtabletype of
  408. staticsymtable :
  409. list.concat(tai_comment.Create(strpnew('Syms - End Staticsymtable')));
  410. globalsymtable :
  411. list.concat(tai_comment.Create(strpnew('Syms - End unit '+st.name^+' has index '+tostr(st.moduleid))));
  412. end;
  413. end;
  414. procedure TDebugInfo.write_symtable_procdefs(list:TAsmList;st:TSymtable);
  415. var
  416. i : longint;
  417. def : tdef;
  418. begin
  419. for i:=0 to st.DefList.Count-1 do
  420. begin
  421. def:=tdef(st.DefList[i]);
  422. case def.typ of
  423. procdef :
  424. begin
  425. appendprocdef(list,tprocdef(def));
  426. if assigned(tprocdef(def).localst) then
  427. write_symtable_procdefs(list,tprocdef(def).localst);
  428. end;
  429. objectdef,recorddef :
  430. begin
  431. write_symtable_procdefs(list,tabstractrecorddef(def).symtable);
  432. end;
  433. end;
  434. end;
  435. end;
  436. procedure TDebugInfo.reset_unit_type_info;
  437. var
  438. hp : tmodule;
  439. begin
  440. hp:=tmodule(loaded_units.first);
  441. while assigned(hp) do
  442. begin
  443. hp.is_dbginfo_written:=false;
  444. hp:=tmodule(hp.next);
  445. end;
  446. end;
  447. procedure TDebugInfo.write_used_unit_type_info(list:TAsmList;hp:tmodule);
  448. var
  449. pu : tused_unit;
  450. begin
  451. pu:=tused_unit(hp.used_units.first);
  452. while assigned(pu) do
  453. begin
  454. if not pu.u.is_dbginfo_written then
  455. begin
  456. { prevent infinte loop for circular dependencies }
  457. pu.u.is_dbginfo_written:=true;
  458. { write type info from used units, use a depth first
  459. strategy to reduce the recursion in writing all
  460. dependent stabs }
  461. write_used_unit_type_info(list,pu.u);
  462. if assigned(pu.u.globalsymtable) then
  463. write_symtable_defs(list,pu.u.globalsymtable);
  464. end;
  465. pu:=tused_unit(pu.next);
  466. end;
  467. end;
  468. {****************************************************************************
  469. Init / Done
  470. ****************************************************************************}
  471. procedure InitDebugInfo(hp:tmodule);
  472. begin
  473. if not assigned(CDebugInfo[target_dbg.id]) then
  474. begin
  475. Comment(V_Fatal,'cg_f_debuginfo_output_not_supported');
  476. exit;
  477. end;
  478. hp.DebugInfo:=CDebugInfo[target_dbg.id].Create;
  479. end;
  480. procedure DoneDebugInfo(hp:tmodule);
  481. begin
  482. if assigned(hp.DebugInfo) then
  483. begin
  484. hp.DebugInfo.Free;
  485. hp.DebugInfo:=nil;
  486. end;
  487. end;
  488. procedure RegisterDebugInfo(const r:tdbginfo;c:TDebugInfoClass);
  489. var
  490. t : tdbg;
  491. begin
  492. t:=r.id;
  493. if assigned(dbginfos[t]) then
  494. writeln('Warning: DebugInfo is already registered!')
  495. else
  496. Getmem(dbginfos[t],sizeof(tdbginfo));
  497. dbginfos[t]^:=r;
  498. CDebugInfo[t]:=c;
  499. end;
  500. const
  501. dbg_none_info : tdbginfo =
  502. (
  503. id : dbg_none;
  504. idtxt : 'NONE';
  505. );
  506. initialization
  507. RegisterDebugInfo(dbg_none_info,TDebugInfo);
  508. end.