dbgbase.pas 17 KB

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