hppa: Fix typo in PA 2.0 trampoline template
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_symbolizer_mac.cpp
bloba9c958b2d10013609ea598f60764cdeebf3ee2a5
1 //===-- sanitizer_symbolizer_mac.cpp --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between various sanitizers' runtime libraries.
11 // Implementation of Mac-specific "atos" symbolizer.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
15 #if SANITIZER_APPLE
17 # include <dlfcn.h>
18 # include <errno.h>
19 # include <stdlib.h>
20 # include <sys/wait.h>
21 # include <unistd.h>
22 # include <util.h>
24 # include "sanitizer_allocator_internal.h"
25 # include "sanitizer_mac.h"
26 # include "sanitizer_symbolizer_mac.h"
28 namespace __sanitizer {
30 bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
31 Dl_info info;
32 int result = dladdr((const void *)addr, &info);
33 if (!result) return false;
35 // Compute offset if possible. `dladdr()` doesn't always ensure that `addr >=
36 // sym_addr` so only compute the offset when this holds. Failure to find the
37 // function offset is not treated as a failure because it might still be
38 // possible to get the symbol name.
39 uptr sym_addr = reinterpret_cast<uptr>(info.dli_saddr);
40 if (addr >= sym_addr) {
41 stack->info.function_offset = addr - sym_addr;
44 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
45 if (!demangled) return false;
46 stack->info.function = internal_strdup(demangled);
47 return true;
50 bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
51 Dl_info info;
52 int result = dladdr((const void *)addr, &info);
53 if (!result) return false;
54 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
55 datainfo->name = internal_strdup(demangled);
56 datainfo->start = (uptr)info.dli_saddr;
57 return true;
60 class AtosSymbolizerProcess final : public SymbolizerProcess {
61 public:
62 explicit AtosSymbolizerProcess(const char *path)
63 : SymbolizerProcess(path, /*use_posix_spawn*/ true) {
64 pid_str_[0] = '\0';
67 private:
68 bool StartSymbolizerSubprocess() override {
69 // Put the string command line argument in the object so that it outlives
70 // the call to GetArgV.
71 internal_snprintf(pid_str_, sizeof(pid_str_), "%d", (int)internal_getpid());
73 // Configure sandbox before starting atos process.
74 return SymbolizerProcess::StartSymbolizerSubprocess();
77 bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
78 return (length >= 1 && buffer[length - 1] == '\n');
81 void GetArgV(const char *path_to_binary,
82 const char *(&argv)[kArgVMax]) const override {
83 int i = 0;
84 argv[i++] = path_to_binary;
85 argv[i++] = "-p";
86 argv[i++] = &pid_str_[0];
87 if (GetMacosAlignedVersion() == MacosVersion(10, 9)) {
88 // On Mavericks atos prints a deprecation warning which we suppress by
89 // passing -d. The warning isn't present on other OSX versions, even the
90 // newer ones.
91 argv[i++] = "-d";
93 argv[i++] = nullptr;
94 CHECK_LE(i, kArgVMax);
97 char pid_str_[16];
100 #undef K_ATOS_ENV_VAR
102 static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
103 char **out_module, char **out_file, uptr *line,
104 uptr *start_address) {
105 // Trim ending newlines.
106 char *trim;
107 ExtractTokenUpToDelimiter(str, "\n", &trim);
109 // The line from `atos` is in one of these formats:
110 // myfunction (in library.dylib) (sourcefile.c:17)
111 // myfunction (in library.dylib) + 0x1fe
112 // myfunction (in library.dylib) + 15
113 // 0xdeadbeef (in library.dylib) + 0x1fe
114 // 0xdeadbeef (in library.dylib) + 15
115 // 0xdeadbeef (in library.dylib)
116 // 0xdeadbeef
118 const char *rest = trim;
119 char *symbol_name;
120 rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
121 if (rest[0] == '\0') {
122 InternalFree(symbol_name);
123 InternalFree(trim);
124 return false;
127 if (internal_strncmp(symbol_name, "0x", 2) != 0)
128 *out_name = symbol_name;
129 else
130 InternalFree(symbol_name);
131 rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
133 if (rest[0] == '(') {
134 if (out_file) {
135 rest++;
136 rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
137 char *extracted_line_number;
138 rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
139 if (line) *line = (uptr)internal_atoll(extracted_line_number);
140 InternalFree(extracted_line_number);
142 } else if (rest[0] == '+') {
143 rest += 2;
144 uptr offset = internal_atoll(rest);
145 if (start_address) *start_address = addr - offset;
148 InternalFree(trim);
149 return true;
152 AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
153 : process_(new (*allocator) AtosSymbolizerProcess(path)) {}
155 bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
156 if (!process_) return false;
157 if (addr == 0) return false;
158 char command[32];
159 internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
160 const char *buf = process_->SendCommand(command);
161 if (!buf) return false;
162 uptr line;
163 uptr start_address = AddressInfo::kUnknown;
164 if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
165 &stack->info.file, &line, &start_address)) {
166 Report("WARNING: atos failed to symbolize address \"0x%zx\"\n", addr);
167 return false;
169 stack->info.line = (int)line;
171 if (start_address == AddressInfo::kUnknown) {
172 // Fallback to dladdr() to get function start address if atos doesn't report
173 // it.
174 Dl_info info;
175 int result = dladdr((const void *)addr, &info);
176 if (result)
177 start_address = reinterpret_cast<uptr>(info.dli_saddr);
180 // Only assign to `function_offset` if we were able to get the function's
181 // start address and we got a sensible `start_address` (dladdr doesn't always
182 // ensure that `addr >= sym_addr`).
183 if (start_address != AddressInfo::kUnknown && addr >= start_address) {
184 stack->info.function_offset = addr - start_address;
186 return true;
189 bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
190 if (!process_) return false;
191 char command[32];
192 internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
193 const char *buf = process_->SendCommand(command);
194 if (!buf) return false;
195 if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
196 nullptr, &info->start)) {
197 process_ = nullptr;
198 return false;
200 return true;
203 } // namespace __sanitizer
205 #endif // SANITIZER_APPLE