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 <stdio.h>
31 #include <unistd.h>
32 
33 #include <cstring>
34 #include <iostream>
35 #include <string>
36 #include <vector>
37 
38 #include "common/linux/dump_symbols.h"
39 
40 using google_breakpad::WriteSymbolFile;
41 
usage(const char * self)42 int usage(const char* self) {
43   fprintf(stderr, "Usage: %s [OPTION] <binary-with-debugging-info> "
44           "[directories-for-debug-file]\n\n", self);
45   fprintf(stderr, "Options:\n");
46   fprintf(stderr, "  -c    Do not generate CFI section\n");
47   fprintf(stderr, "  -r    Do not handle inter-compilation unit references\n");
48   fprintf(stderr, "  -v    Verbose logging. Print all warnings to stderr\n");
49   return 1;
50 }
51 
main(int argc,char ** argv)52 int main(int argc, char **argv) {
53   if (argc < 2)
54     return usage(argv[0]);
55 
56   bool cfi = true;
57   bool handle_inter_cu_refs = true;
58   bool log_to_stderr = false;
59   int arg_index = 1;
60   while (arg_index < argc && strlen(argv[arg_index]) > 0 &&
61          argv[arg_index][0] == '-') {
62     if (strcmp("-c", argv[arg_index]) == 0) {
63       cfi = false;
64     } else if (strcmp("-r", argv[arg_index]) == 0) {
65       handle_inter_cu_refs = false;
66     } else if (strcmp("-v", argv[arg_index]) == 0) {
67       log_to_stderr = true;
68     } else {
69       return usage(argv[0]);
70     }
71     ++arg_index;
72   }
73   if (arg_index == argc)
74     return usage(argv[0]);
75 
76   // Save stderr so it can be used below.
77   FILE* saved_stderr = fdopen(dup(STDERR_FILENO), "w");
78   if (!log_to_stderr) {
79     freopen("/dev/null", "w", stderr);
80   }
81 
82   const char* binary;
83   std::vector<string> debug_dirs;
84   binary = argv[arg_index];
85   for (int debug_dir_index = arg_index + 1;
86        debug_dir_index < argc;
87        ++debug_dir_index) {
88     debug_dirs.push_back(argv[debug_dir_index]);
89   }
90 
91   SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI;
92   google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs);
93   if (!WriteSymbolFile(binary, debug_dirs, options, std::cout)) {
94     fprintf(saved_stderr, "Failed to write symbol file.\n");
95     return 1;
96   }
97 
98   return 0;
99 }
100