2
0

dbgbase.pas 19 KB

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