scc  2022.4.0
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 type, sc_core::sc_trace_file* tf, bool default_enable,
39  sc_core::sc_object* top)
40 : tracer(std::move(name), type, tf, top) {
41  default_trace_enable = default_enable;
42 }
43 
45  for(auto ptr : params)
46  delete ptr;
47 }
48 
49 // const std::unordered_set<std::string> traceable_kinds = {};
50 void configurable_tracer::descend(const sc_core::sc_object* obj, bool trace) {
51  if(obj == this)
52  return;
53  const std::string kind = obj->kind();
54  if((types_to_trace & trace_types::SIGNALS) == trace_types::SIGNALS && kind == "tlm_signal") {
55  if(trace)
56  obj->trace(trf);
57  return;
58  } else if(kind == "sc_vector") {
59  if(trace)
60  for(auto o : obj->get_child_objects())
61  descend(o, trace);
62  return;
63  } else if(kind == "sc_module") {
64  auto trace_enable = get_trace_enabled(obj, default_trace_enable);
65  if(trace_enable)
66  obj->trace(trf);
67  for(auto o : obj->get_child_objects())
68  descend(o, trace_enable);
69  } else if(kind == "sc_variable") {
70  if(trace && (types_to_trace & trace_types::VARIABLES) == trace_types::VARIABLES)
71  obj->trace(trf);
72  } else if(kind == "sc_signal" || kind == "sc_clock" || kind == "sc_buffer" || kind == "sc_signal_rv") {
73  if(trace && (types_to_trace & trace_types::SIGNALS) == trace_types::SIGNALS)
74  try_trace(trf, obj, types_to_trace);
75  } else if(kind == "sc_in" || kind == "sc_out" || kind == "sc_inout") {
76  if(trace && (types_to_trace & trace_types::PORTS) == trace_types::PORTS)
77  try_trace(trf, obj, types_to_trace);
78  } else if(const auto* tr = dynamic_cast<const scc::traceable*>(obj)) {
79  if(tr->is_trace_enabled())
80  obj->trace(trf);
81  for(auto o : obj->get_child_objects())
82  descend(o, tr->is_trace_enabled());
83  // } else if(trace && traceable_kinds.find(kind)!=traceable_kinds.end()) {
84  // try_trace(trf, obj, types_to_trace);
85  }
86 }
87 
88 auto scc::configurable_tracer::get_trace_enabled(const sc_core::sc_object* obj, bool fall_back) -> bool {
89  auto* attr = obj->get_attribute(EN_TRACING_STR);
90  if(attr != nullptr && dynamic_cast<const sc_core::sc_attribute<bool>*>(attr) != nullptr) {
91  const auto* a = dynamic_cast<const sc_core::sc_attribute<bool>*>(attr);
92  return a->value;
93  } else {
94  std::string hier_name{obj->name()};
95  auto h = cci_broker.get_param_handle(hier_name.append("." EN_TRACING_STR));
96  if(h.is_valid())
97  return h.get_cci_value().get_bool();
98  }
99  return fall_back;
100 }
101 
103  if(dynamic_cast<sc_core::sc_module*>(obj) != nullptr || dynamic_cast<scc::traceable*>(obj) != nullptr) {
104  auto* attr = obj->get_attribute(EN_TRACING_STR);
105  if(attr == nullptr || dynamic_cast<const sc_core::sc_attribute<bool>*>(attr) == nullptr) { // check if we have no sc_attribute
106  std::string hier_name{obj->name()};
107  if(hier_name.substr(0, 11) != "scc_tracer") {
108  hier_name += "." EN_TRACING_STR;
109  auto h = cci_broker.get_param_handle(hier_name);
110  if(!h.is_valid()) // we have no cci_param so create one
111  params.push_back(new cci::cci_param<bool>(hier_name, default_trace_enable, cci_broker, "", cci::CCI_ABSOLUTE_NAME,
112  cci_broker.get_originator()));
113  else
114  h.set_cci_value(cci::cci_value{default_trace_enable});
115  }
116  } else if(auto battr = dynamic_cast<sc_core::sc_attribute<bool>*>(attr)) {
117  battr->value = default_trace_enable;
118  }
119  for(auto* o : obj->get_child_objects())
121  }
122 }
123 
124 void configurable_tracer::end_of_elaboration() {
125  add_control();
126  tracer::end_of_elaboration();
127 }
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 SystemC utilities.