Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 1 | //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This utility works much like "addr2line". It is able of transforming |
| 10 | // tuples (module name, module offset) to code locations (function name, |
| 11 | // file, line number, column number). It is targeted for compiler-rt tools |
| 12 | // (especially AddressSanitizer and ThreadSanitizer) that can use it |
| 13 | // to symbolize stack traces in their error reports. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/Symbolize/DIPrinter.h" |
Alexey Samsonov | 57f8837 | 2015-10-26 17:56:12 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/COM.h" |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/Debug.h" |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InitLLVM.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 27 | #include <cstdio> |
| 28 | #include <cstring> |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | |
| 31 | using namespace llvm; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 32 | using namespace symbolize; |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 33 | |
| 34 | static cl::opt<bool> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 35 | ClUseSymbolTable("use-symbol-table", cl::init(true), |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 36 | cl::desc("Prefer names in symbol table to names " |
| 37 | "in debug info")); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 38 | |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 39 | static cl::opt<FunctionNameKind> ClPrintFunctions( |
| 40 | "functions", cl::init(FunctionNameKind::LinkageName), |
James Henderson | 9652652 | 2019-02-04 16:17:57 +0000 | [diff] [blame] | 41 | cl::desc("Print function name for a given address"), cl::ValueOptional, |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 42 | cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"), |
| 43 | clEnumValN(FunctionNameKind::ShortName, "short", |
| 44 | "print short function name"), |
| 45 | clEnumValN(FunctionNameKind::LinkageName, "linkage", |
Igor Kudrin | 99f641c | 2019-04-19 10:17:52 +0000 | [diff] [blame] | 46 | "print function linkage name"), |
James Henderson | 25ce596 | 2019-01-23 17:27:48 +0000 | [diff] [blame] | 47 | // Sentinel value for unspecified value. |
| 48 | clEnumValN(FunctionNameKind::LinkageName, "", ""))); |
| 49 | static cl::alias ClPrintFunctionsShort("f", cl::desc("Alias for -functions"), |
| 50 | cl::NotHidden, cl::Grouping, |
| 51 | cl::aliasopt(ClPrintFunctions)); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 52 | |
| 53 | static cl::opt<bool> |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 54 | ClUseRelativeAddress("relative-address", cl::init(false), |
| 55 | cl::desc("Interpret addresses as relative addresses"), |
| 56 | cl::ReallyHidden); |
| 57 | |
Peter Collingbourne | a56d81f | 2019-08-05 20:59:25 +0000 | [diff] [blame] | 58 | static cl::opt<bool> ClUntagAddresses( |
| 59 | "untag-addresses", cl::init(true), |
| 60 | cl::desc("Remove memory tags from addresses before symbolization")); |
| 61 | |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 62 | static cl::opt<bool> |
| 63 | ClPrintInlining("inlining", cl::init(true), |
| 64 | cl::desc("Print all inlined frames for a given address")); |
Douglas Yung | 7876c0e | 2019-01-24 00:34:09 +0000 | [diff] [blame] | 65 | static cl::alias |
| 66 | ClPrintInliningAliasI("i", cl::desc("Alias for -inlining"), |
| 67 | cl::NotHidden, cl::aliasopt(ClPrintInlining), |
| 68 | cl::Grouping); |
| 69 | static cl::alias |
| 70 | ClPrintInliningAliasInlines("inlines", cl::desc("Alias for -inlining"), |
| 71 | cl::NotHidden, cl::aliasopt(ClPrintInlining)); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 72 | |
James Henderson | 33c16a3 | 2019-01-22 10:24:32 +0000 | [diff] [blame] | 73 | // -basenames, -s |
| 74 | static cl::opt<bool> ClBasenames("basenames", cl::init(false), |
| 75 | cl::desc("Strip directory names from paths")); |
| 76 | static cl::alias ClBasenamesShort("s", cl::desc("Alias for -basenames"), |
| 77 | cl::NotHidden, cl::aliasopt(ClBasenames)); |
| 78 | |
Dmitry Venikov | 119cf66 | 2019-01-21 10:00:57 +0000 | [diff] [blame] | 79 | // -demangle, -C, -no-demangle |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 80 | static cl::opt<bool> |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 81 | ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); |
Dmitry Venikov | d3f21d3 | 2019-01-16 07:05:58 +0000 | [diff] [blame] | 82 | static cl::alias |
| 83 | ClDemangleShort("C", cl::desc("Alias for -demangle"), |
Dmitry Venikov | cce6687 | 2019-01-23 09:49:37 +0000 | [diff] [blame] | 84 | cl::NotHidden, cl::aliasopt(ClDemangle), cl::Grouping); |
Dmitry Venikov | 119cf66 | 2019-01-21 10:00:57 +0000 | [diff] [blame] | 85 | static cl::opt<bool> |
| 86 | ClNoDemangle("no-demangle", cl::init(false), |
| 87 | cl::desc("Don't demangle function names")); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 88 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 89 | static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""), |
| 90 | cl::desc("Default architecture " |
| 91 | "(for multi-arch objects)")); |
| 92 | |
Dmitry Venikov | 37c1e2e | 2019-01-11 11:51:52 +0000 | [diff] [blame] | 93 | // -obj, -exe, -e |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 94 | static cl::opt<std::string> |
| 95 | ClBinaryName("obj", cl::init(""), |
| 96 | cl::desc("Path to object file to be symbolized (if not provided, " |
| 97 | "object file should be specified for each input line)")); |
Dmitry Venikov | 37c1e2e | 2019-01-11 11:51:52 +0000 | [diff] [blame] | 98 | static cl::alias |
| 99 | ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"), |
| 100 | cl::NotHidden, cl::aliasopt(ClBinaryName)); |
Igor Kudrin | 734a2bc | 2019-04-04 08:45:06 +0000 | [diff] [blame] | 101 | static cl::alias ClBinaryNameAliasE("e", cl::desc("Alias for -obj"), |
| 102 | cl::NotHidden, cl::Grouping, cl::Prefix, |
| 103 | cl::aliasopt(ClBinaryName)); |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 104 | |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 105 | static cl::opt<std::string> |
| 106 | ClDwpName("dwp", cl::init(""), |
| 107 | cl::desc("Path to DWP file to be use for any split CUs")); |
| 108 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 109 | static cl::list<std::string> |
| 110 | ClDsymHint("dsym-hint", cl::ZeroOrMore, |
| 111 | cl::desc("Path to .dSYM bundles to search for debug info for the " |
| 112 | "object files")); |
Dmitry Venikov | 5c1768f | 2019-01-14 10:10:51 +0000 | [diff] [blame] | 113 | |
| 114 | // -print-address, -addresses, -a |
Hemant Kulkarni | 80f82fb | 2015-10-12 19:26:44 +0000 | [diff] [blame] | 115 | static cl::opt<bool> |
Dmitry Venikov | 5c1768f | 2019-01-14 10:10:51 +0000 | [diff] [blame] | 116 | ClPrintAddress("print-address", cl::init(false), |
| 117 | cl::desc("Show address before line information")); |
| 118 | static cl::alias |
| 119 | ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"), |
| 120 | cl::NotHidden, cl::aliasopt(ClPrintAddress)); |
| 121 | static cl::alias |
| 122 | ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"), |
Dmitry Venikov | cce6687 | 2019-01-23 09:49:37 +0000 | [diff] [blame] | 123 | cl::NotHidden, cl::aliasopt(ClPrintAddress), cl::Grouping); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 124 | |
Dmitry Venikov | 60d71e4 | 2019-01-10 15:33:35 +0000 | [diff] [blame] | 125 | // -pretty-print, -p |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 126 | static cl::opt<bool> |
| 127 | ClPrettyPrint("pretty-print", cl::init(false), |
| 128 | cl::desc("Make the output more human friendly")); |
Dmitry Venikov | 60d71e4 | 2019-01-10 15:33:35 +0000 | [diff] [blame] | 129 | static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"), |
| 130 | cl::NotHidden, |
Dmitry Venikov | cce6687 | 2019-01-23 09:49:37 +0000 | [diff] [blame] | 131 | cl::aliasopt(ClPrettyPrint), cl::Grouping); |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 132 | |
Mike Aizatsky | 17dbc28 | 2016-01-09 00:14:35 +0000 | [diff] [blame] | 133 | static cl::opt<int> ClPrintSourceContextLines( |
| 134 | "print-source-context-lines", cl::init(0), |
| 135 | cl::desc("Print N number of source file context")); |
| 136 | |
David Blaikie | 0012dd5 | 2017-01-31 22:19:38 +0000 | [diff] [blame] | 137 | static cl::opt<bool> ClVerbose("verbose", cl::init(false), |
| 138 | cl::desc("Print verbose line info")); |
| 139 | |
James Henderson | 759d5e6 | 2019-01-25 11:49:21 +0000 | [diff] [blame] | 140 | // -adjust-vma |
Fangrui Song | b5f3984 | 2019-04-24 02:40:20 +0000 | [diff] [blame] | 141 | static cl::opt<uint64_t> |
James Henderson | 759d5e6 | 2019-01-25 11:49:21 +0000 | [diff] [blame] | 142 | ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"), |
| 143 | cl::desc("Add specified offset to object file addresses")); |
| 144 | |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 145 | static cl::list<std::string> ClInputAddresses(cl::Positional, |
| 146 | cl::desc("<input addresses>..."), |
| 147 | cl::ZeroOrMore); |
| 148 | |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 149 | static cl::opt<std::string> |
| 150 | ClFallbackDebugPath("fallback-debug-path", cl::init(""), |
| 151 | cl::desc("Fallback path for debug binaries.")); |
| 152 | |
Petr Hosek | 00e436f | 2019-11-26 17:18:42 -0800 | [diff] [blame] | 153 | static cl::list<std::string> |
| 154 | ClDebugFileDirectory("debug-file-directory", cl::ZeroOrMore, |
| 155 | cl::value_desc("dir"), |
| 156 | cl::desc("Path to directory where to look for debug " |
| 157 | "files.")); |
| 158 | |
Igor Kudrin | 0fed7b0 | 2019-04-04 08:39:40 +0000 | [diff] [blame] | 159 | static cl::opt<DIPrinter::OutputStyle> |
| 160 | ClOutputStyle("output-style", cl::init(DIPrinter::OutputStyle::LLVM), |
Igor Kudrin | 1b71b7f | 2019-04-19 10:14:18 +0000 | [diff] [blame] | 161 | cl::desc("Specify print style"), |
Igor Kudrin | 0fed7b0 | 2019-04-04 08:39:40 +0000 | [diff] [blame] | 162 | cl::values(clEnumValN(DIPrinter::OutputStyle::LLVM, "LLVM", |
| 163 | "LLVM default style"), |
| 164 | clEnumValN(DIPrinter::OutputStyle::GNU, "GNU", |
| 165 | "GNU addr2line style"))); |
| 166 | |
James Henderson | 9485b26 | 2019-06-21 11:49:20 +0000 | [diff] [blame] | 167 | static cl::extrahelp |
| 168 | HelpResponse("\nPass @FILE as argument to read options from FILE.\n"); |
| 169 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 170 | template<typename T> |
| 171 | static bool error(Expected<T> &ResOrErr) { |
| 172 | if (ResOrErr) |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 173 | return false; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 174 | logAllUnhandledErrors(ResOrErr.takeError(), errs(), |
| 175 | "LLVMSymbolizer: error reading file: "); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 179 | enum class Command { |
| 180 | Code, |
| 181 | Data, |
| 182 | Frame, |
| 183 | }; |
| 184 | |
| 185 | static bool parseCommand(StringRef InputString, Command &Cmd, |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 186 | std::string &ModuleName, uint64_t &ModuleOffset) { |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 187 | const char kDelimiters[] = " \n\r"; |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 188 | ModuleName = ""; |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 189 | if (InputString.consume_front("CODE ")) { |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 190 | Cmd = Command::Code; |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 191 | } else if (InputString.consume_front("DATA ")) { |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 192 | Cmd = Command::Data; |
| 193 | } else if (InputString.consume_front("FRAME ")) { |
| 194 | Cmd = Command::Frame; |
Dmitry Vyukov | d08bd13 | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 195 | } else { |
| 196 | // If no cmd, assume it's CODE. |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 197 | Cmd = Command::Code; |
Dmitry Vyukov | d08bd13 | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 198 | } |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 199 | const char *pos = InputString.data(); |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 200 | // Skip delimiters and parse input filename (if needed). |
Fangrui Song | ffebfe1 | 2018-05-26 02:29:14 +0000 | [diff] [blame] | 201 | if (ClBinaryName.empty()) { |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 202 | pos += strspn(pos, kDelimiters); |
| 203 | if (*pos == '"' || *pos == '\'') { |
| 204 | char quote = *pos; |
| 205 | pos++; |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 206 | const char *end = strchr(pos, quote); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 207 | if (!end) |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 208 | return false; |
| 209 | ModuleName = std::string(pos, end - pos); |
| 210 | pos = end + 1; |
| 211 | } else { |
| 212 | int name_length = strcspn(pos, kDelimiters); |
| 213 | ModuleName = std::string(pos, name_length); |
| 214 | pos += name_length; |
| 215 | } |
Alexey Samsonov | d206932 | 2013-04-05 09:22:24 +0000 | [diff] [blame] | 216 | } else { |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 217 | ModuleName = ClBinaryName; |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 218 | } |
Alexey Samsonov | d206932 | 2013-04-05 09:22:24 +0000 | [diff] [blame] | 219 | // Skip delimiters and parse module offset. |
| 220 | pos += strspn(pos, kDelimiters); |
| 221 | int offset_length = strcspn(pos, kDelimiters); |
Rafael Espindola | 2c5bcc5 | 2015-10-24 23:23:25 +0000 | [diff] [blame] | 222 | return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 223 | } |
| 224 | |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 225 | static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer, |
| 226 | DIPrinter &Printer) { |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 227 | Command Cmd; |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 228 | std::string ModuleName; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 229 | uint64_t Offset = 0; |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 230 | if (!parseCommand(StringRef(InputString), Cmd, ModuleName, Offset)) { |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 231 | outs() << InputString; |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | if (ClPrintAddress) { |
| 236 | outs() << "0x"; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 237 | outs().write_hex(Offset); |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 238 | StringRef Delimiter = ClPrettyPrint ? ": " : "\n"; |
| 239 | outs() << Delimiter; |
| 240 | } |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 241 | Offset -= ClAdjustVMA; |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 242 | if (Cmd == Command::Data) { |
Alexey Lapshin | b2c4b8b | 2019-03-23 08:08:40 +0000 | [diff] [blame] | 243 | auto ResOrErr = Symbolizer.symbolizeData( |
| 244 | ModuleName, {Offset, object::SectionedAddress::UndefSection}); |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 245 | Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get()); |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 246 | } else if (Cmd == Command::Frame) { |
| 247 | auto ResOrErr = Symbolizer.symbolizeFrame( |
| 248 | ModuleName, {Offset, object::SectionedAddress::UndefSection}); |
| 249 | if (!error(ResOrErr)) { |
| 250 | for (DILocal Local : *ResOrErr) |
| 251 | Printer << Local; |
| 252 | if (ResOrErr->empty()) |
| 253 | outs() << "??\n"; |
| 254 | } |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 255 | } else if (ClPrintInlining) { |
Alexey Lapshin | b2c4b8b | 2019-03-23 08:08:40 +0000 | [diff] [blame] | 256 | auto ResOrErr = Symbolizer.symbolizeInlinedCode( |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 257 | ModuleName, {Offset, object::SectionedAddress::UndefSection}); |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 258 | Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get()); |
Igor Kudrin | 4bc29cb | 2019-04-19 10:12:56 +0000 | [diff] [blame] | 259 | } else if (ClOutputStyle == DIPrinter::OutputStyle::GNU) { |
| 260 | // With ClPrintFunctions == FunctionNameKind::LinkageName (default) |
| 261 | // and ClUseSymbolTable == true (also default), Symbolizer.symbolizeCode() |
| 262 | // may override the name of an inlined function with the name of the topmost |
| 263 | // caller function in the inlining chain. This contradicts the existing |
| 264 | // behavior of addr2line. Symbolizer.symbolizeInlinedCode() overrides only |
| 265 | // the topmost function, which suits our needs better. |
| 266 | auto ResOrErr = Symbolizer.symbolizeInlinedCode( |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 267 | ModuleName, {Offset, object::SectionedAddress::UndefSection}); |
Igor Kudrin | 4bc29cb | 2019-04-19 10:12:56 +0000 | [diff] [blame] | 268 | Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get().getFrame(0)); |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 269 | } else { |
Alexey Lapshin | b2c4b8b | 2019-03-23 08:08:40 +0000 | [diff] [blame] | 270 | auto ResOrErr = Symbolizer.symbolizeCode( |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 271 | ModuleName, {Offset, object::SectionedAddress::UndefSection}); |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 272 | Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get()); |
| 273 | } |
Igor Kudrin | 4bc29cb | 2019-04-19 10:12:56 +0000 | [diff] [blame] | 274 | if (ClOutputStyle == DIPrinter::OutputStyle::LLVM) |
| 275 | outs() << "\n"; |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 278 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 279 | InitLLVM X(argc, argv); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 280 | |
Igor Kudrin | 99f641c | 2019-04-19 10:17:52 +0000 | [diff] [blame] | 281 | bool IsAddr2Line = sys::path::stem(argv[0]).contains("addr2line"); |
| 282 | |
| 283 | if (IsAddr2Line) { |
| 284 | ClDemangle.setInitialValue(false); |
| 285 | ClPrintFunctions.setInitialValue(FunctionNameKind::None); |
| 286 | ClPrintInlining.setInitialValue(false); |
Peter Collingbourne | a56d81f | 2019-08-05 20:59:25 +0000 | [diff] [blame] | 287 | ClUntagAddresses.setInitialValue(false); |
Igor Kudrin | 99f641c | 2019-04-19 10:17:52 +0000 | [diff] [blame] | 288 | ClOutputStyle.setInitialValue(DIPrinter::OutputStyle::GNU); |
| 289 | } |
| 290 | |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 291 | llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded); |
Petr Hosek | dedad08 | 2019-12-18 10:19:47 -0800 | [diff] [blame^] | 292 | cl::ParseCommandLineOptions( |
| 293 | argc, argv, IsAddr2Line ? "llvm-addr2line\n" : "llvm-symbolizer\n", |
| 294 | /*Errs=*/nullptr, |
| 295 | IsAddr2Line ? "LLVM_ADDR2LINE_OPTS" : "LLVM_SYMBOLIZER_OPTS"); |
Dmitry Venikov | 119cf66 | 2019-01-21 10:00:57 +0000 | [diff] [blame] | 296 | |
| 297 | // If both --demangle and --no-demangle are specified then pick the last one. |
| 298 | if (ClNoDemangle.getPosition() > ClDemangle.getPosition()) |
| 299 | ClDemangle = !ClNoDemangle; |
| 300 | |
Peter Collingbourne | a2048f8 | 2019-06-11 02:31:54 +0000 | [diff] [blame] | 301 | LLVMSymbolizer::Options Opts; |
| 302 | Opts.PrintFunctions = ClPrintFunctions; |
| 303 | Opts.UseSymbolTable = ClUseSymbolTable; |
| 304 | Opts.Demangle = ClDemangle; |
| 305 | Opts.RelativeAddresses = ClUseRelativeAddress; |
Peter Collingbourne | a56d81f | 2019-08-05 20:59:25 +0000 | [diff] [blame] | 306 | Opts.UntagAddresses = ClUntagAddresses; |
Peter Collingbourne | a2048f8 | 2019-06-11 02:31:54 +0000 | [diff] [blame] | 307 | Opts.DefaultArch = ClDefaultArch; |
| 308 | Opts.FallbackDebugPath = ClFallbackDebugPath; |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 309 | Opts.DWPName = ClDwpName; |
Petr Hosek | 00e436f | 2019-11-26 17:18:42 -0800 | [diff] [blame] | 310 | Opts.DebugFileDirectory = ClDebugFileDirectory; |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 311 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 312 | for (const auto &hint : ClDsymHint) { |
| 313 | if (sys::path::extension(hint) == ".dSYM") { |
| 314 | Opts.DsymHints.push_back(hint); |
| 315 | } else { |
| 316 | errs() << "Warning: invalid dSYM hint: \"" << hint << |
| 317 | "\" (must have the '.dSYM' extension).\n"; |
| 318 | } |
| 319 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 320 | LLVMSymbolizer Symbolizer(Opts); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 321 | |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 322 | DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None, |
James Henderson | 33c16a3 | 2019-01-22 10:24:32 +0000 | [diff] [blame] | 323 | ClPrettyPrint, ClPrintSourceContextLines, ClVerbose, |
Igor Kudrin | 0fed7b0 | 2019-04-04 08:39:40 +0000 | [diff] [blame] | 324 | ClBasenames, ClOutputStyle); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 325 | |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 326 | if (ClInputAddresses.empty()) { |
| 327 | const int kMaxInputStringLength = 1024; |
| 328 | char InputString[kMaxInputStringLength]; |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 329 | |
James Henderson | 7f31350 | 2019-06-04 15:34:58 +0000 | [diff] [blame] | 330 | while (fgets(InputString, sizeof(InputString), stdin)) { |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 331 | symbolizeInput(InputString, Symbolizer, Printer); |
James Henderson | 7f31350 | 2019-06-04 15:34:58 +0000 | [diff] [blame] | 332 | outs().flush(); |
| 333 | } |
James Henderson | 3a6a5a3 | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 334 | } else { |
| 335 | for (StringRef Address : ClInputAddresses) |
| 336 | symbolizeInput(Address, Symbolizer, Printer); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 337 | } |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 338 | |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | } |