C/C++ API Reference
Loading...
Searching...
No Matches
trace_tokenized.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14//==============================================================================
15//
16// This file provides the interface for working with the tokenized trace
17// backend.
18
19#pragma once
20
21#include <stdbool.h>
22#include <stdint.h>
23#include <string.h>
24
25#ifndef PW_TRACE_GET_TIME_DELTA
26#ifdef __cplusplus
27#include <type_traits>
28#endif // __cplusplus
29#endif // PW_TRACE_GET_TIME_DELTA
30
31#include "pw_status/status.h"
32#include "pw_tokenizer/tokenize.h"
33#include "pw_trace_tokenized/config.h"
34#include "pw_trace_tokenized/internal/trace_tokenized_internal.h"
35
36#ifdef __cplusplus
37namespace pw {
38namespace trace {
39
40using EventType = pw_trace_EventType;
41
42namespace internal {
43
44// Simple ring buffer which is suitable for use in a critical section.
45template <size_t kSize>
47 public:
49 uint32_t trace_token;
50 EventType event_type;
51 const char* module;
52 uint32_t trace_id;
53 uint8_t flags;
54 size_t data_size;
55 std::byte data_buffer[PW_TRACE_BUFFER_MAX_DATA_SIZE_BYTES];
56 };
57
58 pw::Status TryPushBack(uint32_t trace_token,
59 EventType event_type,
60 const char* module,
61 uint32_t trace_id,
62 uint8_t flags,
63 const void* data_buffer,
64 size_t data_size) {
65 if (IsFull()) {
67 }
70 }
71 event_queue_[head_].trace_token = trace_token;
72 event_queue_[head_].event_type = event_type;
73 event_queue_[head_].module = module;
74 event_queue_[head_].trace_id = trace_id;
75 event_queue_[head_].flags = flags;
76 event_queue_[head_].data_size = data_size;
77 for (size_t i = 0; i < data_size; i++) {
78 event_queue_[head_].data_buffer[i] =
79 reinterpret_cast<const std::byte*>(data_buffer)[i];
80 }
81 head_ = (head_ + 1) % kSize;
82 is_empty_ = false;
83 return pw::OkStatus();
84 }
85
86 const volatile QueueEventBlock* PeekFront() const {
87 if (IsEmpty()) {
88 return nullptr;
89 }
90 return &event_queue_[tail_];
91 }
92
93 void PopFront() {
94 if (!IsEmpty()) {
95 tail_ = (tail_ + 1) % kSize;
96 is_empty_ = (tail_ == head_);
97 }
98 }
99
100 void Clear() {
101 head_ = 0;
102 tail_ = 0;
103 is_empty_ = true;
104 }
105
106 bool IsEmpty() const { return is_empty_; }
107 bool IsFull() const { return !is_empty_ && (head_ == tail_); }
108
109 private:
110 std::array<volatile QueueEventBlock, kSize> event_queue_;
111 volatile size_t head_ = 0; // Next write
112 volatile size_t tail_ = 0; // Next read
113 volatile bool is_empty_ =
114 true; // Used to distinquish if head==tail is empty or full
115};
116
117} // namespace internal
118
120
123class Callbacks;
125 public:
126 TokenizedTracer(Callbacks& callbacks) : callbacks_(callbacks) {}
127 void Enable(bool enable) {
128 if (enable != enabled_ && enable) {
129 event_queue_.Clear();
130 last_trace_time_ = 0;
131 }
132 enabled_ = enable;
133 }
134 bool IsEnabled() const { return enabled_; }
135
136 void HandleTraceEvent(uint32_t trace_token,
137 EventType event_type,
138 const char* module,
139 uint32_t trace_id,
140 uint8_t flags,
141 const void* data_buffer,
142 size_t data_size);
143
144 private:
146 PW_TRACE_TIME_TYPE last_trace_time_ = 0;
147 bool enabled_ = false;
148 TraceQueue event_queue_;
149 Callbacks& callbacks_;
150
151 void HandleNextItemInQueue(
152 const volatile TraceQueue::QueueEventBlock* event_block);
153};
154
157
158} // namespace trace
159
161
162} // namespace pw
163#endif // __cplusplus
164
166
168#define PW_TRACE_SET_ENABLED(enabled) pw_trace_Enable(enabled)
169
211#define PW_TRACE_REF(event_type, module, label, flags, group) \
212 PW_TOKENIZE_STRING_DOMAIN("trace", \
213 PW_STRINGIFY(event_type) "|" PW_STRINGIFY( \
214 flags) "|" module "|" group "|" label)
215
216#define PW_TRACE_REF_DATA(event_type, module, label, flags, group, type) \
217 PW_TOKENIZE_STRING_DOMAIN( \
218 "trace", \
219 PW_STRINGIFY(event_type) "|" PW_STRINGIFY(flags) "|" module "|" group \
220 "|" label "|" type)
221
Definition: status.h:120
static constexpr Status InvalidArgument()
Definition: status.h:164
static constexpr Status ResourceExhausted()
Definition: status.h:230
Definition: trace_callback.h:135
Definition: trace_tokenized.h:124
Definition: trace_tokenized.h:46
constexpr Status OkStatus()
Definition: status.h:450
#define PW_TRACE_TIME_TYPE
The type for trace time.
Definition: config.h:37
#define PW_TRACE_BUFFER_MAX_DATA_SIZE_BYTES
Definition: config.h:93
TokenizedTracer & GetTokenizedTracer()
The Pigweed namespace.
Definition: alignment.h:27
pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::strin...
Definition: trace_tokenized.h:48