scc 2025.09
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
20using namespace sc_core;
21using namespace scc;
22
23#define EN_TRACING_STR "enableTracing"
24
25configurable_tracer::configurable_tracer(std::string const&& name, bool enable_tx, bool enable_vcd, sc_core::sc_object* top)
26: tracer(std::move(name), enable_tx ? ENABLE : NONE, enable_vcd ? ENABLE : NONE, top) {}
27
28configurable_tracer::configurable_tracer(std::string const&& name, file_type type, bool enable_vcd, sc_core::sc_object* top)
29: tracer(std::move(name), type, enable_vcd ? ENABLE : NONE, top) {}
30
31configurable_tracer::configurable_tracer(std::string const&& name, file_type tx_type, file_type sig_type, sc_core::sc_object* top)
32: tracer(std::move(name), tx_type, sig_type, top) {}
33
34configurable_tracer::configurable_tracer(std::string const&& name, file_type type, sc_core::sc_trace_file* tf, sc_core::sc_object* top)
35: tracer(std::move(name), type, tf, top) {}
36
38 for(auto ptr : params)
39 delete ptr;
40}
41
42// const std::unordered_set<std::string> traceable_kinds = {};
43void configurable_tracer::descend(const sc_core::sc_object* obj, bool trace) {
44 if(obj == this)
45 return;
46 const std::string kind = obj->kind();
47 if((types_to_trace & trace_types::SIGNALS) == trace_types::SIGNALS && kind == "tlm_signal") {
48 if(trace)
49 obj->trace(trf);
50 return;
51 } else if(kind == "sc_vector") {
52 if(trace)
53 for(auto o : obj->get_child_objects())
54 descend(o, trace);
55 return;
56 } else if(kind == "sc_module") {
57 auto trace_enable = get_trace_enabled(obj, default_trace_enable_handle.get_cci_value().get<bool>());
58 if(trace_enable)
59 obj->trace(trf);
60 for(auto o : obj->get_child_objects())
61 descend(o, trace_enable);
62 } else if(kind == "sc_variable") {
63 if(trace && (types_to_trace & trace_types::VARIABLES) == trace_types::VARIABLES)
64 obj->trace(trf);
65 } else if(kind == "sc_signal" || kind == "sc_clock" || kind == "sc_buffer" || kind == "sc_signal_rv") {
66 if(trace && (types_to_trace & trace_types::SIGNALS) == trace_types::SIGNALS)
67 try_trace(trf, obj, types_to_trace);
68 } else if(kind == "sc_in" || kind == "sc_out" || kind == "sc_inout") {
69 if(trace && (types_to_trace & trace_types::PORTS) == trace_types::PORTS)
70 try_trace(trf, obj, types_to_trace);
71 } else if(const auto* tr = dynamic_cast<const scc::traceable*>(obj)) {
72 if(tr->is_trace_enabled())
73 obj->trace(trf);
74 for(auto o : obj->get_child_objects())
75 descend(o, tr->is_trace_enabled());
76 // } else if(trace && traceable_kinds.find(kind)!=traceable_kinds.end()) {
77 // try_trace(trf, obj, types_to_trace);
78 }
79}
80
81auto scc::configurable_tracer::get_trace_enabled(const sc_core::sc_object* obj, bool fall_back) -> bool {
82 auto* attr = obj->get_attribute(EN_TRACING_STR);
83 if(attr != nullptr && dynamic_cast<const sc_core::sc_attribute<bool>*>(attr) != nullptr) {
84 const auto* a = dynamic_cast<const sc_core::sc_attribute<bool>*>(attr);
85 return a->value;
86 } else {
87 std::string hier_name{obj->name()};
88 auto h = cci_broker.get_param_handle(hier_name.append("." EN_TRACING_STR));
89 if(h.is_valid())
90 return h.get_cci_value().get_bool();
91 }
92 return fall_back;
93}
94
95void configurable_tracer::augment_object_hierarchical(sc_core::sc_object* obj, bool trace_enable) {
96 if(dynamic_cast<sc_core::sc_module*>(obj) != nullptr || dynamic_cast<scc::traceable*>(obj) != nullptr) {
97 auto* attr = obj->get_attribute(EN_TRACING_STR);
98 if(attr == nullptr || dynamic_cast<const sc_core::sc_attribute<bool>*>(attr) == nullptr) { // check if we have no sc_attribute
99 std::string hier_name{obj->name()};
100 if(hier_name.substr(0, 3) != "$$$") {
101 hier_name += "." EN_TRACING_STR;
102 auto h = cci_broker.get_param_handle(hier_name);
103 if(!h.is_valid()) // we have no cci_param so create one
104 params.push_back(new cci::cci_param<bool>(hier_name, trace_enable, cci_broker,
105 "Enables the signal tracing of this module", cci::CCI_ABSOLUTE_NAME,
106 cci_broker.get_originator()));
107 else
108 h.set_cci_value(cci::cci_value{default_trace_enable_handle.get_cci_value().get<bool>()});
109 }
110 } else if(auto battr = dynamic_cast<sc_core::sc_attribute<bool>*>(attr)) {
111 battr->value = default_trace_enable_handle.get_cci_value().get<bool>();
112 }
113 for(auto* o : obj->get_child_objects())
114 augment_object_hierarchical(o, trace_enable);
115 }
116}
117
118void configurable_tracer::end_of_elaboration() {
119 add_control();
120 tracer::end_of_elaboration();
121}
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