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 "
51                                  "unit references\n");
52   fprintf(stderr, "  -v          Print all warnings to stderr\n");
53   fprintf(stderr, "  -n <name>   Use specified name for name of the object\n");
54   fprintf(stderr, "  -o <os>     Use specified name for the "
55                                  "operating system\n");
56   return 1;
57 }
58 
main(int argc,char ** argv)59 int main(int argc, char** argv) {
60   if (argc < 2)
61     return usage(argv[0]);
62   bool header_only = false;
63   bool cfi = true;
64   bool handle_inter_cu_refs = true;
65   bool log_to_stderr = false;
66   std::string obj_name;
67   const char* obj_os = "Linux";
68   int arg_index = 1;
69   while (arg_index < argc && strlen(argv[arg_index]) > 0 &&
70          argv[arg_index][0] == '-') {
71     if (strcmp("-i", argv[arg_index]) == 0) {
72       header_only = true;
73     } else if (strcmp("-c", argv[arg_index]) == 0) {
74       cfi = false;
75     } else if (strcmp("-r", argv[arg_index]) == 0) {
76       handle_inter_cu_refs = false;
77     } else if (strcmp("-v", argv[arg_index]) == 0) {
78       log_to_stderr = true;
79     } else if (strcmp("-n", argv[arg_index]) == 0) {
80       if (arg_index + 1 >= argc) {
81         fprintf(stderr, "Missing argument to -n\n");
82         return usage(argv[0]);
83       }
84       obj_name = argv[arg_index + 1];
85       ++arg_index;
86     } else if (strcmp("-o", argv[arg_index]) == 0) {
87       if (arg_index + 1 >= argc) {
88         fprintf(stderr, "Missing argument to -o\n");
89         return usage(argv[0]);
90       }
91       obj_os = argv[arg_index + 1];
92       ++arg_index;
93     } else {
94       printf("2.4 %s\n", argv[arg_index]);
95       return usage(argv[0]);
96     }
97     ++arg_index;
98   }
99   if (arg_index == argc)
100     return usage(argv[0]);
101   // Save stderr so it can be used below.
102   FILE* saved_stderr = fdopen(dup(fileno(stderr)), "w");
103   if (!log_to_stderr) {
104     if (freopen(_PATH_DEVNULL, "w", stderr)) {
105       // If it fails, not a lot we can (or should) do.
106       // Add this brace section to silence gcc warnings.
107     }
108   }
109   const char* binary;
110   std::vector<string> debug_dirs;
111   binary = argv[arg_index];
112   for (int debug_dir_index = arg_index + 1;
113        debug_dir_index < argc;
114        ++debug_dir_index) {
115     debug_dirs.push_back(argv[debug_dir_index]);
116   }
117 
118   if (obj_name.empty())
119     obj_name = binary;
120 
121   if (header_only) {
122     if (!WriteSymbolFileHeader(binary, obj_name, obj_os, std::cout)) {
123       fprintf(saved_stderr, "Failed to process file.\n");
124       return 1;
125     }
126   } else {
127     SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI;
128     google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs);
129     if (!WriteSymbolFile(binary, obj_name, obj_os, debug_dirs, options,
130                          std::cout)) {
131       fprintf(saved_stderr, "Failed to write symbol file.\n");
132       return 1;
133     }
134   }
135 
136   return 0;
137 }
138