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
41 using google_breakpad::WriteSymbolFile;
42 using google_breakpad::WriteSymbolFileHeader;
43
usage(const char * self)44 int usage(const char* self) {
45 fprintf(stderr, "Usage: %s [OPTION] <binary-with-debugging-info> "
46 "[directories-for-debug-file]\n\n", self);
47 fprintf(stderr, "Options:\n");
48 fprintf(stderr, " -i: Output module header information only.\n");
49 fprintf(stderr, " -c Do not generate CFI section\n");
50 fprintf(stderr, " -r Do not handle inter-compilation unit references\n");
51 fprintf(stderr, " -v Print all warnings to stderr\n");
52 return 1;
53 }
54
main(int argc,char ** argv)55 int main(int argc, char **argv) {
56 if (argc < 2)
57 return usage(argv[0]);
58 bool header_only = false;
59 bool cfi = true;
60 bool handle_inter_cu_refs = true;
61 bool log_to_stderr = false;
62 int arg_index = 1;
63 while (arg_index < argc && strlen(argv[arg_index]) > 0 &&
64 argv[arg_index][0] == '-') {
65 if (strcmp("-i", argv[arg_index]) == 0) {
66 header_only = true;
67 } else if (strcmp("-c", argv[arg_index]) == 0) {
68 cfi = false;
69 } else if (strcmp("-r", argv[arg_index]) == 0) {
70 handle_inter_cu_refs = false;
71 } else if (strcmp("-v", argv[arg_index]) == 0) {
72 log_to_stderr = true;
73 } else {
74 printf("2.4 %s\n", argv[arg_index]);
75 return usage(argv[0]);
76 }
77 ++arg_index;
78 }
79 if (arg_index == argc)
80 return usage(argv[0]);
81 // Save stderr so it can be used below.
82 FILE* saved_stderr = fdopen(dup(fileno(stderr)), "w");
83 if (!log_to_stderr) {
84 if (freopen(_PATH_DEVNULL, "w", stderr)) {
85 // If it fails, not a lot we can (or should) do.
86 // Add this brace section to silence gcc warnings.
87 }
88 }
89 const char* binary;
90 std::vector<string> debug_dirs;
91 binary = argv[arg_index];
92 for (int debug_dir_index = arg_index + 1;
93 debug_dir_index < argc;
94 ++debug_dir_index) {
95 debug_dirs.push_back(argv[debug_dir_index]);
96 }
97
98 if (header_only) {
99 if (!WriteSymbolFileHeader(binary, std::cout)) {
100 fprintf(saved_stderr, "Failed to process file.\n");
101 return 1;
102 }
103 } else {
104 SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI;
105 google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs);
106 if (!WriteSymbolFile(binary, debug_dirs, options, std::cout)) {
107 fprintf(saved_stderr, "Failed to write symbol file.\n");
108 return 1;
109 }
110 }
111
112 return 0;
113 }
114