1 /*
2 * Copyright (C) 2020 The Android Open Source Project
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 "src/traced/probes/ftrace/printk_formats_parser.h"
18
19 #include <stdio.h>
20
21 #include <cinttypes>
22 #include <optional>
23
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/file_utils.h"
26 #include "perfetto/ext/base/string_splitter.h"
27 #include "perfetto/ext/base/string_utils.h"
28
29 namespace perfetto {
30
ParsePrintkFormats(const std::string & format)31 PrintkMap ParsePrintkFormats(const std::string& format) {
32 PrintkMap mapping;
33 for (base::StringSplitter lines(format, '\n'); lines.Next();) {
34 // Lines have the format:
35 // 0xdeadbeef : "not alive cow"
36 // and may be duplicated.
37 std::string line(lines.cur_token());
38
39 auto index = line.find(':');
40 if (index == std::string::npos)
41 continue;
42 std::string raw_address = line.substr(0, index);
43 std::string name = line.substr(index);
44
45 // Remove colon, space and surrounding quotes:
46 raw_address = base::StripSuffix(raw_address, " ");
47 name = base::StripPrefix(name, ":");
48 name = base::StripPrefix(name, " ");
49 name = base::StripPrefix(name, "\"");
50 name = base::StripSuffix(name, "\"");
51
52 if (name.empty())
53 continue;
54
55 std::optional<uint64_t> address = base::StringToUInt64(raw_address, 16);
56 if (address && address.value() != 0)
57 mapping.insert(address.value(), name);
58 }
59 return mapping;
60 }
61
62 } // namespace perfetto
63