gem5  v22.1.0.0
sc_report_handler.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <fstream>
29 #include <map>
30 #include <sstream>
31 #include <string>
32 
33 #include "base/cprintf.hh"
34 #include "systemc/core/process.hh"
39 #include "systemc/utils/report.hh"
40 
41 namespace sc_core
42 {
43 
44 namespace
45 {
46 
47 std::unique_ptr<std::string> logFileName;
48 std::unique_ptr<std::ofstream> logFile;
49 
50 } // anonymous namespace
51 
52 void
53 sc_report_handler::report(sc_severity severity, const char *msg_type,
54  const char *msg, const char *file, int line)
55 {
56  report(severity, msg_type, msg, SC_MEDIUM, file, line);
57 }
58 
59 void
60 sc_report_handler::report(sc_severity severity, const char *msg_type,
61  const char *msg, int verbosity, const char *file,
62  int line)
63 {
64  if (!msg_type)
65  msg_type = SC_ID_UNKNOWN_ERROR_;
66 
67  if (severity == SC_INFO && verbosity > sc_gem5::reportVerbosityLevel)
68  return;
69 
72 
73  sevInfo.count++;
74  msgInfo.count++;
75  msgInfo.sevCounts[severity]++;
76 
77  sc_actions actions = SC_UNSPECIFIED;
78  if (msgInfo.sevActions[severity] != SC_UNSPECIFIED)
79  actions = msgInfo.sevActions[severity];
80  else if (msgInfo.actions != SC_UNSPECIFIED)
81  actions = msgInfo.actions;
82  else if (sevInfo.actions != SC_UNSPECIFIED)
83  actions = sevInfo.actions;
84 
87 
88  msgInfo.checkLimits(severity, actions);
89  sevInfo.checkLimit(actions);
90 
92  sc_report report(severity, msg_type, msg, verbosity, file, line,
94  current ? current->name() : nullptr, msgInfo.id);
95 
96  if (actions & SC_CACHE_REPORT) {
97  if (current) {
98  current->lastReport(&report);
99  } else {
101  std::unique_ptr<sc_report>(new sc_report(report));
102  }
103  }
104 
106 }
107 
108 void
109 sc_report_handler::report(sc_severity severity, int id, const char *msg,
110  const char *file, int line)
111 {
112  std::string &msg_type = sc_gem5::reportIdToMsgMap()[id];
113 
114  if (sc_gem5::reportWarningsAsErrors && severity == SC_WARNING)
115  severity = SC_ERROR;
116 
117  report(severity, msg_type.c_str(), msg, file, line);
118 }
119 
122 {
124  sc_actions previous = info.actions;
125  info.actions = actions;
126  return previous;
127 }
128 
130 sc_report_handler::set_actions(const char *msg_type, sc_actions actions)
131 {
132  if (!msg_type)
133  msg_type = SC_ID_UNKNOWN_ERROR_;
134 
136  sc_actions previous = info.actions;
137  info.actions = actions;
138  return previous;
139 }
140 
143  const char *msg_type, sc_severity severity, sc_actions actions)
144 {
145  if (!msg_type)
146  msg_type = SC_ID_UNKNOWN_ERROR_;
147 
149  sc_actions previous = info.sevActions[severity];
150  info.sevActions[severity] = actions;
151  return previous;
152 }
153 
154 int
156 {
158  int previous = info.limit;
159  info.limit = limit;
160  return previous;
161 }
162 
163 int
164 sc_report_handler::stop_after(const char *msg_type, int limit)
165 {
166  if (!msg_type)
167  msg_type = SC_ID_UNKNOWN_ERROR_;
168 
170  int previous = info.limit;
171  info.limit = limit;
172  return previous;
173 }
174 
175 int
177  const char *msg_type, sc_severity severity, int limit)
178 {
179  if (!msg_type)
180  msg_type = SC_ID_UNKNOWN_ERROR_;
181 
183  int previous = info.sevLimits[severity];
184  info.sevLimits[severity] = limit;
185  return previous;
186 }
187 
188 int
190 {
191  return sc_gem5::reportSevInfos[severity].count;
192 }
193 
194 int
195 sc_report_handler::get_count(const char *msg_type)
196 {
197  if (!msg_type)
198  msg_type = SC_ID_UNKNOWN_ERROR_;
199 
200  return sc_gem5::reportMsgInfoMap()[msg_type].count;
201 }
202 
203 int
204 sc_report_handler::get_count(const char *msg_type, sc_severity severity)
205 {
206  if (!msg_type)
207  msg_type = SC_ID_UNKNOWN_ERROR_;
208 
209  return sc_gem5::reportMsgInfoMap()[msg_type].sevCounts[severity];
210 }
211 
212 int
214 {
215  int previous = sc_gem5::reportVerbosityLevel;
217  return previous;
218 }
219 
220 int
222 {
224 }
225 
226 
229 {
232  return previous;
233 }
234 
237 {
238  return suppress(SC_UNSPECIFIED);
239 }
240 
243 {
246  return previous;
247 }
248 
251 {
252  return force(SC_UNSPECIFIED);
253 }
254 
255 
258 {
260  sc_gem5::reportCatchActions = actions;
261  return previous;
262 }
263 
266 {
268 }
269 
270 
271 void
273 {
275 }
276 
277 void
279  const sc_report &report, const sc_actions &actions)
280 {
281  if (actions & SC_DISPLAY)
283 
284  if ((actions & SC_LOG) && logFile) {
285  gem5::ccprintf(*logFile, "%s: %s\n", report.get_time().to_string(),
287  }
288  if (actions & SC_STOP) {
289  sc_stop_here(report.get_msg_type(), report.get_severity());
290  sc_stop();
291  }
292  if (actions & SC_INTERRUPT)
293  sc_interrupt_here(report.get_msg_type(), report.get_severity());
294  if (actions & SC_ABORT)
295  sc_abort();
296  if (actions & SC_THROW) {
298  if (current)
299  current->isUnwinding(false);
300  throw report;
301  }
302 }
303 
306 {
307  static sc_actions maxAction = SC_ABORT;
308  maxAction = maxAction << 1;
309  return maxAction;
310 }
311 
312 sc_report *
314 {
316  if (current)
317  return current->lastReport();
319 }
320 
321 void
323 {
325  if (current) {
326  current->lastReport(nullptr);
327  } else {
329  }
330 }
331 
332 bool
334 {
335  if (!new_name) {
336  logFile = nullptr;
337  logFileName = nullptr;
338  return false;
339  } else {
340  if (logFileName)
341  return false;
342  logFileName = std::unique_ptr<std::string>(new std::string(new_name));
343  logFile = std::unique_ptr<std::ofstream>(new std::ofstream(new_name));
344  return true;
345  }
346 }
347 
348 const char *
350 {
351  if (!logFileName)
352  return nullptr;
353  else
354  return logFileName->c_str();
355 }
356 
357 void
358 sc_interrupt_here(const char *msg_type, sc_severity)
359 {
360  // Purposefully empty, for setting breakpoints supposedly.
361 }
362 
363 void
364 sc_stop_here(const char *msg_type, sc_severity)
365 {
366  // Purposefully empty, for setting breakpoints supposedly.
367 }
368 
369 const std::string
371 {
372  std::ostringstream str;
373 
374  const char *sevName = sc_gem5::reportSeverityNames[report.get_severity()];
375  int id = report.get_id();
376 
377  str << sevName << ": ";
378  if (id >= 0) {
379  gem5::ccprintf(str, "(%c%d) ", sevName[0], id);
380  }
381  str << report.get_msg_type();
382 
383  const char *msg = report.get_msg();
384  if (msg && msg[0])
385  str << ": " << msg;
386 
387  if (report.get_severity() > SC_INFO) {
388  gem5::ccprintf(str, "\nIn file: %s:%d", report.get_file_name(),
389  report.get_line_number());
390 
392  const char *name = report.get_process_name();
393  if (current && sc_is_running() && name) {
394  gem5::ccprintf(str, "\nIn process: %s @ %s", name,
395  report.get_time().to_string());
396  }
397  }
398 
399  return str.str();
400 }
401 
402 bool
404 {
405  if (logFile) {
406  logFile = nullptr;
407  logFileName = nullptr;
408  return false;
409  }
410  return true;
411 }
412 
413 } // namespace sc_core
const char * name() const
Definition: sc_object.cc:44
static sc_report * get_cached_report()
static sc_actions suppress()
static sc_actions set_actions(sc_severity, sc_actions=SC_UNSPECIFIED)
static int get_count(sc_severity)
static bool set_log_file_name(const char *)
static sc_actions get_new_action_id()
static void default_handler(const sc_report &, const sc_actions &)
static sc_actions get_catch_actions()
static void report(sc_severity, const char *msg_type, const char *msg, const char *file, int line)
static const char * get_log_file_name()
static int stop_after(sc_severity, int limit=-1)
static sc_actions set_catch_actions(sc_actions)
static void set_handler(sc_report_handler_proc)
const char * get_process_name() const
Definition: sc_report.hh:75
const char * get_msg_type() const
Definition: sc_report.hh:68
int get_line_number() const
Definition: sc_report.hh:72
int get_id() const
Definition: sc_report.hh:87
const char * get_file_name() const
Definition: sc_report.hh:71
const sc_time & get_time() const
Definition: sc_report.hh:74
sc_severity get_severity() const
Definition: sc_report.hh:67
const char * get_msg() const
Definition: sc_report.hh:69
static sc_time from_value(sc_dt::uint64)
Definition: sc_time.cc:210
const std::string to_string() const
Definition: sc_time.cc:132
bool isUnwinding() const
Definition: process.hh:69
void lastReport(::sc_core::sc_report *report)
Definition: process.cc:366
Process * current()
Definition: scheduler.hh:185
Bitfield< 33 > id
Definition: misc_types.hh:257
BitfieldType< SegDescriptorLimit > limit
Definition: misc.hh:931
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:155
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
void sc_stop()
Definition: sc_main.cc:103
void sc_abort()
Definition: sc_report.cc:178
bool sc_report_close_default_log()
@ SC_MEDIUM
Definition: sc_report.hh:52
void sc_interrupt_here(const char *msg_type, sc_severity)
bool sc_is_running()
Definition: sc_main.cc:141
unsigned sc_actions
const std::string sc_report_compose_message(const sc_report &)
@ SC_ERROR
Definition: sc_report.hh:43
@ SC_INFO
Definition: sc_report.hh:41
@ SC_WARNING
Definition: sc_report.hh:42
void sc_stop_here(const char *msg_type, sc_severity)
const char SC_ID_UNKNOWN_ERROR_[]
Definition: messages.cc:35
void(* sc_report_handler_proc)(const sc_report &, const sc_actions &)
int reportVerbosityLevel
Definition: report.cc:62
sc_core::sc_report_handler_proc reportHandlerProc
Definition: report.cc:68
sc_core::sc_actions reportSuppressedActions
Definition: report.cc:64
std::map< std::string, ReportMsgInfo > & reportMsgInfoMap()
Definition: report.cc:49
sc_core::sc_actions reportForcedActions
Definition: report.cc:65
std::map< int, std::string > & reportIdToMsgMap()
Definition: report.cc:56
std::unique_ptr< sc_core::sc_report > globalReportCache
Definition: report.cc:71
bool reportWarningsAsErrors
Definition: report.cc:73
ReportSevInfo reportSevInfos[sc_core::SC_MAX_SEVERITY]
Definition: report.cc:40
const char * reportSeverityNames[]
Definition: report.cc:33
Scheduler scheduler
Definition: scheduler.cc:494
sc_core::sc_actions reportCatchActions
Definition: report.cc:66
sc_core::sc_actions actions
Definition: report.hh:63
int sevCounts[sc_core::SC_MAX_SEVERITY]
Definition: report.hh:68
int sevLimits[sc_core::SC_MAX_SEVERITY]
Definition: report.hh:69
sc_core::sc_actions sevActions[sc_core::SC_MAX_SEVERITY]
Definition: report.hh:67
void checkLimits(sc_core::sc_severity severity, sc_core::sc_actions &actions)
Definition: report.hh:53
void checkLimit(sc_core::sc_actions &actions)
Definition: report.hh:81
sc_core::sc_actions actions
Definition: report.hh:87
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:50 for gem5 by doxygen 1.9.1