1 //===-- sanitizer_symbolizer_mac.cc ---------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between various sanitizers' runtime libraries.
12 // Implementation of Mac-specific "atos" symbolizer.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_platform.h"
18 #include "sanitizer_allocator_internal.h"
19 #include "sanitizer_mac.h"
20 #include "sanitizer_symbolizer_mac.h"
29 namespace __sanitizer {
31 bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
33 int result = dladdr((const void *)addr, &info);
34 if (!result) return false;
35 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
36 if (!demangled) return false;
37 stack->info.function = internal_strdup(demangled);
41 bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
43 int result = dladdr((const void *)addr, &info);
44 if (!result) return false;
45 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
46 datainfo->name = internal_strdup(demangled);
47 datainfo->start = (uptr)info.dli_saddr;
51 class AtosSymbolizerProcess : public SymbolizerProcess {
53 explicit AtosSymbolizerProcess(const char *path, pid_t parent_pid)
54 : SymbolizerProcess(path, /*use_forkpty*/ true) {
55 // Put the string command line argument in the object so that it outlives
56 // the call to GetArgV.
57 internal_snprintf(pid_str_, sizeof(pid_str_), "%d", parent_pid);
61 bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
62 return (length >= 1 && buffer[length - 1] == '\n');
65 void GetArgV(const char *path_to_binary,
66 const char *(&argv)[kArgVMax]) const override {
68 argv[i++] = path_to_binary;
70 argv[i++] = &pid_str_[0];
71 if (GetMacosVersion() == MACOS_VERSION_MAVERICKS) {
72 // On Mavericks atos prints a deprecation warning which we suppress by
73 // passing -d. The warning isn't present on other OSX versions, even the
83 static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
84 char **out_module, char **out_file, uptr *line,
85 uptr *start_address) {
86 // Trim ending newlines.
88 ExtractTokenUpToDelimiter(str, "\n", &trim);
90 // The line from `atos` is in one of these formats:
91 // myfunction (in library.dylib) (sourcefile.c:17)
92 // myfunction (in library.dylib) + 0x1fe
93 // myfunction (in library.dylib) + 15
94 // 0xdeadbeef (in library.dylib) + 0x1fe
95 // 0xdeadbeef (in library.dylib) + 15
96 // 0xdeadbeef (in library.dylib)
99 const char *rest = trim;
101 rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
102 if (rest[0] == '\0') {
103 InternalFree(symbol_name);
108 if (internal_strncmp(symbol_name, "0x", 2) != 0)
109 *out_name = symbol_name;
111 InternalFree(symbol_name);
112 rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
114 if (rest[0] == '(') {
117 rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
118 char *extracted_line_number;
119 rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
120 if (line) *line = (uptr)internal_atoll(extracted_line_number);
121 InternalFree(extracted_line_number);
123 } else if (rest[0] == '+') {
125 uptr offset = internal_atoll(rest);
126 if (start_address) *start_address = addr - offset;
133 AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
134 : process_(new(*allocator) AtosSymbolizerProcess(path, getpid())) {}
136 bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
137 if (!process_) return false;
138 if (addr == 0) return false;
140 internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
141 const char *buf = process_->SendCommand(command);
142 if (!buf) return false;
144 if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
145 &stack->info.file, &line, nullptr)) {
149 stack->info.line = (int)line;
153 bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
154 if (!process_) return false;
156 internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
157 const char *buf = process_->SendCommand(command);
158 if (!buf) return false;
159 if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
160 nullptr, &info->start)) {
167 } // namespace __sanitizer
169 #endif // SANITIZER_MAC