This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathtombstone_symbolize.cpp
160 lines (126 loc) · 4.42 KB
/
tombstone_symbolize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tombstone_symbolize.h"
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "android-base/stringprintf.h"
#include "android-base/unique_fd.h"
#include "tombstone.pb.h"
using android::base::StringPrintf;
using android::base::unique_fd;
bool Symbolizer::Start(const std::vector<std::string>& debug_file_directories) {
unique_fd parent_in, parent_out, child_in, child_out;
if (!Pipe(&parent_in, &child_out) || !Pipe(&child_in, &parent_out)) {
return false;
}
std::vector<const char *> args;
args.push_back("llvm-symbolizer");
for (const std::string &dir : debug_file_directories) {
args.push_back("--debug-file-directory");
args.push_back(dir.c_str());
}
args.push_back(0);
int pid = fork();
if (pid == -1) {
return false;
} else if (pid == 0) {
parent_in.reset();
parent_out.reset();
dup2(child_in.get(), STDIN_FILENO);
dup2(child_out.get(), STDOUT_FILENO);
execvp("llvm-symbolizer", const_cast<char *const *>(args.data()));
fprintf(stderr, "unable to start llvm-symbolizer: %s\n", strerror(errno));
_exit(1);
} else {
child_in.reset();
child_out.reset();
// TODO: Check that llvm-symbolizer started up successfully.
// There used to be an easy way to do this, but it was removed in:
// https://github.com/llvm/llvm-project/commit/1792852f86dc75efa1f44d46b1a0daf386d64afa
in_fd = std::move(parent_in);
out_fd = std::move(parent_out);
return true;
}
}
std::string Symbolizer::read_response() {
std::string resp;
while (resp.size() < 2 || resp[resp.size() - 2] != '\n' || resp[resp.size() - 1] != '\n') {
char buf[4096];
ssize_t size = read(in_fd, buf, 4096);
if (size <= 0) {
return "";
}
resp.append(buf, size);
}
return resp;
}
std::vector<Symbolizer::Frame> Symbolizer::SymbolizeCode(std::string path, uint64_t rel_pc) {
std::string request = StringPrintf("CODE %s 0x%" PRIx64 "\n", path.c_str(), rel_pc);
if (write(out_fd, request.c_str(), request.size()) != static_cast<ssize_t>(request.size())) {
return {};
}
std::string response = read_response();
if (response.empty()) {
return {};
}
std::vector<Symbolizer::Frame> frames;
size_t frame_start = 0;
while (frame_start < response.size() - 1) {
Symbolizer::Frame frame;
size_t second_line_start = response.find('\n', frame_start) + 1;
if (second_line_start == std::string::npos + 1) {
return {};
}
size_t third_line_start = response.find('\n', second_line_start) + 1;
if (third_line_start == std::string::npos + 1) {
return {};
}
frame.function_name = response.substr(frame_start, second_line_start - frame_start - 1);
size_t column_number_start = response.rfind(':', third_line_start);
if (column_number_start == std::string::npos) {
return {};
}
size_t line_number_start = response.rfind(':', column_number_start - 1);
if (line_number_start == std::string::npos) {
return {};
}
frame.file = response.substr(second_line_start, line_number_start - second_line_start);
errno = 0;
frame.line = strtoull(response.c_str() + line_number_start + 1, 0, 10);
frame.column = strtoull(response.c_str() + column_number_start + 1, 0, 10);
if (errno != 0) {
return {};
}
frames.push_back(frame);
frame_start = third_line_start;
}
if (frames.size() == 1 && frames[0].file == "??") {
return {};
}
return frames;
}
void symbolize_backtrace_frame(const BacktraceFrame& frame, Symbolizer& sym) {
if (frame.build_id().empty()) {
return;
}
for (Symbolizer::Frame f : sym.SymbolizeCode("BUILDID:" + frame.build_id(), frame.rel_pc())) {
printf(" %s:%" PRId64 ":%" PRId64 " (%s)\n", f.file.c_str(), f.line, f.column,
f.function_name.c_str());
}
}