sc_report_handler.cc revision 13313:306a97d3b040
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 * Authors: Gabe Black
28 */
29
30#include <fstream>
31#include <map>
32#include <sstream>
33#include <string>
34
35#include "base/logging.hh"
36#include "systemc/core/process.hh"
37#include "systemc/core/scheduler.hh"
38#include "systemc/ext/core/sc_main.hh"
39#include "systemc/ext/utils/sc_report_handler.hh"
40#include "systemc/utils/report.hh"
41
42namespace sc_core
43{
44
45namespace
46{
47
48std::unique_ptr<std::string> logFileName;
49std::unique_ptr<std::ofstream> logFile;
50
51} // anonymous namespace
52
53void
54sc_report_handler::report(sc_severity severity, const char *msg_type,
55                          const char *msg, const char *file, int line)
56{
57    report(severity, msg_type, msg, SC_MEDIUM, file, line);
58}
59
60void
61sc_report_handler::report(sc_severity severity, const char *msg_type,
62                          const char *msg, int verbosity, const char *file,
63                          int line)
64{
65    if (severity == SC_INFO && verbosity > sc_gem5::reportVerbosityLevel)
66        return;
67
68    sc_gem5::ReportSevInfo &sevInfo = sc_gem5::reportSevInfos[severity];
69    sc_gem5::ReportMsgInfo &msgInfo = sc_gem5::reportMsgInfoMap[msg_type];
70
71    sevInfo.count++;
72    msgInfo.count++;
73    msgInfo.sevCounts[severity]++;
74
75    sc_actions actions = SC_UNSPECIFIED;
76    if (msgInfo.sevActions[severity] != SC_UNSPECIFIED)
77        actions = msgInfo.sevActions[severity];
78    else if (msgInfo.actions != SC_UNSPECIFIED)
79        actions = msgInfo.actions;
80    else if (sevInfo.actions != SC_UNSPECIFIED)
81        actions = sevInfo.actions;
82
83    actions &= ~sc_gem5::reportSuppressedActions;
84    actions |= sc_gem5::reportForcedActions;
85
86    msgInfo.checkLimits(severity, actions);
87    sevInfo.checkLimit(actions);
88
89    ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
90    sc_report report(severity, msg_type, msg, verbosity, file, line,
91            sc_time::from_value(::sc_gem5::scheduler.getCurTick()),
92            current ? current->name() : nullptr, msgInfo.id);
93
94    if (actions & SC_CACHE_REPORT) {
95        if (current) {
96            current->lastReport(&report);
97        } else {
98            sc_gem5::globalReportCache =
99                std::unique_ptr<sc_report>(new sc_report(report));
100        }
101    }
102
103    sc_gem5::reportHandlerProc(report, actions);
104}
105
106void
107sc_report_handler::report(sc_severity severity, int id, const char *msg,
108                          const char *file, int line)
109{
110    std::string &msg_type = sc_gem5::reportIdToMsgMap[id];
111    if (msg_type == "")
112        msg_type = "unknown 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
120sc_actions
121sc_report_handler::set_actions(sc_severity severity, sc_actions actions)
122{
123    sc_gem5::ReportSevInfo &info = sc_gem5::reportSevInfos[severity];
124    sc_actions previous = info.actions;
125    info.actions = actions;
126    return previous;
127}
128
129sc_actions
130sc_report_handler::set_actions(const char *msg_type, sc_actions actions)
131{
132    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
133    sc_actions previous = info.actions;
134    info.actions = actions;
135    return previous;
136}
137
138sc_actions
139sc_report_handler::set_actions(
140        const char *msg_type, sc_severity severity, sc_actions actions)
141{
142    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
143    sc_actions previous = info.sevActions[severity];
144    info.sevActions[severity] = actions;
145    return previous;
146}
147
148int
149sc_report_handler::stop_after(sc_severity severity, int limit)
150{
151    sc_gem5::ReportSevInfo &info = sc_gem5::reportSevInfos[severity];
152    int previous = info.limit;
153    info.limit = limit;
154    return previous;
155}
156
157int
158sc_report_handler::stop_after(const char *msg_type, int limit)
159{
160    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
161    int previous = info.limit;
162    info.limit = limit;
163    return previous;
164}
165
166int
167sc_report_handler::stop_after(
168        const char *msg_type, sc_severity severity, int limit)
169{
170    sc_gem5::ReportMsgInfo &info = sc_gem5::reportMsgInfoMap[msg_type];
171    int previous = info.sevLimits[severity];
172    info.sevLimits[severity] = limit;
173    return previous;
174}
175
176int
177sc_report_handler::get_count(sc_severity severity)
178{
179    return sc_gem5::reportSevInfos[severity].count;
180}
181
182int
183sc_report_handler::get_count(const char *msg_type)
184{
185    return sc_gem5::reportMsgInfoMap[msg_type].count;
186}
187
188int
189sc_report_handler::get_count(const char *msg_type, sc_severity severity)
190{
191    return sc_gem5::reportMsgInfoMap[msg_type].sevCounts[severity];
192}
193
194int
195sc_report_handler::set_verbosity_level(int vl)
196{
197    int previous = sc_gem5::reportVerbosityLevel;
198    sc_gem5::reportVerbosityLevel = vl;
199    return previous;
200}
201
202int
203sc_report_handler::get_verbosity_level()
204{
205    return sc_gem5::reportVerbosityLevel;
206}
207
208
209sc_actions
210sc_report_handler::suppress(sc_actions actions)
211{
212    sc_actions previous = sc_gem5::reportSuppressedActions;
213    sc_gem5::reportSuppressedActions = actions;
214    return previous;
215}
216
217sc_actions
218sc_report_handler::suppress()
219{
220    return suppress(SC_UNSPECIFIED);
221}
222
223sc_actions
224sc_report_handler::force(sc_actions actions)
225{
226    sc_actions previous = sc_gem5::reportForcedActions;
227    sc_gem5::reportForcedActions = actions;
228    return previous;
229}
230
231sc_actions
232sc_report_handler::force()
233{
234    return force(SC_UNSPECIFIED);
235}
236
237
238sc_actions
239sc_report_handler::set_catch_actions(sc_actions actions)
240{
241    sc_actions previous = sc_gem5::reportCatchActions;
242    sc_gem5::reportCatchActions = actions;
243    return previous;
244}
245
246sc_actions
247sc_report_handler::get_catch_actions()
248{
249    return sc_gem5::reportCatchActions;
250}
251
252
253void
254sc_report_handler::set_handler(sc_report_handler_proc proc)
255{
256    sc_gem5::reportHandlerProc = proc;
257}
258
259void
260sc_report_handler::default_handler(
261        const sc_report &report, const sc_actions &actions)
262{
263    if (actions & SC_DISPLAY)
264        cprintf("\n%s\n", sc_report_compose_message(report));
265
266    if ((actions & SC_LOG) && logFile) {
267        ccprintf(*logFile, "%s: %s\n", report.get_time().to_string(),
268                 sc_report_compose_message(report));
269    }
270    if (actions & SC_STOP) {
271        sc_stop_here(report.get_msg_type(), report.get_severity());
272        sc_stop();
273    }
274    if (actions & SC_INTERRUPT)
275        sc_interrupt_here(report.get_msg_type(), report.get_severity());
276    if (actions & SC_ABORT)
277        sc_abort();
278    if (actions & SC_THROW) {
279        ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
280        if (current)
281            current->isUnwinding(false);
282        throw report;
283    }
284}
285
286sc_actions
287sc_report_handler::get_new_action_id()
288{
289    static sc_actions maxAction = SC_ABORT;
290    maxAction = maxAction << 1;
291    return maxAction;
292}
293
294sc_report *
295sc_report_handler::get_cached_report()
296{
297    ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
298    if (current)
299        return current->lastReport();
300    return ::sc_gem5::globalReportCache.get();
301}
302
303void
304sc_report_handler::clear_cached_report()
305{
306    ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
307    if (current) {
308        current->lastReport(nullptr);
309    } else {
310        ::sc_gem5::globalReportCache = nullptr;
311    }
312}
313
314bool
315sc_report_handler::set_log_file_name(const char *new_name)
316{
317    if (!new_name) {
318        logFile = nullptr;
319        logFileName = nullptr;
320        return false;
321    } else {
322        if (logFileName)
323            return false;
324        logFileName = std::unique_ptr<std::string>(new std::string(new_name));
325        logFile = std::unique_ptr<std::ofstream>(new std::ofstream(new_name));
326        return true;
327    }
328}
329
330const char *
331sc_report_handler::get_log_file_name()
332{
333    if (!logFileName)
334        return nullptr;
335    else
336        return logFileName->c_str();
337}
338
339void
340sc_interrupt_here(const char *msg_type, sc_severity)
341{
342    // Purposefully empty, for setting breakpoints supposedly.
343}
344
345void
346sc_stop_here(const char *msg_type, sc_severity)
347{
348    // Purposefully empty, for setting breakpoints supposedly.
349}
350
351const std::string
352sc_report_compose_message(const sc_report &report)
353{
354    std::ostringstream str;
355
356    const char *sevName = sc_gem5::reportSeverityNames[report.get_severity()];
357    int id = report.get_id();
358
359    str << sevName << ": ";
360    if (id >= 0) {
361        ccprintf(str, "(%c%d) ", sevName[0], id);
362    }
363    str << report.get_msg_type();
364
365    const char *msg = report.get_msg();
366    if (msg && msg[0])
367        str << ": " << msg;
368
369    if (report.get_severity() > SC_INFO) {
370        ccprintf(str, "\nIn file: %s:%d", report.get_file_name(),
371                 report.get_line_number());
372
373        ::sc_gem5::Process *current = ::sc_gem5::scheduler.current();
374        const char *name = report.get_process_name();
375        if (current && sc_is_running() && name) {
376            ccprintf(str, "\nIn process: %s @ %s", name,
377                    report.get_time().to_string());
378        }
379    }
380
381    return str.str();
382}
383
384bool
385sc_report_close_default_log()
386{
387    if (logFile) {
388        logFile = nullptr;
389        logFileName = nullptr;
390        return false;
391    }
392    return true;
393}
394
395} // namespace sc_core
396