tracetools.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016, 2017 - for information on the respective
2 // copyright owner see the NOTICE file and/or the repository
3 // https://github.com/bosch-robotics-cr/tracetools.git
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 
18 #include <tracetools/tracetools.h>
19 #ifdef WITH_LTTNG
20 #include "tp_call.h"
21 #endif
22 #include <execinfo.h>
23 #include <sstream>
24 #include <sys/prctl.h>
25 
26 namespace ros {
27 namespace trace {
28 
29 bool compile_status() throw () {
30 #ifdef WITH_LTTNG
31  return true;
32 #else
33  return false;
34 #endif
35 }
36 void task_init(const char* name, const char* owner) {
37 #ifdef WITH_LTTNG
38  std::ostringstream oss;
39  oss << name << "_";
40  if (owner != NULL && strlen(owner) > 0) {
41  oss << owner;
42  } else {
43  char thread_name[256];
44  prctl(PR_GET_NAME, thread_name, NULL, NULL, NULL);
45  oss << thread_name;
46  }
47  std::string fqn(oss.str());
48  tracepoint(roscpp, task_start, fqn.c_str());
49 
50  // check if we need to truncate, because name is usually not unique
51  // and we need the procname to disambiguate
52  if (strlen(name) > 10) {
53  oss.str(fqn.substr(0, 8));
54  // let prctl truncate the rest
55  oss << "_" << fqn.substr(strlen(name));
56  fqn = oss.str();
57  }
58  prctl(PR_SET_NAME, fqn.c_str(), NULL, NULL, NULL);
59 #endif
60 }
61 void node_init(const char* node_name, unsigned int roscpp_version) {
62 #ifdef WITH_LTTNG
63  tracepoint(roscpp, init_node, node_name, roscpp_version);
64  prctl(PR_SET_NAME, node_name, NULL, NULL, NULL);
65 #endif
66 }
67 void call_start(const void* ptr_ref, const void* data,
68  const uint64_t trace_id) {
69 #ifdef WITH_LTTNG
70  tracepoint(roscpp, callback_start, ptr_ref, data, trace_id);
71 #endif
72 }
73 void call_end(const void* ptr_ref, const void* data, const uint64_t trace_id) {
74 #ifdef WITH_LTTNG
75  tracepoint(roscpp, callback_end, ptr_ref, data, trace_id);
76 #endif
77 }
78 
79 void message_processed(const char* message_name, const void* callback_ref,
80  const uint32_t receipt_time_sec, const uint32_t receipt_time_nsec) {
81 #ifdef WITH_LTTNG
82  tracepoint(roscpp, message_processed, message_name, callback_ref,
83  receipt_time_sec, receipt_time_nsec);
84 #endif
85 }
86 
87 
88 void fn_name_info(const void* const_ptr, const void* ptr) {
89 #ifdef WITH_LTTNG
90  void* func_ptr = const_cast<void*>(const_ptr);
91  char** symbols = backtrace_symbols(&func_ptr, 1);
92  // check if we just got a pointer
93  if (symbols[0][0] == '[') {
94  tracepoint(roscpp, ptr_name_info, impl::get_backtrace().c_str(), ptr);
95  } else {
96  tracepoint(roscpp, ptr_name_info, symbols[0], ptr);
97  }
98  free(symbols);
99 #endif
100 }
101 /*void cb_call(const void* ptr_ref, const void* data, const
102  uint64_t trace_id) {
103  tracepoint(roscpp, ptr_call, ptr_ref, data, trace_id);
104  }*/
105 
106 
107 void timer_added(const void* fun_ptr, const char* type_info, int period_sec,
108  int period_nsec) {
109 #ifdef WITH_LTTNG
110  tracepoint(roscpp, timer_added, fun_ptr,
111  impl::get_symbol(const_cast<void*>(fun_ptr)).c_str(), type_info,
112  period_sec, period_nsec);
113 #endif
114 }
115 
116 void timer_scheduled(const void* callback_ref, const void* timer_ref) {
117 #ifdef WITH_LTTNG
118  tracepoint(roscpp, timer_scheduled, callback_ref, timer_ref);
119 #endif
120 }
121 
122 void time_sleep(const void* callback_ref, int sleep_sec, int sleep_nsec) {
123 #ifdef WITH_LTTNG
124  tracepoint(roscpp, time_sleep, callback_ref, sleep_sec, sleep_nsec);
125 #endif
126 }
127 
128 void link_step(const char* element_name, const void* caller_ref,
129  const void* in_data_ref, const void* out_data_ref,
130  const uint64_t trace_id) {
131 #ifdef WITH_LTTNG
132 #ifdef NO_LINK_BACKTRACES
133  tracepoint(roscpp, trace_link, element_name, user_name,
134  in_data_ref, out_data_ref, trace_id, NULL);
135 #else
136  tracepoint(roscpp, trace_link, element_name, typeid(caller_ref).name(),
137  caller_ref, in_data_ref, out_data_ref, trace_id,
138  impl::get_backtrace().c_str());
139 #endif
140 #endif
141 }
142 
143 void new_connection(const char* local_hostport_arg,
144  const char* remote_hostport_arg, const void* channel_ref_arg,
145  const char* channel_type_arg, const char* name_arg,
146  const char* data_type_arg) {
147 #ifdef WITH_LTTNG
148  tracepoint(roscpp, new_connection, local_hostport_arg, remote_hostport_arg,
149  channel_ref_arg, channel_type_arg, name_arg, data_type_arg);
150 #endif
151 }
152 void publisher_link_handle_message(const void* channel_ref_arg,
153  const void* buffer_ref_arg) {
154 #ifdef WITH_LTTNG
155  tracepoint(roscpp, publisher_link_handle_message, channel_ref_arg,
156  buffer_ref_arg);
157 #endif
158 }
159 
160 void publisher_message_queued(const char* topic_arg,
161  const void* buffer_ref_arg) {
162 #ifdef WITH_LTTNG
163  tracepoint(roscpp, publisher_message_queued, topic_arg, buffer_ref_arg);
164 #endif
165 }
166 void publisher_message_queued(const std::string& topic_arg,
167  const void* buffer_ref_arg) {
168 #ifdef WITH_LTTNG
169  tracepoint(roscpp, publisher_message_queued, topic_arg.c_str(),
170  buffer_ref_arg);
171 #endif
172 }
173 void subscriber_link_message_write(const void* message_ref_arg,
174  const void* channel_ref_arg) {
175 #ifdef WITH_LTTNG
176  tracepoint(roscpp, subscriber_link_message_write, message_ref_arg,
177  channel_ref_arg);
178 #endif
179 }
180 void subscriber_link_message_dropped(const void* message_ref_arg) {
181 #ifdef WITH_LTTNG
182  tracepoint(roscpp, subscriber_link_message_dropped, message_ref_arg);
183 #endif
184 }
185 
186 void subscription_message_queued(const char* topic_arg,
187  const void* buffer_ref_arg, const void* queue_ref_arg,
188  const void* callback_ref_arg, const void* message_ref_arg,
189  int receipt_time_sec_arg, int receipt_time_nsec_arg) {
190 #ifdef WITH_LTTNG
191  tracepoint(roscpp, subscription_message_queued, topic_arg, buffer_ref_arg,
192  queue_ref_arg, callback_ref_arg, message_ref_arg,
193  receipt_time_sec_arg, receipt_time_nsec_arg);
194 #endif
195 }
196 void subscription_message_dropped(const char* topic_arg,
197  const void* buffer_ref_arg,
198  const void* queue_ref_arg,
199  const void* callback_ref_arg,
200  const void* message_ref_arg,
201  int receipt_time_sec_arg,
202  int receipt_time_nsec_arg)
203 {
204 #ifdef WITH_LTTNG
205  tracepoint(roscpp, subscription_message_dropped, topic_arg, buffer_ref_arg,
206  queue_ref_arg, callback_ref_arg, message_ref_arg,
207  receipt_time_sec_arg, receipt_time_nsec_arg);
208 #endif
209 }
210 void subscriber_callback_added(const void* queue_ref_arg,
211  const void* callback_ref_arg, const char* type_info_arg,
212  const char* data_type_arg, const char* source_name_arg,
213  int queue_size_arg) {
214 #ifdef WITH_LTTNG
215  tracepoint(roscpp, subscriber_callback_added, queue_ref_arg,
216  callback_ref_arg, type_info_arg, data_type_arg, source_name_arg,
217  queue_size_arg);
218 #endif
219 }
220 
221 void subscriber_call_start(const std::string& topic, const void* queue_ref,
222  const void* callback_ref, const void* message_ref, int receipt_time_sec,
223  int receipt_time_nsec) {
224 #ifdef WITH_LTTNG
225  tracepoint(roscpp, subscriber_callback_start, topic.c_str(), 0, queue_ref,
226  callback_ref, message_ref, receipt_time_sec, receipt_time_nsec);
227 #endif
228 }
229 void subscriber_call_end(const std::string& topic, const void* queue_ref,
230  const void* callback_ref, const void* message_ref, int receipt_time_sec,
231  int receipt_time_nsec) {
232 #ifdef WITH_LTTNG
233  tracepoint(roscpp, subscriber_callback_end, topic.c_str(), 0, queue_ref,
234  callback_ref, message_ref, receipt_time_sec, receipt_time_nsec);
235 #endif
236 }
237 
238 void queue_delay(const char* queue_name, const void* ptr_ref, const void* data,
239  const uint32_t entry_time_sec, const uint32_t entry_time_nsec) {
240 #ifdef WITH_LTTNG
241  tracepoint(roscpp, queue_delay, queue_name, ptr_ref, data, entry_time_sec,
242  entry_time_nsec);
243 #endif
244 }
245 
246 namespace impl {
247 std::string get_backtrace(int index) {
248 #ifdef WITH_LTTNG
249  const int bufsize = 50;
250  void* bt_buffer[bufsize];
251  int size = backtrace(bt_buffer, bufsize);
252  char** symbols = backtrace_symbols(bt_buffer, size);
253 
254  std::ostringstream oss;
255  if (index < size) {
256  if (index < 0) {
257  // add full backtrace (excepting ourselves and our immediate
258  // caller)
259  for (int i = 2; i < size; ++i) {
260  oss << symbols[i] << "|";
261  }
262  } else {
263  oss << symbols[index];
264  }
265  } else
266  oss << "Invalid index " << index << " requested, only have " << size
267  << " backtrace entries";
268 
269  free(symbols);
270 
271  return oss.str();
272 #else
273  return "";
274 #endif
275 }
276 
277 std::string get_symbol(void* funptr) {
278 #ifdef WITH_LTTNG
279  char** symbols = backtrace_symbols(&funptr, 1);
280  std::string result(symbols[0]);
281  free(symbols);
282  return result;
283 #else
284  return "";
285 #endif
286 }
287 std::string getCallbackInfo(const void* func_ptr, const char* name) {
288 #ifdef WITH_LTTNG
289  void* funptr = const_cast<void*>(func_ptr);
290  char** symbols = backtrace_symbols(&funptr, 1);
291  std::string result(symbols[0]);
292  free(symbols);
293  return name + std::string(" ") + result;
294 #else
295  return "";
296 #endif
297 }
298 }
299 
300 
301 }
302 }
void subscriber_call_end(const std::string &topic, const void *queue_ref, const void *callback_ref, const void *message_ref, int receipt_time_sec, int receipt_time_nsec)
Marks the end of the call, same arguments as above.
Definition: tracetools.cpp:229
std::string getCallbackInfo(const void *func_ptr, const char *name)
try to get a name for the function inside the CallbackInterfacePtr
Definition: tracetools.cpp:287
void subscriber_callback_added(const void *queue_ref_arg, const void *callback_ref_arg, const char *type_info_arg, const char *data_type_arg, const char *source_name_arg, int queue_size_arg)
Trace metadata on a new subscription callback.
Definition: tracetools.cpp:210
std::string get_backtrace(int index=-1)
Definition: tracetools.cpp:247
void timer_added(const void *fun_ptr, const char *type_info, int period_sec, int period_nsec)
Definition: tracetools.cpp:107
void node_init(const char *node_name, unsigned int roscpp_version)
also set&#39;s procname, but be aware that&#39;s limited to 16 chars
Definition: tracetools.cpp:61
void fn_name_info(const void *fun_ptr, const void *ref)
Emit tracing information linking the function ptr&#39;s name to the given reference pointer.
Definition: tracetools.cpp:88
std::string get_symbol(void *funptr)
Definition: tracetools.cpp:277
void publisher_link_handle_message(const void *channel_ref_arg, const void *buffer_ref_arg)
Trace metadata on creation of a publisher link (incoming topic connection)
Definition: tracetools.cpp:152
void publisher_message_queued(const char *topic_arg, const void *buffer_ref_arg)
Trace a message being queue for publishing.
Definition: tracetools.cpp:160
void new_connection(const char *local_hostport_arg, const char *remote_hostport_arg, const void *channel_ref_arg, const char *channel_type_arg, const char *name_arg, const char *data_type_arg)
Trace metadata on creation of a new connection.
Definition: tracetools.cpp:143
void subscriber_link_message_dropped(const void *message_ref_arg)
Trac on an incoming message being dropped (queue full, etc.)
Definition: tracetools.cpp:180
void call_end(const void *ptr_ref, const void *data, const uint64_t trace_id)
Trace the end of a user-callback invocation.
Definition: tracetools.cpp:73
void time_sleep(const void *callback_ref, int sleep_sec, int sleep_nsec)
Definition: tracetools.cpp:122
void task_init(const char *task_name, const char *owner=NULL)
emit a tracepoint specifying a name for this thread.
Definition: tracetools.cpp:36
void subscription_message_queued(const char *topic_arg, const void *buffer_ref_arg, const void *queue_ref_arg, const void *callback_ref_arg, const void *message_ref_arg, int receipt_time_sec_arg, int receipt_time_nsec_arg)
Trace on a message having been received and queued.
Definition: tracetools.cpp:186
bool compile_status()
report whether tracing is compiled in
Definition: tracetools.cpp:29
void queue_delay(const char *queue_name, const void *ptr_ref, const void *data, const uint32_t entry_time_sec, const uint32_t entry_time_nsec)
Trace queue delay experienced by the given function pointer.
Definition: tracetools.cpp:238
void message_processed(const char *message_name, const void *callback_ref, const uint32_t receipt_time_sec, const uint32_t receipt_time_nsec)
Mark the processing of a given "message".
Definition: tracetools.cpp:79
tracing_tools.hpp
Definition: tracetools.h:23
void subscription_message_dropped(const char *topic_arg, const void *buffer_arg, const void *queue_ref_arg, const void *callback_ref_arg, const void *message_ref_arg, int receipt_time_sec, int receipt_time_nsec)
Definition: tracetools.cpp:196
void subscriber_call_start(const std::string &topic, const void *queue_ref, const void *callback_ref, const void *message_ref, int receipt_time_sec, int receipt_time_nsec)
Trace the invocation of a previously queued subscriber call.
Definition: tracetools.cpp:221
void call_start(const void *ptr_ref, const void *data, const uint64_t trace_id)
Trace the start of a function call through a function pointer.
Definition: tracetools.cpp:67
void timer_scheduled(const void *callback_ref, const void *timer_ref)
Emit tracing information that the timer identified by &#39;timer_ref&#39; has been scheduled for invocation o...
Definition: tracetools.cpp:116
void subscriber_link_message_write(const void *message_ref_arg, const void *channel_ref_arg)
Trace on a message being written to the socket.
Definition: tracetools.cpp:173
void link_step(const char *element_name, const void *caller_name, const void *in_data_ref, const void *out_data_ref, const uint64_t trace_id)
Emit a trace message for a link in a processing chain.
Definition: tracetools.cpp:128


tracetools
Author(s): Ingo Luetkebohle
autogenerated on Wed Dec 18 2019 03:54:24