gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  for (auto& handler : sc_gem5::getExtraSystemCReportHandlers()) {
108  handler(report, actions);
109  }
110 }
111 
112 void
113 sc_report_handler::report(sc_severity severity, int id, const char *msg,
114  const char *file, int line)
115 {
116  std::string &msg_type = sc_gem5::reportIdToMsgMap()[id];
117 
118  if (sc_gem5::reportWarningsAsErrors && severity == SC_WARNING)
119  severity = SC_ERROR;
120 
121  report(severity, msg_type.c_str(), msg, file, line);
122 }
123 
126 {
128  sc_actions previous = info.actions;
129  info.actions = actions;
130  return previous;
131 }
132 
134 sc_report_handler::set_actions(const char *msg_type, sc_actions actions)
135 {
136  if (!msg_type)
137  msg_type = SC_ID_UNKNOWN_ERROR_;
138 
140  sc_actions previous = info.actions;
141  info.actions = actions;
142  return previous;
143 }
144 
147  const char *msg_type, sc_severity severity, sc_actions actions)
148 {
149  if (!msg_type)
150  msg_type = SC_ID_UNKNOWN_ERROR_;
151 
153  sc_actions previous = info.sevActions[severity];
154  info.sevActions[severity] = actions;
155  return previous;
156 }
157 
158 int
160 {
162  int previous = info.limit;
163  info.limit = limit;
164  return previous;
165 }
166 
167 int
168 sc_report_handler::stop_after(const char *msg_type, int limit)
169 {
170  if (!msg_type)
171  msg_type = SC_ID_UNKNOWN_ERROR_;
172 
174  int previous = info.limit;
175  info.limit = limit;
176  return previous;
177 }
178 
179 int
181  const char *msg_type, sc_severity severity, int limit)
182 {
183  if (!msg_type)
184  msg_type = SC_ID_UNKNOWN_ERROR_;
185 
187  int previous = info.sevLimits[severity];
188  info.sevLimits[severity] = limit;
189  return previous;
190 }
191 
192 int
194 {
195  return sc_gem5::reportSevInfos[severity].count;
196 }
197 
198 int
199 sc_report_handler::get_count(const char *msg_type)
200 {
201  if (!msg_type)
202  msg_type = SC_ID_UNKNOWN_ERROR_;
203 
204  return sc_gem5::reportMsgInfoMap()[msg_type].count;
205 }
206 
207 int
208 sc_report_handler::get_count(const char *msg_type, sc_severity severity)
209 {
210  if (!msg_type)
211  msg_type = SC_ID_UNKNOWN_ERROR_;
212 
213  return sc_gem5::reportMsgInfoMap()[msg_type].sevCounts[severity];
214 }
215 
216 int
218 {
219  int previous = sc_gem5::reportVerbosityLevel;
221  return previous;
222 }
223 
224 int
226 {
228 }
229 
230 
233 {
236  return previous;
237 }
238 
241 {
242  return suppress(SC_UNSPECIFIED);
243 }
244 
247 {
250  return previous;
251 }
252 
255 {
256  return force(SC_UNSPECIFIED);
257 }
258 
259 
262 {
264  sc_gem5::reportCatchActions = actions;
265  return previous;
266 }
267 
270 {
272 }
273 
274 
275 void
277 {
279 }
280 
281 void
283  const sc_report &report, const sc_actions &actions)
284 {
285  if (actions & SC_DISPLAY)
287 
288  if ((actions & SC_LOG) && logFile) {
289  gem5::ccprintf(*logFile, "%s: %s\n", report.get_time().to_string(),
291  }
292  if (actions & SC_STOP) {
293  sc_stop_here(report.get_msg_type(), report.get_severity());
294  sc_stop();
295  }
296  if (actions & SC_INTERRUPT)
297  sc_interrupt_here(report.get_msg_type(), report.get_severity());
298  if (actions & SC_ABORT)
299  sc_abort();
300  if (actions & SC_THROW) {
302  if (current)
303  current->isUnwinding(false);
304  throw report;
305  }
306 }
307 
310 {
311  static sc_actions maxAction = SC_ABORT;
312  maxAction = maxAction << 1;
313  return maxAction;
314 }
315 
316 sc_report *
318 {
320  if (current)
321  return current->lastReport();
323 }
324 
325 void
327 {
329  if (current) {
330  current->lastReport(nullptr);
331  } else {
333  }
334 }
335 
336 bool
338 {
339  if (!new_name) {
340  logFile = nullptr;
341  logFileName = nullptr;
342  return false;
343  } else {
344  if (logFileName)
345  return false;
346  logFileName = std::unique_ptr<std::string>(new std::string(new_name));
347  logFile = std::unique_ptr<std::ofstream>(new std::ofstream(new_name));
348  return true;
349  }
350 }
351 
352 const char *
354 {
355  if (!logFileName)
356  return nullptr;
357  else
358  return logFileName->c_str();
359 }
360 
361 void
362 sc_interrupt_here(const char *msg_type, sc_severity)
363 {
364  // Purposefully empty, for setting breakpoints supposedly.
365 }
366 
367 void
368 sc_stop_here(const char *msg_type, sc_severity)
369 {
370  // Purposefully empty, for setting breakpoints supposedly.
371 }
372 
373 const std::string
375 {
376  std::ostringstream str;
377 
378  const char *sevName = sc_gem5::reportSeverityNames[report.get_severity()];
379  int id = report.get_id();
380 
381  str << sevName << ": ";
382  if (id >= 0) {
383  gem5::ccprintf(str, "(%c%d) ", sevName[0], id);
384  }
385  str << report.get_msg_type();
386 
387  const char *msg = report.get_msg();
388  if (msg && msg[0])
389  str << ": " << msg;
390 
391  if (report.get_severity() > SC_INFO) {
392  gem5::ccprintf(str, "\nIn file: %s:%d", report.get_file_name(),
393  report.get_line_number());
394 
396  const char *name = report.get_process_name();
397  if (current && sc_is_running() && name) {
398  gem5::ccprintf(str, "\nIn process: %s @ %s", name,
399  report.get_time().to_string());
400  }
401  }
402 
403  return str.str();
404 }
405 
406 bool
408 {
409  if (logFile) {
410  logFile = nullptr;
411  logFileName = nullptr;
412  return false;
413  }
414  return true;
415 }
416 
417 } // namespace sc_core
sc_core::SC_INFO
@ SC_INFO
Definition: sc_report.hh:41
sc_core::sc_report_handler::get_count
static int get_count(sc_severity)
Definition: sc_report_handler.cc:193
sc_core::sc_time::from_value
static sc_time from_value(sc_dt::uint64)
Definition: sc_time.cc:210
messages.hh
sc_core::sc_actions
unsigned sc_actions
Definition: sc_report_handler.hh:39
sc_gem5::ReportSevInfo
Definition: report.hh:75
gem5::cprintf
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:155
sc_core::SC_WARNING
@ SC_WARNING
Definition: sc_report.hh:42
sc_core::sc_severity
sc_severity
Definition: sc_report.hh:39
sc_gem5::reportSeverityNames
const char * reportSeverityNames[]
Definition: report.cc:33
sc_core::sc_report_handler::set_catch_actions
static sc_actions set_catch_actions(sc_actions)
Definition: sc_report_handler.cc:261
sc_core::sc_report_compose_message
const std::string sc_report_compose_message(const sc_report &)
Definition: sc_report_handler.cc:374
sc_core::sc_report_handler::set_actions
static sc_actions set_actions(sc_severity, sc_actions=SC_UNSPECIFIED)
Definition: sc_report_handler.cc:125
sc_core
Definition: messages.cc:31
sc_core::sc_report::get_process_name
const char * get_process_name() const
Definition: sc_report.hh:75
report.hh
gem5::RiscvISA::vl
Bitfield< 57, 41 > vl
Definition: types.hh:63
sc_gem5::reportVerbosityLevel
int reportVerbosityLevel
Definition: report.cc:62
sc_gem5::getExtraSystemCReportHandlers
const std::list< sc_core::sc_report_handler_proc > & getExtraSystemCReportHandlers()
Definition: report.cc:79
sc_core::sc_report_handler::default_handler
static void default_handler(const sc_report &, const sc_actions &)
Definition: sc_report_handler.cc:282
sc_core::sc_report_close_default_log
bool sc_report_close_default_log()
Definition: sc_report_handler.cc:407
sc_core::sc_report::get_time
const sc_time & get_time() const
Definition: sc_report.hh:74
sc_core::sc_report::get_msg
const char * get_msg() const
Definition: sc_report.hh:69
gem5::X86ISA::limit
BitfieldType< SegDescriptorLimit > limit
Definition: misc.hh:931
sc_core::sc_report_handler::force
static sc_actions force()
Definition: sc_report_handler.cc:254
sc_core::sc_report_handler::clear_cached_report
static void clear_cached_report()
Definition: sc_report_handler.cc:326
sc_core::sc_report_handler::suppress
static sc_actions suppress()
Definition: sc_report_handler.cc:240
sc_gem5::ReportMsgInfo::actions
sc_core::sc_actions actions
Definition: report.hh:64
sc_gem5::ReportSevInfo::actions
sc_core::sc_actions actions
Definition: report.hh:88
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
sc_gem5::ReportSevInfo::count
int count
Definition: report.hh:89
sc_core::SC_ID_UNKNOWN_ERROR_
const char SC_ID_UNKNOWN_ERROR_[]
Definition: messages.cc:35
sc_core::sc_stop
void sc_stop()
Definition: sc_main.cc:103
sc_core::sc_report_handler::stop_after
static int stop_after(sc_severity, int limit=-1)
Definition: sc_report_handler.cc:159
sc_gem5::reportSevInfos
ReportSevInfo reportSevInfos[sc_core::SC_MAX_SEVERITY]
Definition: report.cc:40
sc_core::SC_ABORT
@ SC_ABORT
Definition: sc_report_handler.hh:51
sc_core::sc_report_handler::get_catch_actions
static sc_actions get_catch_actions()
Definition: sc_report_handler.cc:269
sc_gem5::reportHandlerProc
sc_core::sc_report_handler_proc reportHandlerProc
Definition: report.cc:68
sc_gem5::Process::lastReport
void lastReport(::sc_core::sc_report *report)
Definition: process.cc:366
sc_gem5::ReportSevInfo::checkLimit
void checkLimit(sc_core::sc_actions &actions)
Definition: report.hh:82
sc_gem5::ReportMsgInfo::count
int count
Definition: report.hh:65
sc_core::SC_STOP
@ SC_STOP
Definition: sc_report_handler.hh:50
sc_main.hh
sc_gem5::ReportMsgInfo::sevLimits
int sevLimits[sc_core::SC_MAX_SEVERITY]
Definition: report.hh:70
sc_gem5::globalReportCache
std::unique_ptr< sc_core::sc_report > globalReportCache
Definition: report.cc:96
sc_core::sc_report_handler::set_verbosity_level
static int set_verbosity_level(int)
Definition: sc_report_handler.cc:217
sc_core::sc_report_handler::report
static void report(sc_severity, const char *msg_type, const char *msg, const char *file, int line)
Definition: sc_report_handler.cc:53
cprintf.hh
sc_core::SC_UNSPECIFIED
@ SC_UNSPECIFIED
Definition: sc_report_handler.hh:43
sc_gem5::ReportMsgInfo::limit
int limit
Definition: report.hh:66
sc_core::sc_report::get_file_name
const char * get_file_name() const
Definition: sc_report.hh:71
sc_core::SC_DISPLAY
@ SC_DISPLAY
Definition: sc_report_handler.hh:47
sc_gem5::reportMsgInfoMap
std::map< std::string, ReportMsgInfo > & reportMsgInfoMap()
Definition: report.cc:49
sc_gem5::ReportMsgInfo::sevCounts
int sevCounts[sc_core::SC_MAX_SEVERITY]
Definition: report.hh:69
sc_core::sc_report::get_severity
sc_severity get_severity() const
Definition: sc_report.hh:67
sc_gem5::Process
Definition: process.hh:62
name
const std::string & name()
Definition: trace.cc:48
sc_gem5::ReportMsgInfo::id
int id
Definition: report.hh:72
sc_core::sc_report
Definition: sc_report.hh:60
process.hh
sc_gem5::reportWarningsAsErrors
bool reportWarningsAsErrors
Definition: report.cc:98
sc_gem5::ReportMsgInfo::sevActions
sc_core::sc_actions sevActions[sc_core::SC_MAX_SEVERITY]
Definition: report.hh:68
sc_core::sc_stop_here
void sc_stop_here(const char *msg_type, sc_severity)
Definition: sc_report_handler.cc:368
sc_gem5::reportForcedActions
sc_core::sc_actions reportForcedActions
Definition: report.cc:65
sc_core::sc_report::get_id
int get_id() const
Definition: sc_report.hh:87
sc_report_handler.hh
sc_gem5::ReportMsgInfo::checkLimits
void checkLimits(sc_core::sc_severity severity, sc_core::sc_actions &actions)
Definition: report.hh:54
sc_gem5::Scheduler::current
Process * current()
Definition: scheduler.hh:185
sc_core::sc_report_handler_proc
void(* sc_report_handler_proc)(const sc_report &, const sc_actions &)
Definition: sc_report_handler.hh:62
sc_core::SC_MEDIUM
@ SC_MEDIUM
Definition: sc_report.hh:52
sc_core::sc_report_handler::get_new_action_id
static sc_actions get_new_action_id()
Definition: sc_report_handler.cc:309
sc_gem5::reportIdToMsgMap
std::map< int, std::string > & reportIdToMsgMap()
Definition: report.cc:56
sc_core::sc_report_handler::set_handler
static void set_handler(sc_report_handler_proc)
Definition: sc_report_handler.cc:276
sc_core::sc_object::name
const char * name() const
Definition: sc_object.cc:44
sc_core::sc_report_handler::get_verbosity_level
static int get_verbosity_level()
Definition: sc_report_handler.cc:225
sc_core::SC_CACHE_REPORT
@ SC_CACHE_REPORT
Definition: sc_report_handler.hh:48
sc_core::sc_interrupt_here
void sc_interrupt_here(const char *msg_type, sc_severity)
Definition: sc_report_handler.cc:362
sc_gem5::Process::isUnwinding
bool isUnwinding() const
Definition: process.hh:69
sc_core::sc_report::get_line_number
int get_line_number() const
Definition: sc_report.hh:72
sc_core::SC_INTERRUPT
@ SC_INTERRUPT
Definition: sc_report_handler.hh:49
sc_core::sc_abort
void sc_abort()
Definition: sc_report.cc:178
gem5::ArmISA::id
Bitfield< 33 > id
Definition: misc_types.hh:305
sc_gem5::reportSuppressedActions
sc_core::sc_actions reportSuppressedActions
Definition: report.cc:64
sc_core::sc_report::get_msg_type
const char * get_msg_type() const
Definition: sc_report.hh:68
sc_core::sc_report_handler::get_cached_report
static sc_report * get_cached_report()
Definition: sc_report_handler.cc:317
sc_gem5
Definition: sc_clock.cc:41
sc_gem5::ReportMsgInfo
Definition: report.hh:44
sc_gem5::reportCatchActions
sc_core::sc_actions reportCatchActions
Definition: report.cc:66
sc_gem5::scheduler
Scheduler scheduler
Definition: scheduler.cc:494
sc_core::SC_ERROR
@ SC_ERROR
Definition: sc_report.hh:43
sc_core::SC_THROW
@ SC_THROW
Definition: sc_report_handler.hh:45
sc_core::sc_report_handler::get_log_file_name
static const char * get_log_file_name()
Definition: sc_report_handler.cc:353
sc_core::SC_LOG
@ SC_LOG
Definition: sc_report_handler.hh:46
sc_gem5::ReportSevInfo::limit
int limit
Definition: report.hh:90
sc_core::sc_report_handler::set_log_file_name
static bool set_log_file_name(const char *)
Definition: sc_report_handler.cc:337
scheduler.hh
sc_core::sc_time::to_string
const std::string to_string() const
Definition: sc_time.cc:132
sc_core::sc_is_running
bool sc_is_running()
Definition: sc_main.cc:141

Generated on Sun Jul 30 2023 01:57:03 for gem5 by doxygen 1.8.17