]>
Commit | Line | Data |
---|---|---|
bd6521f0 A |
1 | // |
2 | // TraceCodes.cpp | |
3 | // KDBG | |
4 | // | |
5 | // Created by James McIlree on 4/16/13. | |
6 | // Copyright (c) 2014 Apple. All rights reserved. | |
7 | // | |
8 | ||
9 | #include "KDebug.h" | |
10 | ||
11 | std::vector<std::string> default_trace_code_paths() { | |
12 | // As of 4/17/2013, this is a single file. | |
13 | return { "/usr/share/misc/trace.codes" }; | |
14 | } | |
15 | ||
16 | std::unordered_map<uint32_t, std::string> trace_codes_at_path(const char* path) | |
17 | { | |
18 | std::unordered_map<uint32_t, std::string> codes; | |
19 | ||
20 | if (FILE* fp = fopen(path, "r")) { | |
21 | char line[PATH_MAX]; | |
22 | ||
23 | while (fgets(line, sizeof(line), fp)) { | |
24 | int code; | |
25 | char name[128]; | |
26 | if (sscanf(line, "%x%127s\n", &code, name) == 2) { | |
27 | ASSERT(code != 0, "Should never have a code equal to zero"); | |
28 | ASSERT(strlen(name), "Invalid name"); | |
29 | codes[code] = name; | |
30 | } | |
31 | } | |
32 | ||
33 | fclose(fp); | |
34 | } | |
35 | ||
36 | return codes; | |
37 | } | |
38 | ||
39 | std::unordered_map<uint32_t, std::string> resolve_trace_codes(bool should_read_default_codes, int output_fd, std::vector<std::string>& additional_paths) { | |
40 | std::unordered_map<uint32_t, std::string> codes; | |
41 | ||
42 | std::vector<std::string> paths; | |
43 | ||
44 | if (should_read_default_codes) { | |
45 | std::vector<std::string> default_paths = default_trace_code_paths(); | |
46 | paths.insert(paths.end(), default_paths.begin(), default_paths.end()); | |
47 | } | |
48 | ||
49 | paths.insert(paths.end(), additional_paths.begin(), additional_paths.end()); | |
50 | ||
51 | for (auto& path : paths) { | |
52 | std::unordered_map<uint32_t, std::string> partial = trace_codes_at_path(path.c_str()); | |
53 | ||
54 | if (output_fd > -1) { | |
55 | dprintf(output_fd, "Read %zd codes from %s\n", partial.size(), path.c_str()); | |
56 | } | |
57 | ||
58 | if (codes.empty()) { | |
59 | codes = std::move(partial); | |
60 | } else { | |
61 | for (auto& map_pair : partial) { | |
62 | auto insert_it = codes.insert(map_pair); | |
63 | if (insert_it.second == false) { | |
64 | if (map_pair.second != codes[map_pair.first]) { | |
65 | dprintf(output_fd, "WARNING: code entry for 0x%x has multiple entries (%s, %s)\n", map_pair.first, map_pair.second.c_str(), codes[map_pair.first].c_str()); | |
66 | } | |
67 | } | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | return codes; | |
73 | } |