trace.cc (4042:dbd98b2264ed) trace.cc (4046:ef34b290091e)
1/*
2 * Copyright (c) 2001-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 23 unchanged lines hidden (view full) ---

32#include <ctype.h>
33#include <fstream>
34#include <iostream>
35#include <list>
36#include <string>
37#include <vector>
38
39#include "base/misc.hh"
1/*
2 * Copyright (c) 2001-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 23 unchanged lines hidden (view full) ---

32#include <ctype.h>
33#include <fstream>
34#include <iostream>
35#include <list>
36#include <string>
37#include <vector>
38
39#include "base/misc.hh"
40#include "base/trace.hh"
40#include "base/output.hh"
41#include "base/str.hh"
41#include "base/str.hh"
42#include "base/trace.hh"
42#include "base/varargs.hh"
43
44using namespace std;
45
46namespace Trace {
47const string DefaultName("global");
48FlagVec flags(NumFlags, false);
49bool enabled = true;
50
51//
52// This variable holds the output stream for debug information. Other
53// than setting up/redirecting this stream, do *NOT* reference this
54// directly; use DebugOut() (see below) to access this stream for
55// output.
56//
57ostream *dprintf_stream = &cerr;
43#include "base/varargs.hh"
44
45using namespace std;
46
47namespace Trace {
48const string DefaultName("global");
49FlagVec flags(NumFlags, false);
50bool enabled = true;
51
52//
53// This variable holds the output stream for debug information. Other
54// than setting up/redirecting this stream, do *NOT* reference this
55// directly; use DebugOut() (see below) to access this stream for
56// output.
57//
58ostream *dprintf_stream = &cerr;
58
59ObjectMatch ignore;
60
61Log theLog;
62
63Log::Log()
59ostream &
60output()
64{
61{
65 size = 0;
66 buffer = NULL;
62 return *dprintf_stream;
67}
68
63}
64
69
70void
65void
71Log::init(int _size)
66setOutput(const string &filename)
72{
67{
73 if (buffer != NULL) {
74 fatal("Trace::Log::init called twice!");
75 }
76
77 size = _size;
78
79 buffer = new Record *[size];
80
81 for (int i = 0; i < size; ++i) {
82 buffer[i] = NULL;
83 }
84
85 nextRecPtr = &buffer[0];
86 wrapRecPtr = &buffer[size];
68 dprintf_stream = simout.find(filename);
87}
88
69}
70
71ObjectMatch ignore;
89
72
90Log::~Log()
73void
74dprintf(Tick when, const std::string &name, const char *format,
75 CPRINTF_DEFINITION)
91{
76{
92 for (int i = 0; i < size; ++i) {
93 delete buffer[i];
94 }
77 if (!name.empty() && ignore.match(name))
78 return;
95
79
96 delete [] buffer;
97}
80 std::ostream &os = *dprintf_stream;
98
81
82 string fmt = "";
83 CPrintfArgsList args(VARARGS_ALLARGS);
99
84
100void
101Log::append(Record *rec)
102{
103 // dump record to output stream if there's one open
104 if (dprintf_stream != NULL) {
105 rec->dump(*dprintf_stream);
106 } else {
107 rec->dump(cout);
85 if (!name.empty()) {
86 fmt = "%s: " + fmt;
87 args.push_front(name);
108 }
109
88 }
89
110 // no buffering: justget rid of it now
111 if (buffer == NULL) {
112 delete rec;
113 return;
90 if (when != (Tick)-1) {
91 fmt = "%7d: " + fmt;
92 args.push_front(when);
114 }
115
93 }
94
116 Record *oldRec = *nextRecPtr;
95 fmt += format;
117
96
118 if (oldRec != NULL) {
119 // log has wrapped: overwrite
120 delete oldRec;
121 }
122
123 *nextRecPtr = rec;
124
125 if (++nextRecPtr == wrapRecPtr) {
126 nextRecPtr = &buffer[0];
127 }
97 ccprintf(os, fmt.c_str(), args);
98 os.flush();
128}
129
99}
100
130
131void
101void
132Log::dump(ostream &os)
102dump(Tick when, const std::string &name, const void *d, int len)
133{
103{
134 if (buffer == NULL) {
104 if (!name.empty() && ignore.match(name))
135 return;
105 return;
136 }
137
106
138 Record **bufPtr = nextRecPtr;
107 std::ostream &os = *dprintf_stream;
139
108
140 if (*bufPtr == NULL) {
141 // next record slot is empty: log must not be full yet.
142 // start dumping from beginning of buffer
143 bufPtr = buffer;
144 }
145
146 do {
147 Record *rec = *bufPtr;
148
149 rec->dump(os);
150
151 if (++bufPtr == wrapRecPtr) {
152 bufPtr = &buffer[0];
153 }
154 } while (bufPtr != nextRecPtr);
155}
156
157PrintfRecord::~PrintfRecord()
158{}
159
160void
161PrintfRecord::dump(ostream &os)
162{
163 string fmt = "";
109 string fmt = "";
110 CPrintfArgsList args;
164
165 if (!name.empty()) {
166 fmt = "%s: " + fmt;
167 args.push_front(name);
168 }
169
111
112 if (!name.empty()) {
113 fmt = "%s: " + fmt;
114 args.push_front(name);
115 }
116
170 if (cycle != (Tick)-1) {
117 if (when != (Tick)-1) {
171 fmt = "%7d: " + fmt;
118 fmt = "%7d: " + fmt;
172 args.push_front(cycle);
119 args.push_front(when);
173 }
174
120 }
121
175 fmt += format;
176
177 ccprintf(os, fmt.c_str(), args);
178 os.flush();
179}
180
181DataRecord::DataRecord(Tick _cycle, const string &_name,
182 const void *_data, int _len)
183 : Record(_cycle), name(_name), len(_len)
184{
185 data = new uint8_t[len];
186 memcpy(data, _data, len);
187}
188
189DataRecord::~DataRecord()
190{
191 delete [] data;
192}
193
194void
195DataRecord::dump(ostream &os)
196{
122 const char *data = static_cast<const char *>(d);
197 int c, i, j;
123 int c, i, j;
198
199 for (i = 0; i < len; i += 16) {
124 for (i = 0; i < len; i += 16) {
200 ccprintf(os, "%d: %s: %08x ", cycle, name, i);
125 ccprintf(os, fmt, args);
126 ccprintf(os, "%08x ", i);
201 c = len - i;
202 if (c > 16) c = 16;
203
204 for (j = 0; j < c; j++) {
205 ccprintf(os, "%02x ", data[i + j] & 0xff);
206 if ((j & 0xf) == 7 && j > 0)
207 ccprintf(os, " ");
208 }
209
210 for (; j < 16; j++)
211 ccprintf(os, " ");
212 ccprintf(os, " ");
213
214 for (j = 0; j < c; j++) {
215 int ch = data[i + j] & 0x7f;
127 c = len - i;
128 if (c > 16) c = 16;
129
130 for (j = 0; j < c; j++) {
131 ccprintf(os, "%02x ", data[i + j] & 0xff);
132 if ((j & 0xf) == 7 && j > 0)
133 ccprintf(os, " ");
134 }
135
136 for (; j < 16; j++)
137 ccprintf(os, " ");
138 ccprintf(os, " ");
139
140 for (j = 0; j < c; j++) {
141 int ch = data[i + j] & 0x7f;
216 ccprintf(os,
217 "%c", (char)(isprint(ch) ? ch : ' '));
142 ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
218 }
219
220 ccprintf(os, "\n");
221
222 if (c < 16)
223 break;
224 }
225}
143 }
144
145 ccprintf(os, "\n");
146
147 if (c < 16)
148 break;
149 }
150}
226} // namespace Trace
151
152bool
153changeFlag(const char *s, bool value)
154{
155 using namespace Trace;
156 std::string str(s);
157
158 for (int i = 0; i < numFlagStrings; ++i) {
159 if (str != flagStrings[i])
160 continue;
161
162 if (i < NumFlags) {
163 flags[i] = value;
164 } else {
165 i -= NumFlags;
166
167 const Flags *flagVec = compoundFlags[i];
168 for (int j = 0; flagVec[j] != -1; ++j) {
169 if (flagVec[j] < NumFlags)
170 flags[flagVec[j]] = value;
171 }
172 }
173
174 return true;
175 }
176
177 // the flag was not found.
178 return false;
179}
180
181void
182dumpStatus()
183{
184 using namespace Trace;
185 for (int i = 0; i < numFlagStrings; ++i) {
186 if (flags[i])
187 cprintf("%s\n", flagStrings[i]);
188 }
189}
190
191/* namespace Trace */ }
192
193
194// add a set of functions that can easily be invoked from gdb
195extern "C" {
196 void
197 setTraceFlag(const char *string)
198 {
199 Trace::changeFlag(string, true);
200 }
201
202 void
203 clearTraceFlag(const char *string)
204 {
205 Trace::changeFlag(string, false);
206 }
207
208 void
209 dumpTraceStatus()
210 {
211 Trace::dumpStatus();
212 }
213/* extern "C" */ }