1 // This may look like C code, but it is really -*- C++ -*-
2
3 // ------------------------------------------------------------------
4 // The Goldware Library
5 // Copyright (C) 1990-1999 Odinn Sorensen
6 // ------------------------------------------------------------------
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this program; if not, write to the Free
19 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 // MA 02111-1307, USA
21 // ------------------------------------------------------------------
22 // $Id: gdbgtrk.cpp,v 1.3 2009/04/19 20:49:49 stas_degteff Exp $
23 // ------------------------------------------------------------------
24 // Function tracker.
25 // ------------------------------------------------------------------
26
27 #include <gtimall.h>
28 #include <gdbgtrk.h>
29 #include <gmemdbg.h>
30 #include <glog.h>
31 #include <stdlib.h>
32
33 // ------------------------------------------------------------------
34
35 #if defined(GFTRK_ENABLE)
36
37
38 // ------------------------------------------------------------------
39 // Externals
40
41 extern glog LOG;
42
43
44 // ------------------------------------------------------------------
45 // Global vars
46
47 int __gftrk = false;
48 int __gftrk_on = true;
49 int __gftrk_max = 50;
50 int __gftrk_curr;
51 int __gftrk_indent;
52 GFTrk* __gftrk_ptr;
53 GFTrk* __gftrk_list;
54
55 int __gftrk_statusline = false;
56
57
58 // ------------------------------------------------------------------
59
__gftrk_term(void)60 static void __gftrk_term(void) {
61
62 if(__gftrk)
63 free(__gftrk_list);
64 }
65
66
67 // ------------------------------------------------------------------
68
__gftrk_init(int trackmax)69 void __gftrk_init(int trackmax) {
70
71 if(trackmax != -1)
72 __gftrk_max = trackmax;
73 __gftrk_list = (GFTrk*)calloc(__gftrk_max, sizeof(GFTrk));
74 if(__gftrk_list) {
75 __gftrk = true;
76 __gftrk_curr = 0;
77 __gftrk_indent = 0;
78 atexit(__gftrk_term);
79 }
80 }
81
82
83 // ------------------------------------------------------------------
84
85 extern bool cmdlinedebughg;
86 void update_statusline(const char* info);
87
__gftrk_track(const char * text)88 void __gftrk_track(const char* text) {
89
90 if(__gftrk_on) {
91 if(text) {
92 __gftrk_ptr = __gftrk_list + __gftrk_curr;
93 __gftrk_ptr->tick = clock();
94 __gftrk_ptr->text = *text ? text : text+1;
95 __gftrk_ptr->indent = __gftrk_indent;
96 if(*text) {
97 __gftrk_indent++;
98 if(__gftrk_statusline)
99 update_statusline(text);
100 if(cmdlinedebughg)
101 LOG.printf("- %08u %*s%s", __gftrk_ptr->tick, __gftrk_ptr->indent*2, "", __gftrk_ptr->text);
102 }
103 __gftrk_curr = (++__gftrk_curr) % __gftrk_max;
104 }
105 else {
106 __gftrk_indent--;
107 }
108 if(__gftrk_statusline)
109 THROW_CHECK();
110 }
111 }
112
113
114 // ------------------------------------------------------------------
115
__gftrk_log()116 void __gftrk_log() {
117
118 if(__gftrk) {
119 int _curr = __gftrk_curr - 1;
120 if(_curr == -1)
121 _curr = __gftrk_max - 1;
122 int _count = 0;
123 int _first = true;
124 while(_count < __gftrk_max) {
125 GFTrk* _ptr = __gftrk_list + _curr;
126 if(_ptr->text) {
127 if(_first) {
128 LOG.printf("! Function track dump follows:");
129 _first = false;
130 }
131 LOG.printf("- %08u %*s%s", _ptr->tick, _ptr->indent*2, "", _ptr->text);
132 }
133 _count++;
134 _curr--;
135 if(_curr == -1)
136 _curr = __gftrk_max - 1;
137 }
138 }
139 }
140
141
142 // ------------------------------------------------------------------
143
144 #endif
145
146 // ------------------------------------------------------------------
147