dbgbase.pas 17 KB

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