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"
17 #include "sanitizer_allocator_internal.h"
18 #include "sanitizer_mac.h"
19 #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
);
45 if (!demangled
) return false;
46 stack
->info
.function
= internal_strdup(demangled
);
50 bool DlAddrSymbolizer::SymbolizeData(uptr addr
, DataInfo
*datainfo
) {
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
;
60 class AtosSymbolizerProcess final
: public SymbolizerProcess
{
62 explicit AtosSymbolizerProcess(const char *path
)
63 : SymbolizerProcess(path
, /*use_posix_spawn*/ true) {
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
{
84 argv
[i
++] = path_to_binary
;
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
94 CHECK_LE(i
, kArgVMax
);
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.
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)
118 const char *rest
= trim
;
120 rest
= ExtractTokenUpToDelimiter(rest
, " (in ", &symbol_name
);
121 if (rest
[0] == '\0') {
122 InternalFree(symbol_name
);
127 if (internal_strncmp(symbol_name
, "0x", 2) != 0)
128 *out_name
= symbol_name
;
130 InternalFree(symbol_name
);
131 rest
= ExtractTokenUpToDelimiter(rest
, ") ", out_module
);
133 if (rest
[0] == '(') {
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] == '+') {
144 uptr offset
= internal_atoll(rest
);
145 if (start_address
) *start_address
= addr
- offset
;
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;
159 internal_snprintf(command
, sizeof(command
), "0x%zx\n", addr
);
160 const char *buf
= process_
->SendCommand(command
);
161 if (!buf
) return false;
163 uptr start_address
= AddressInfo::kUnknown
;
164 if (!ParseCommandOutput(buf
, addr
, &stack
->info
.function
, &stack
->info
.module
,
165 &stack
->info
.file
, &line
, &start_address
)) {
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
175 int result
= dladdr((const void *)addr
, &info
);
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
;
189 bool AtosSymbolizer::SymbolizeData(uptr addr
, DataInfo
*info
) {
190 if (!process_
) return false;
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
)) {
203 } // namespace __sanitizer
205 #endif // SANITIZER_APPLE