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 // This file defines the functions that encode tokenized logs at runtime. These
16 // are the only pw_tokenizer functions present in a binary that tokenizes
17 // strings. All other tokenizing code is resolved at compile time.
18
19 #include "pw_tokenizer/tokenize.h"
20
21 #include <cstring>
22
23 #include "pw_span/span.h"
24 #include "pw_tokenizer/encode_args.h"
25
26 namespace pw::tokenizer {
27 namespace {
28
29 static_assert(sizeof(PW_TOKENIZER_NESTED_PREFIX_STR) == 2,
30 "The nested prefix must be a single character string");
31
32 // Store metadata about this compilation's string tokenization in the ELF.
33 //
34 // The tokenizer metadata will not go into the on-device executable binary code.
35 // This metadata will be present in the ELF file's .pw_tokenizer.info section,
36 // from which the host-side tooling (Python, Java, etc.) can understand how to
37 // decode tokenized strings for the given binary. Only attributes that affect
38 // the decoding process are recorded.
39 //
40 // Tokenizer metadata is stored in an array of key-value pairs. Each Metadata
41 // object is 32 bytes: a 24-byte string and an 8-byte value. Metadata structs
42 // may be parsed in Python with the struct format '24s<Q'.
PW_PACKED(struct)43 PW_PACKED(struct) Metadata {
44 char name[24]; // name of the metadata field
45 uint64_t value; // value of the field
46 };
47
48 static_assert(sizeof(Metadata) == 32, "Metadata should be exactly 32 bytes");
49
50 // Store tokenization metadata in its own section. Mach-O files are not
51 // supported by pw_tokenizer, but a short, Mach-O compatible section name is
52 // used on macOS so that this file can at least compile.
53 #ifdef __APPLE__
54 #define PW_TOKENIZER_INFO_SECTION PW_KEEP_IN_SECTION(".pw_tokenizer")
55 #else
56 #define PW_TOKENIZER_INFO_SECTION PW_KEEP_IN_SECTION(".pw_tokenizer.info")
57 #endif // __APPLE__
58
59 constexpr Metadata metadata[] PW_TOKENIZER_INFO_SECTION = {
60 {"c_hash_length_bytes", PW_TOKENIZER_CFG_C_HASH_LENGTH},
61 {"sizeof_long", sizeof(long)}, // %l conversion specifier
62 {"sizeof_intmax_t", sizeof(intmax_t)}, // %j conversion specifier
63 {"sizeof_size_t", sizeof(size_t)}, // %z conversion specifier
64 {"sizeof_ptrdiff_t", sizeof(ptrdiff_t)}, // %t conversion specifier
65 };
66
67 } // namespace
68
_pw_tokenizer_ToBuffer(void * buffer,size_t * buffer_size_bytes,Token token,pw_tokenizer_ArgTypes types,...)69 extern "C" void _pw_tokenizer_ToBuffer(void* buffer,
70 size_t* buffer_size_bytes,
71 Token token,
72 pw_tokenizer_ArgTypes types,
73 ...) {
74 if (*buffer_size_bytes < sizeof(token)) {
75 *buffer_size_bytes = 0;
76 return;
77 }
78
79 std::memcpy(buffer, &token, sizeof(token));
80
81 va_list args;
82 va_start(args, types);
83 const size_t encoded_bytes = EncodeArgs(
84 types,
85 args,
86 span<std::byte>(static_cast<std::byte*>(buffer) + sizeof(token),
87 *buffer_size_bytes - sizeof(token)));
88 va_end(args);
89
90 *buffer_size_bytes = sizeof(token) + encoded_bytes;
91 }
92
93 } // namespace pw::tokenizer
94