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