browlog.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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,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. {$ifdef FPC}
  132. write(stderr,buf);
  133. {$else FPC}
  134. write(buf);
  135. {$endif FPC}
  136. end;
  137. bufidx:=0;
  138. end;
  139. procedure tbrowserlog.closelog;
  140. begin
  141. if logopen then
  142. begin
  143. flushlog;
  144. close(f);
  145. freemem(buf,logbufsize);
  146. logopen:=false;
  147. end;
  148. end;
  149. procedure tbrowserlog.list_elements;
  150. begin
  151. stderrlog:=true;
  152. getmem(buf,logbufsize);
  153. logopen:=true;
  154. while not elements_to_list.empty do
  155. browse_symbol(elements_to_list.getfirst);
  156. flushlog;
  157. logopen:=false;
  158. freemem(buf,logbufsize);
  159. stderrlog:=false;
  160. end;
  161. procedure tbrowserlog.list_debug_infos;
  162. {$ifndef debug}
  163. begin
  164. end;
  165. {$else debug}
  166. var
  167. hp : tmodule;
  168. ff : tinputfile;
  169. begin
  170. hp:=tmodule(loaded_units.first);
  171. while assigned(hp) do
  172. begin
  173. addlog('Unit '+hp.modulename^+' has index '+tostr(hp.unit_index));
  174. ff:=hp.sourcefiles.files;
  175. while assigned(ff) do
  176. begin
  177. addlog('File '+ff.name^+' index '+tostr(ff.ref_index));
  178. ff:=ff.ref_next;
  179. end;
  180. hp:=tmodule(hp.next);
  181. end;
  182. end;
  183. {$endif debug}
  184. procedure tbrowserlog.addlog(const s:string);
  185. begin
  186. if not logopen then
  187. exit;
  188. { add ident }
  189. if (identidx>0) and not stderrlog then
  190. begin
  191. if bufidx+identidx>logbufsize then
  192. flushlog;
  193. fillchar(buf[bufidx],identidx,' ');
  194. inc(bufidx,identidx);
  195. end;
  196. { add text }
  197. if bufidx+length(s)>logbufsize-2 then
  198. flushlog;
  199. move(s[1],buf[bufidx],length(s));
  200. inc(bufidx,length(s));
  201. { add crlf }
  202. buf[bufidx]:=target_info.newline[1];
  203. inc(bufidx);
  204. if length(target_info.newline)=2 then
  205. begin
  206. buf[bufidx]:=target_info.newline[2];
  207. inc(bufidx);
  208. end;
  209. end;
  210. procedure tbrowserlog.addlogrefs(p:tref);
  211. var
  212. ref : tref;
  213. begin
  214. ref:=p;
  215. Ident;
  216. while assigned(ref) do
  217. begin
  218. Browserlog.AddLog(get_file_line(ref));
  219. ref:=ref.nextref;
  220. end;
  221. Unident;
  222. end;
  223. procedure tbrowserlog.browse_symbol(const sr : string);
  224. var
  225. sym : tsym;
  226. symb : tstoredsym;
  227. symt : tsymtable;
  228. hp : tmodule;
  229. s,ss : string;
  230. p : byte;
  231. procedure next_substring;
  232. begin
  233. p:=pos('.',s);
  234. if p>0 then
  235. begin
  236. ss:=copy(s,1,p-1);
  237. s:=copy(s,p+1,255);
  238. end
  239. else
  240. begin
  241. ss:=s;
  242. s:='';
  243. end;
  244. addlog('substring : '+ss);
  245. end;
  246. begin
  247. { don't create a new reference when
  248. looking for the symbol !! }
  249. make_ref:=false;
  250. s:=sr;
  251. symt:=symtablestack.top;
  252. next_substring;
  253. if assigned(symt) then
  254. begin
  255. sym:=tstoredsym(symt.search(ss));
  256. if sym=nil then
  257. sym:=tstoredsym(symt.search(upper(ss)));
  258. end
  259. else
  260. sym:=nil;
  261. if assigned(sym) and (sym.typ=unitsym) and (s<>'') then
  262. begin
  263. addlog('Unitsym found !');
  264. symt:=tmodule(tunitsym(sym).module).globalsymtable;
  265. if assigned(symt) then
  266. begin
  267. next_substring;
  268. sym:=tstoredsym(symt.search(ss));
  269. end
  270. else
  271. sym:=nil;
  272. end;
  273. if not assigned(sym) then
  274. begin
  275. symt:=nil;
  276. { try all loaded_units }
  277. hp:=tmodule(loaded_units.first);
  278. while assigned(hp) do
  279. begin
  280. if hp.modulename^=upper(ss) then
  281. begin
  282. symt:=hp.globalsymtable;
  283. break;
  284. end;
  285. hp:=tmodule(hp.next);
  286. end;
  287. if not assigned(symt) then
  288. begin
  289. addlog('!!!Symbol '+ss+' not found !!!');
  290. make_ref:=true;
  291. exit;
  292. end
  293. else
  294. begin
  295. next_substring;
  296. sym:=tstoredsym(symt.search(ss));
  297. if sym=nil then
  298. sym:=tstoredsym(symt.search(upper(ss)));
  299. end;
  300. end;
  301. while assigned(sym) and (s<>'') do
  302. begin
  303. next_substring;
  304. case sym.typ of
  305. typesym :
  306. begin
  307. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  308. begin
  309. if ttypesym(sym).restype.def.deftype=recorddef then
  310. symt:=trecorddef(ttypesym(sym).restype.def).symtable
  311. else
  312. symt:=tobjectdef(ttypesym(sym).restype.def).symtable;
  313. sym:=tstoredsym(symt.search(ss));
  314. if sym=nil then
  315. sym:=tstoredsym(symt.search(upper(ss)));
  316. end;
  317. end;
  318. globalvarsym,
  319. localvarsym,
  320. paravarsym,
  321. fieldvarsym :
  322. begin
  323. if tabstractvarsym(sym).vartype.def.deftype in [recorddef,objectdef] then
  324. begin
  325. symt:=tabstractvarsym(sym).vartype.def.getsymtable(gs_record);
  326. sym:=tstoredsym(symt.search(ss));
  327. if sym=nil then
  328. sym:=tstoredsym(symt.search(upper(ss)));
  329. end;
  330. end;
  331. procsym :
  332. begin
  333. symt:=tprocsym(sym).first_procdef.parast;
  334. symb:=tstoredsym(symt.search(ss));
  335. if symb=nil then
  336. symb:=tstoredsym(symt.search(upper(ss)));
  337. if not assigned(symb) then
  338. begin
  339. symt:=tprocsym(sym).first_procdef.localst;
  340. sym:=tstoredsym(symt.search(ss));
  341. if symb=nil then
  342. symb:=tstoredsym(symt.search(upper(ss)));
  343. end
  344. else
  345. sym:=symb;
  346. end;
  347. end;
  348. end;
  349. if assigned(sym) then
  350. begin
  351. if assigned(sym.defref) then
  352. begin
  353. browserlog.AddLog('***'+sym.name+'***');
  354. browserlog.AddLogRefs(sym.defref);
  355. end;
  356. end
  357. else
  358. addlog('!!!Symbol '+ss+' not found !!!');
  359. make_ref:=true;
  360. end;
  361. procedure tbrowserlog.ident;
  362. begin
  363. inc(identidx,2);
  364. end;
  365. procedure tbrowserlog.unident;
  366. begin
  367. dec(identidx,2);
  368. end;
  369. procedure writesymtable(p:Tsymtable);forward;
  370. procedure writelocalsymtables(p:Tprocdef;arg:pointer);
  371. begin
  372. if assigned(p.defref) then
  373. begin
  374. browserlog.AddLog('***'+p.mangledname);
  375. browserlog.AddLogRefs(p.defref);
  376. if (current_module.flags and uf_local_browser)<>0 then
  377. begin
  378. if assigned(p.parast) then
  379. writesymtable(p.parast);
  380. if assigned(p.localst) then
  381. writesymtable(p.localst);
  382. end;
  383. end;
  384. end;
  385. procedure writesymtable(p:tsymtable);
  386. var
  387. hp : tsym;
  388. begin
  389. if cs_browser in aktmoduleswitches then
  390. begin
  391. if assigned(p.name) then
  392. Browserlog.AddLog('---Symtable '+p.name^)
  393. else
  394. begin
  395. if (p.symtabletype=recordsymtable) and
  396. assigned(tdef(p.defowner).typesym) then
  397. Browserlog.AddLog('---Symtable '+tdef(p.defowner).typesym.name)
  398. else
  399. Browserlog.AddLog('---Symtable with no name');
  400. end;
  401. Browserlog.Ident;
  402. hp:=tstoredsym(p.symindex.first);
  403. while assigned(hp) do
  404. begin
  405. if assigned(hp.defref) then
  406. begin
  407. browserlog.AddLog('***'+hp.name+'***');
  408. browserlog.AddLogRefs(hp.defref);
  409. end;
  410. case hp.typ of
  411. typesym :
  412. begin
  413. if (ttypesym(hp).restype.def.deftype=recorddef) then
  414. writesymtable(trecorddef(ttypesym(hp).restype.def).symtable);
  415. if (ttypesym(hp).restype.def.deftype=objectdef) then
  416. writesymtable(tobjectdef(ttypesym(hp).restype.def).symtable);
  417. end;
  418. procsym :
  419. Tprocsym(hp).foreach_procdef_static(@writelocalsymtables,nil);
  420. end;
  421. hp:=tstoredsym(hp.indexnext);
  422. end;
  423. browserlog.Unident;
  424. end;
  425. end;
  426. {****************************************************************************
  427. Helpers
  428. ****************************************************************************}
  429. procedure WriteBrowserLog;
  430. var
  431. p : tstoredsymtable;
  432. hp : tmodule;
  433. begin
  434. browserlog.CreateLog;
  435. browserlog.list_debug_infos;
  436. hp:=tmodule(loaded_units.first);
  437. while assigned(hp) do
  438. begin
  439. p:=tstoredsymtable(hp.globalsymtable);
  440. if assigned(p) then
  441. writesymtable(p);
  442. if cs_local_browser in aktmoduleswitches then
  443. begin
  444. p:=tstoredsymtable(hp.localsymtable);
  445. if assigned(p) then
  446. writesymtable(p);
  447. end;
  448. hp:=tmodule(hp.next);
  449. end;
  450. browserlog.CloseLog;
  451. end;
  452. procedure InitBrowserLog;
  453. begin
  454. browserlog.init;
  455. end;
  456. procedure DoneBrowserLog;
  457. begin
  458. browserlog.done;
  459. end;
  460. end.