scc 2026.07
SystemC components library
configurable_tracer.cpp
1/*******************************************************************************
2 * Copyright 2018-2022 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17#include "configurable_tracer.h"
18#include "traceable.h"
19#include <cstdio>
20#include <fmt/format.h>
21using namespace sc_core;
22using namespace scc;
23
24#define EN_TRACING_STR "enableTracing"
25
26configurable_tracer::configurable_tracer(std::string const&& name, bool enable_tx, bool enable_vcd, sc_core::sc_object* top)
27: tracer(std::move(name), enable_tx ? ENABLE : NONE, enable_vcd ? ENABLE : NONE, top) {}
28
29configurable_tracer::configurable_tracer(std::string const&& name, file_type type, bool enable_vcd, sc_core::sc_object* top)
30: tracer(std::move(name), type, enable_vcd ? ENABLE : NONE, top) {}
31
32configurable_tracer::configurable_tracer(std::string const&& name, file_type tx_type, file_type sig_type, sc_core::sc_object* top)
33: tracer(std::move(name), tx_type, sig_type, top) {}
34
35configurable_tracer::configurable_tracer(std::string const&& name, file_type type, sc_core::sc_trace_file* tf, sc_core::sc_object* top)
36: tracer(std::move(name), type, tf, top) {}
37
39 for(auto ptr : params)
40 delete ptr;
41}
42
43// const std::unordered_set<std::string> traceable_kinds = {};
44void configurable_tracer::descend(const sc_core::sc_object* obj, bool trace) {
45 if(obj == this)
46 return;
47 const std::string kind = obj->kind();
48 if((types_to_trace & trace_types::SIGNALS) == trace_types::SIGNALS && kind == "tlm_signal") {
49 if(trace)
50 obj->trace(trf);
51 return;
52 } else if(kind == "sc_vector") {
53 if(trace)
54 for(auto o : obj->get_child_objects())
55 descend(o, trace);
56 return;
57 } else if(kind == "sc_module") {
58 auto module_name = obj->name();
59 auto trace_enable = get_trace_enabled(obj, default_trace_enable_handle.get_cci_value().get<bool>());
60 if(trace_enable)
61 obj->trace(trf);
62 for(auto o : obj->get_child_objects())
63 descend(o, trace_enable);
64 } else if(kind == "sc_variable") {
65 if(trace && (types_to_trace & trace_types::VARIABLES) == trace_types::VARIABLES)
66 obj->trace(trf);
67 } else if(kind == "sc_signal" || kind == "sc_clock" || kind == "sc_buffer" || kind == "sc_signal_rv") {
68 if(trace && (types_to_trace & trace_types::SIGNALS) == trace_types::SIGNALS)
69 try_trace(trf, obj, types_to_trace);
70 } else if(kind == "sc_in" || kind == "sc_out" || kind == "sc_inout") {
71 if(trace && (types_to_trace & trace_types::PORTS) == trace_types::PORTS)
72 try_trace(trf, obj, types_to_trace);
73 } else if(const auto* tr = dynamic_cast<const scc::traceable*>(obj)) {
74 if(tr->is_trace_enabled())
75 obj->trace(trf);
76 for(auto o : obj->get_child_objects())
77 descend(o, tr->is_trace_enabled());
78 // } else if(trace && traceable_kinds.find(kind)!=traceable_kinds.end()) {
79 // try_trace(trf, obj, types_to_trace);
80 }
81}
82
83auto scc::configurable_tracer::get_trace_enabled(const sc_core::sc_object* obj, bool fall_back) -> bool {
84 auto* attr = obj->get_attribute(EN_TRACING_STR);
85 if(attr != nullptr && dynamic_cast<const sc_core::sc_attribute<bool>*>(attr) != nullptr) {
86 const auto* a = dynamic_cast<const sc_core::sc_attribute<bool>*>(attr);
87 return a->value;
88 } else {
89 std::string hier_name{obj->name()};
90 auto h = cci_broker.get_param_handle(hier_name.append("." EN_TRACING_STR));
91 if(h.is_valid())
92 return h.get_cci_value().get_bool();
93 }
94 return fall_back;
95}
96
97void configurable_tracer::augment_object_hierarchical(sc_core::sc_object* obj, bool trace_enable) {
98 if(dynamic_cast<sc_core::sc_module*>(obj) != nullptr || dynamic_cast<scc::traceable*>(obj) != nullptr) {
99 auto* attr = obj->get_attribute(EN_TRACING_STR);
100 if(attr == nullptr || dynamic_cast<const sc_core::sc_attribute<bool>*>(attr) == nullptr) { // check if we have no sc_attribute
101 std::string hier_name{obj->name()};
102 if(hier_name.substr(0, 3) != "$$$") {
103 hier_name += "." EN_TRACING_STR;
104 auto h = cci_broker.get_param_handle(hier_name);
105 if(!h.is_valid()) // we have no cci_param so create one
106 params.push_back(new cci::cci_param<bool>(hier_name, trace_enable, cci_broker,
107 "Enables the signal tracing of this module", cci::CCI_ABSOLUTE_NAME,
108 cci_broker.get_originator()));
109 else
110 h.set_cci_value(cci::cci_value{default_trace_enable_handle.get_cci_value().get<bool>()});
111 }
112 } else if(auto battr = dynamic_cast<sc_core::sc_attribute<bool>*>(attr)) {
113 battr->value = default_trace_enable_handle.get_cci_value().get<bool>();
114 }
115 for(auto* o : obj->get_child_objects())
116 augment_object_hierarchical(o, trace_enable);
117 }
118}
119
120void configurable_tracer::end_of_elaboration() {
121 add_control();
122 tracer::end_of_elaboration();
123}
configurable_tracer(std::string const &&name, bool enable_tx=true, bool enable_vcd=true, sc_core::sc_object *top=nullptr)
bool get_trace_enabled(const sc_core::sc_object *, bool=false)
check for existence of 'enableTracing' attribute and return value of default otherwise
void augment_object_hierarchical(sc_core::sc_object *, bool)
add the 'enableTracing' attribute to sc_module
void descend(const sc_core::sc_object *, bool trace_all=false) override
depth-first walk thru the design hierarchy and trace signals resp. call trace() function
std::vector< cci::cci_param_untyped * > params
array of created cci parameter
file_type
defines the transaction trace output type
Definition tracer.h:59
SCC SystemC tracing utilities.
Definition fst_trace.cpp:33
SCC TLM utilities.
interface defining a traceable component
Definition traceable.h:32
cci::cci_param_handle default_trace_enable_handle
Definition tracer_base.h:84