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