5 // Created by James McIlree on 4/16/13.
6 // Copyright (c) 2014 Apple. All rights reserved.
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" };
16 std::unordered_map
<uint32_t, std::string
> trace_codes_at_path(const char* path
)
18 std::unordered_map
<uint32_t, std::string
> codes
;
20 if (FILE* fp
= fopen(path
, "r")) {
23 while (fgets(line
, sizeof(line
), fp
)) {
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");
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
;
42 std::vector
<std::string
> paths
;
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());
49 paths
.insert(paths
.end(), additional_paths
.begin(), additional_paths
.end());
51 for (auto& path
: paths
) {
52 std::unordered_map
<uint32_t, std::string
> partial
= trace_codes_at_path(path
.c_str());
55 dprintf(output_fd
, "Read %zd codes from %s\n", partial
.size(), path
.c_str());
59 codes
= std::move(partial
);
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());