1 //===-- sanitizer_symbolizer_mac.cpp --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between various sanitizers' runtime libraries.
11 // Implementation of Mac-specific "atos" symbolizer.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
20 # include <sys/wait.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
) {
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
);
46 demangled
= info
.dli_sname
;
47 stack
->info
.function
= internal_strdup(demangled
);
51 bool DlAddrSymbolizer::SymbolizeData(uptr addr
, DataInfo
*datainfo
) {
53 int result
= dladdr((const void *)addr
, &info
);
54 if (!result
) return false;
55 const char *demangled
= DemangleSwiftAndCXX(info
.dli_sname
);
57 demangled
= info
.dli_sname
;
58 datainfo
->name
= internal_strdup(demangled
);
59 datainfo
->start
= (uptr
)info
.dli_saddr
;
63 class AtosSymbolizerProcess final
: public SymbolizerProcess
{
65 explicit AtosSymbolizerProcess(const char *path
)
66 : SymbolizerProcess(path
, /*use_posix_spawn*/ true) {
71 bool StartSymbolizerSubprocess() override
{
72 // Put the string command line argument in the object so that it outlives
73 // the call to GetArgV.
74 internal_snprintf(pid_str_
, sizeof(pid_str_
), "%d", (int)internal_getpid());
76 // Configure sandbox before starting atos process.
77 return SymbolizerProcess::StartSymbolizerSubprocess();
80 bool ReachedEndOfOutput(const char *buffer
, uptr length
) const override
{
81 return (length
>= 1 && buffer
[length
- 1] == '\n');
84 void GetArgV(const char *path_to_binary
,
85 const char *(&argv
)[kArgVMax
]) const override
{
87 argv
[i
++] = path_to_binary
;
89 argv
[i
++] = &pid_str_
[0];
90 if (GetMacosAlignedVersion() == MacosVersion(10, 9)) {
91 // On Mavericks atos prints a deprecation warning which we suppress by
92 // passing -d. The warning isn't present on other OSX versions, even the
97 CHECK_LE(i
, kArgVMax
);
103 #undef K_ATOS_ENV_VAR
105 static bool ParseCommandOutput(const char *str
, uptr addr
, char **out_name
,
106 char **out_module
, char **out_file
, uptr
*line
,
107 uptr
*start_address
) {
108 // Trim ending newlines.
110 ExtractTokenUpToDelimiter(str
, "\n", &trim
);
112 // The line from `atos` is in one of these formats:
113 // myfunction (in library.dylib) (sourcefile.c:17)
114 // myfunction (in library.dylib) + 0x1fe
115 // myfunction (in library.dylib) + 15
116 // 0xdeadbeef (in library.dylib) + 0x1fe
117 // 0xdeadbeef (in library.dylib) + 15
118 // 0xdeadbeef (in library.dylib)
121 const char *rest
= trim
;
123 rest
= ExtractTokenUpToDelimiter(rest
, " (in ", &symbol_name
);
124 if (rest
[0] == '\0') {
125 InternalFree(symbol_name
);
130 if (internal_strncmp(symbol_name
, "0x", 2) != 0)
131 *out_name
= symbol_name
;
133 InternalFree(symbol_name
);
134 rest
= ExtractTokenUpToDelimiter(rest
, ") ", out_module
);
136 if (rest
[0] == '(') {
139 rest
= ExtractTokenUpToDelimiter(rest
, ":", out_file
);
140 char *extracted_line_number
;
141 rest
= ExtractTokenUpToDelimiter(rest
, ")", &extracted_line_number
);
142 if (line
) *line
= (uptr
)internal_atoll(extracted_line_number
);
143 InternalFree(extracted_line_number
);
145 } else if (rest
[0] == '+') {
147 uptr offset
= internal_atoll(rest
);
148 if (start_address
) *start_address
= addr
- offset
;
155 AtosSymbolizer::AtosSymbolizer(const char *path
, LowLevelAllocator
*allocator
)
156 : process_(new (*allocator
) AtosSymbolizerProcess(path
)) {}
158 bool AtosSymbolizer::SymbolizePC(uptr addr
, SymbolizedStack
*stack
) {
159 if (!process_
) return false;
160 if (addr
== 0) return false;
162 internal_snprintf(command
, sizeof(command
), "0x%zx\n", addr
);
163 const char *buf
= process_
->SendCommand(command
);
164 if (!buf
) return false;
166 uptr start_address
= AddressInfo::kUnknown
;
167 if (!ParseCommandOutput(buf
, addr
, &stack
->info
.function
, &stack
->info
.module
,
168 &stack
->info
.file
, &line
, &start_address
)) {
169 Report("WARNING: atos failed to symbolize address \"0x%zx\"\n", addr
);
172 stack
->info
.line
= (int)line
;
174 if (start_address
== AddressInfo::kUnknown
) {
175 // Fallback to dladdr() to get function start address if atos doesn't report
178 int result
= dladdr((const void *)addr
, &info
);
180 start_address
= reinterpret_cast<uptr
>(info
.dli_saddr
);
183 // Only assign to `function_offset` if we were able to get the function's
184 // start address and we got a sensible `start_address` (dladdr doesn't always
185 // ensure that `addr >= sym_addr`).
186 if (start_address
!= AddressInfo::kUnknown
&& addr
>= start_address
) {
187 stack
->info
.function_offset
= addr
- start_address
;
192 bool AtosSymbolizer::SymbolizeData(uptr addr
, DataInfo
*info
) {
193 if (!process_
) return false;
195 internal_snprintf(command
, sizeof(command
), "0x%zx\n", addr
);
196 const char *buf
= process_
->SendCommand(command
);
197 if (!buf
) return false;
198 if (!ParseCommandOutput(buf
, addr
, &info
->name
, &info
->module
, nullptr,
199 nullptr, &info
->start
)) {
206 } // namespace __sanitizer
208 #endif // SANITIZER_APPLE