browlog.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Pierre Muller
  3. Support routines for creating the browser log
  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 browlog;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,
  23. fmodule,finput,
  24. symbase,symconst,symtype,symsym,symdef,symtable;
  25. const
  26. logbufsize = 16384;
  27. type
  28. pbrowserlog=^tbrowserlog;
  29. tbrowserlog=object
  30. fname : string;
  31. logopen : boolean;
  32. stderrlog : boolean;
  33. f : file;
  34. elements_to_list : tstringlist;
  35. buf : pchar;
  36. bufidx : longint;
  37. identidx : longint;
  38. constructor init;
  39. destructor done;
  40. procedure setfilename(const fn:string);
  41. procedure createlog;
  42. procedure flushlog;
  43. procedure addlog(const s:string);
  44. procedure addlogrefs(p:tref);
  45. procedure closelog;
  46. procedure ident;
  47. procedure unident;
  48. procedure browse_symbol(const sr : string);
  49. procedure list_elements;
  50. procedure list_debug_infos;
  51. end;
  52. var
  53. browserlog : tbrowserlog;
  54. procedure WriteBrowserLog;
  55. procedure InitBrowserLog;
  56. procedure DoneBrowserLog;
  57. implementation
  58. uses
  59. cutils,cfileutils,comphook,
  60. globals,systems,
  61. ppu;
  62. function get_file_line(ref:tref): string;
  63. var
  64. inputfile : tinputfile;
  65. begin
  66. get_file_line:='';
  67. with ref do
  68. begin
  69. inputfile:=get_source_file(moduleindex,posinfo.fileindex);
  70. if assigned(inputfile) then
  71. if status.use_gccoutput then
  72. { for use with rhide
  73. add warning so that it does not interpret
  74. this as an error !! }
  75. get_file_line:=lower(inputfile.name^)
  76. +':'+tostr(posinfo.line)+': warning: '+tostr(posinfo.column)+':'
  77. else
  78. get_file_line:=inputfile.name^
  79. +'('+tostr(posinfo.line)+','+tostr(posinfo.column)+')'
  80. else
  81. if status.use_gccoutput then
  82. get_file_line:='file_unknown:'
  83. +tostr(posinfo.line)+': warning: '+tostr(posinfo.column)+':'
  84. else
  85. get_file_line:='file_unknown('
  86. +tostr(posinfo.line)+','+tostr(posinfo.column)+')'
  87. end;
  88. end;
  89. {****************************************************************************
  90. TBrowser
  91. ****************************************************************************}
  92. constructor tbrowserlog.init;
  93. begin
  94. fname:=FixFileName('browser.log');
  95. logopen:=false;
  96. elements_to_list:=TStringList.Create;
  97. end;
  98. destructor tbrowserlog.done;
  99. begin
  100. if logopen then
  101. closelog;
  102. elements_to_list.free;
  103. end;
  104. procedure tbrowserlog.setfilename(const fn:string);
  105. begin
  106. fname:=FixFileName(fn);
  107. end;
  108. procedure tbrowserlog.createlog;
  109. begin
  110. if logopen then
  111. closelog;
  112. assign(f,fname);
  113. {$I-}
  114. rewrite(f,1);
  115. {$I+}
  116. if ioresult<>0 then
  117. exit;
  118. logopen:=true;
  119. getmem(buf,logbufsize);
  120. bufidx:=0;
  121. identidx:=0;
  122. end;
  123. procedure tbrowserlog.flushlog;
  124. begin
  125. if logopen then
  126. if not stderrlog then
  127. blockwrite(f,buf^,bufidx)
  128. else
  129. begin
  130. buf[bufidx]:=#0;
  131. write(stderr,buf);
  132. end;
  133. bufidx:=0;
  134. end;
  135. procedure tbrowserlog.closelog;
  136. begin
  137. if logopen then
  138. begin
  139. flushlog;
  140. close(f);
  141. freemem(buf,logbufsize);
  142. logopen:=false;
  143. end;
  144. end;
  145. procedure tbrowserlog.list_elements;
  146. begin
  147. stderrlog:=true;
  148. getmem(buf,logbufsize);
  149. logopen:=true;
  150. while not elements_to_list.empty do
  151. browse_symbol(elements_to_list.getfirst);
  152. flushlog;
  153. logopen:=false;
  154. freemem(buf,logbufsize);
  155. stderrlog:=false;
  156. end;
  157. procedure tbrowserlog.list_debug_infos;
  158. {$ifndef debug}
  159. begin
  160. end;
  161. {$else debug}
  162. var
  163. hp : tmodule;
  164. ff : tinputfile;
  165. begin
  166. hp:=tmodule(loaded_units.first);
  167. while assigned(hp) do
  168. begin
  169. addlog('Unit '+hp.modulename^+' has index '+tostr(hp.unit_index));
  170. ff:=hp.sourcefiles.files;
  171. while assigned(ff) do
  172. begin
  173. addlog('File '+ff.name^+' index '+tostr(ff.ref_index));
  174. ff:=ff.ref_next;
  175. end;
  176. hp:=tmodule(hp.next);
  177. end;
  178. end;
  179. {$endif debug}
  180. procedure tbrowserlog.addlog(const s:string);
  181. begin
  182. if not logopen then
  183. exit;
  184. { add ident }
  185. if (identidx>0) and not stderrlog then
  186. begin
  187. if bufidx+identidx>logbufsize then
  188. flushlog;
  189. fillchar(buf[bufidx],identidx,' ');
  190. inc(bufidx,identidx);
  191. end;
  192. { add text }
  193. if bufidx+length(s)>logbufsize-2 then
  194. flushlog;
  195. move(s[1],buf[bufidx],length(s));
  196. inc(bufidx,length(s));
  197. { add crlf }
  198. buf[bufidx]:=target_info.newline[1];
  199. inc(bufidx);
  200. if length(target_info.newline)=2 then
  201. begin
  202. buf[bufidx]:=target_info.newline[2];
  203. inc(bufidx);
  204. end;
  205. end;
  206. procedure tbrowserlog.addlogrefs(p:tref);
  207. var
  208. ref : tref;
  209. begin
  210. ref:=p;
  211. Ident;
  212. while assigned(ref) do
  213. begin
  214. Browserlog.AddLog(get_file_line(ref));
  215. ref:=ref.nextref;
  216. end;
  217. Unident;
  218. end;
  219. procedure tbrowserlog.browse_symbol(const sr : string);
  220. var
  221. sym : tsym;
  222. symb : tstoredsym;
  223. symt : tsymtable;
  224. hp : tmodule;
  225. s,ss : string;
  226. p : byte;
  227. procedure next_substring;
  228. begin
  229. p:=pos('.',s);
  230. if p>0 then
  231. begin
  232. ss:=copy(s,1,p-1);
  233. s:=copy(s,p+1,255);
  234. end
  235. else
  236. begin
  237. ss:=s;
  238. s:='';
  239. end;
  240. addlog('substring : '+ss);
  241. end;
  242. begin
  243. { don't create a new reference when
  244. looking for the symbol !! }
  245. make_ref:=false;
  246. s:=sr;
  247. symt:=symtablestack.top;
  248. next_substring;
  249. if assigned(symt) then
  250. begin
  251. sym:=tstoredsym(symt.search(ss));
  252. if sym=nil then
  253. sym:=tstoredsym(symt.search(upper(ss)));
  254. end
  255. else
  256. sym:=nil;
  257. if assigned(sym) and (sym.typ=unitsym) and (s<>'') then
  258. begin
  259. addlog('Unitsym found !');
  260. symt:=tmodule(tunitsym(sym).module).globalsymtable;
  261. if assigned(symt) then
  262. begin
  263. next_substring;
  264. sym:=tstoredsym(symt.search(ss));
  265. end
  266. else
  267. sym:=nil;
  268. end;
  269. if not assigned(sym) then
  270. begin
  271. symt:=nil;
  272. { try all loaded_units }
  273. hp:=tmodule(loaded_units.first);
  274. while assigned(hp) do
  275. begin
  276. if hp.modulename^=upper(ss) then
  277. begin
  278. symt:=hp.globalsymtable;
  279. break;
  280. end;
  281. hp:=tmodule(hp.next);
  282. end;
  283. if not assigned(symt) then
  284. begin
  285. addlog('!!!Symbol '+ss+' not found !!!');
  286. make_ref:=true;
  287. exit;
  288. end
  289. else
  290. begin
  291. next_substring;
  292. sym:=tstoredsym(symt.search(ss));
  293. if sym=nil then
  294. sym:=tstoredsym(symt.search(upper(ss)));
  295. end;
  296. end;
  297. while assigned(sym) and (s<>'') do
  298. begin
  299. next_substring;
  300. case sym.typ of
  301. typesym :
  302. begin
  303. if ttypesym(sym).typedef.deftype in [recorddef,objectdef] then
  304. begin
  305. if ttypesym(sym).typedef.deftype=recorddef then
  306. symt:=trecorddef(ttypesym(sym).typedef).symtable
  307. else
  308. symt:=tobjectdef(ttypesym(sym).typedef).symtable;
  309. sym:=tstoredsym(symt.search(ss));
  310. if sym=nil then
  311. sym:=tstoredsym(symt.search(upper(ss)));
  312. end;
  313. end;
  314. globalvarsym,
  315. localvarsym,
  316. paravarsym,
  317. fieldvarsym :
  318. begin
  319. if tabstractvarsym(sym).vardef.deftype in [recorddef,objectdef] then
  320. begin
  321. symt:=tabstractvarsym(sym).vardef.getsymtable(gs_record);
  322. sym:=tstoredsym(symt.search(ss));
  323. if sym=nil then
  324. sym:=tstoredsym(symt.search(upper(ss)));
  325. end;
  326. end;
  327. procsym :
  328. begin
  329. symt:=tprocsym(sym).first_procdef.parast;
  330. symb:=tstoredsym(symt.search(ss));
  331. if symb=nil then
  332. symb:=tstoredsym(symt.search(upper(ss)));
  333. if not assigned(symb) then
  334. begin
  335. symt:=tprocsym(sym).first_procdef.localst;
  336. sym:=tstoredsym(symt.search(ss));
  337. if symb=nil then
  338. symb:=tstoredsym(symt.search(upper(ss)));
  339. end
  340. else
  341. sym:=symb;
  342. end;
  343. end;
  344. end;
  345. if assigned(sym) then
  346. begin
  347. if assigned(sym.defref) then
  348. begin
  349. browserlog.AddLog('***'+sym.name+'***');
  350. browserlog.AddLogRefs(sym.defref);
  351. end;
  352. end
  353. else
  354. addlog('!!!Symbol '+ss+' not found !!!');
  355. make_ref:=true;
  356. end;
  357. procedure tbrowserlog.ident;
  358. begin
  359. inc(identidx,2);
  360. end;
  361. procedure tbrowserlog.unident;
  362. begin
  363. dec(identidx,2);
  364. end;
  365. procedure writesymtable(p:Tsymtable);forward;
  366. procedure writelocalsymtables(p:Tprocdef;arg:pointer);
  367. begin
  368. if assigned(p.defref) then
  369. begin
  370. browserlog.AddLog('***'+p.mangledname);
  371. browserlog.AddLogRefs(p.defref);
  372. if (current_module.flags and uf_local_browser)<>0 then
  373. begin
  374. if assigned(p.parast) then
  375. writesymtable(p.parast);
  376. if assigned(p.localst) then
  377. writesymtable(p.localst);
  378. end;
  379. end;
  380. end;
  381. procedure writesymtable(p:tsymtable);
  382. var
  383. hp : tsym;
  384. begin
  385. if cs_browser in current_settings.moduleswitches then
  386. begin
  387. if assigned(p.name) then
  388. Browserlog.AddLog('---Symtable '+p.name^)
  389. else
  390. begin
  391. if (p.symtabletype=recordsymtable) and
  392. assigned(tdef(p.defowner).typesym) then
  393. Browserlog.AddLog('---Symtable '+tdef(p.defowner).typesym.name)
  394. else
  395. Browserlog.AddLog('---Symtable with no name');
  396. end;
  397. Browserlog.Ident;
  398. hp:=tstoredsym(p.symindex.first);
  399. while assigned(hp) do
  400. begin
  401. if assigned(hp.defref) then
  402. begin
  403. browserlog.AddLog('***'+hp.name+'***');
  404. browserlog.AddLogRefs(hp.defref);
  405. end;
  406. case hp.typ of
  407. typesym :
  408. begin
  409. if (ttypesym(hp).typedef.deftype=recorddef) then
  410. writesymtable(trecorddef(ttypesym(hp).typedef).symtable);
  411. if (ttypesym(hp).typedef.deftype=objectdef) then
  412. writesymtable(tobjectdef(ttypesym(hp).typedef).symtable);
  413. end;
  414. procsym :
  415. Tprocsym(hp).foreach_procdef_static(@writelocalsymtables,nil);
  416. end;
  417. hp:=tstoredsym(hp.indexnext);
  418. end;
  419. browserlog.Unident;
  420. end;
  421. end;
  422. {****************************************************************************
  423. Helpers
  424. ****************************************************************************}
  425. procedure WriteBrowserLog;
  426. var
  427. p : tstoredsymtable;
  428. hp : tmodule;
  429. begin
  430. browserlog.CreateLog;
  431. browserlog.list_debug_infos;
  432. hp:=tmodule(loaded_units.first);
  433. while assigned(hp) do
  434. begin
  435. p:=tstoredsymtable(hp.globalsymtable);
  436. if assigned(p) then
  437. writesymtable(p);
  438. if cs_local_browser in current_settings.moduleswitches then
  439. begin
  440. p:=tstoredsymtable(hp.localsymtable);
  441. if assigned(p) then
  442. writesymtable(p);
  443. end;
  444. hp:=tmodule(hp.next);
  445. end;
  446. browserlog.CloseLog;
  447. end;
  448. procedure InitBrowserLog;
  449. begin
  450. browserlog.init;
  451. end;
  452. procedure DoneBrowserLog;
  453. begin
  454. browserlog.done;
  455. end;
  456. end.