| 1 | // Copyright (c) 2011, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <paths.h> |
| 31 | #include <stdio.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <cstring> |
| 35 | #include <iostream> |
| 36 | #include <string> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include "common/linux/dump_symbols.h" |
| 40 | #include "common/path_helper.h" |
| 41 | |
| 42 | using google_breakpad::WriteSymbolFile; |
| 43 | using google_breakpad::WriteSymbolFileHeader; |
| 44 | |
| 45 | int usage(const char* self) { |
| 46 | fprintf(stderr, |
| 47 | "Usage: %s [OPTION] <binary-with-debugging-info> " |
| 48 | "[directories-for-debug-file]\n\n" , |
| 49 | google_breakpad::BaseName(self).c_str()); |
| 50 | fprintf(stderr, "Options:\n" ); |
| 51 | fprintf(stderr, " -i: Output module header information only.\n" ); |
| 52 | fprintf(stderr, " -c Do not generate CFI section\n" ); |
| 53 | fprintf(stderr, " -d Generate INLINE/INLINE_ORIGIN records\n" ); |
| 54 | fprintf(stderr, " -r Do not handle inter-compilation " |
| 55 | "unit references\n" ); |
| 56 | fprintf(stderr, " -v Print all warnings to stderr\n" ); |
| 57 | fprintf(stderr, " -n <name> Use specified name for name of the object\n" ); |
| 58 | fprintf(stderr, " -o <os> Use specified name for the " |
| 59 | "operating system\n" ); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | int main(int argc, char** argv) { |
| 64 | if (argc < 2) |
| 65 | return usage(argv[0]); |
| 66 | bool = false; |
| 67 | bool cfi = true; |
| 68 | bool handle_inlines = false; |
| 69 | bool handle_inter_cu_refs = true; |
| 70 | bool log_to_stderr = false; |
| 71 | std::string obj_name; |
| 72 | const char* obj_os = "Linux" ; |
| 73 | int arg_index = 1; |
| 74 | while (arg_index < argc && strlen(argv[arg_index]) > 0 && |
| 75 | argv[arg_index][0] == '-') { |
| 76 | if (strcmp("-i" , argv[arg_index]) == 0) { |
| 77 | header_only = true; |
| 78 | } else if (strcmp("-c" , argv[arg_index]) == 0) { |
| 79 | cfi = false; |
| 80 | } else if (strcmp("-d" , argv[arg_index]) == 0) { |
| 81 | handle_inlines = true; |
| 82 | } else if (strcmp("-r" , argv[arg_index]) == 0) { |
| 83 | handle_inter_cu_refs = false; |
| 84 | } else if (strcmp("-v" , argv[arg_index]) == 0) { |
| 85 | log_to_stderr = true; |
| 86 | } else if (strcmp("-n" , argv[arg_index]) == 0) { |
| 87 | if (arg_index + 1 >= argc) { |
| 88 | fprintf(stderr, "Missing argument to -n\n" ); |
| 89 | return usage(argv[0]); |
| 90 | } |
| 91 | obj_name = argv[arg_index + 1]; |
| 92 | ++arg_index; |
| 93 | } else if (strcmp("-o" , argv[arg_index]) == 0) { |
| 94 | if (arg_index + 1 >= argc) { |
| 95 | fprintf(stderr, "Missing argument to -o\n" ); |
| 96 | return usage(argv[0]); |
| 97 | } |
| 98 | obj_os = argv[arg_index + 1]; |
| 99 | ++arg_index; |
| 100 | } else { |
| 101 | printf("2.4 %s\n" , argv[arg_index]); |
| 102 | return usage(argv[0]); |
| 103 | } |
| 104 | ++arg_index; |
| 105 | } |
| 106 | if (arg_index == argc) |
| 107 | return usage(argv[0]); |
| 108 | // Save stderr so it can be used below. |
| 109 | FILE* saved_stderr = fdopen(dup(fileno(stderr)), "w" ); |
| 110 | if (!log_to_stderr) { |
| 111 | if (freopen(_PATH_DEVNULL, "w" , stderr)) { |
| 112 | // If it fails, not a lot we can (or should) do. |
| 113 | // Add this brace section to silence gcc warnings. |
| 114 | } |
| 115 | } |
| 116 | const char* binary; |
| 117 | std::vector<string> debug_dirs; |
| 118 | binary = argv[arg_index]; |
| 119 | for (int debug_dir_index = arg_index + 1; |
| 120 | debug_dir_index < argc; |
| 121 | ++debug_dir_index) { |
| 122 | debug_dirs.push_back(argv[debug_dir_index]); |
| 123 | } |
| 124 | |
| 125 | if (obj_name.empty()) |
| 126 | obj_name = binary; |
| 127 | |
| 128 | if (header_only) { |
| 129 | if (!WriteSymbolFileHeader(binary, obj_name, obj_os, std::cout)) { |
| 130 | fprintf(saved_stderr, "Failed to process file.\n" ); |
| 131 | return 1; |
| 132 | } |
| 133 | } else { |
| 134 | SymbolData symbol_data = (handle_inlines ? INLINES : NO_DATA) | |
| 135 | (cfi ? CFI : NO_DATA) | SYMBOLS_AND_FILES; |
| 136 | google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs); |
| 137 | if (!WriteSymbolFile(binary, obj_name, obj_os, debug_dirs, options, |
| 138 | std::cout)) { |
| 139 | fprintf(saved_stderr, "Failed to write symbol file.\n" ); |
| 140 | return 1; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |