1 // Copyright 2006 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 // Windows utility to dump the line number data from a pdb file to
30 // a text-based format that we can use from the minidump processor.
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h> // Must come first
34 #endif
35
36 #include <stdio.h>
37 #include <wchar.h>
38
39 #include <memory>
40 #include <string>
41
42 #include "common/windows/pdb_source_line_writer.h"
43 #include "common/windows/pe_source_line_writer.h"
44
45 using google_breakpad::PDBSourceLineWriter;
46 using google_breakpad::PESourceLineWriter;
47 using std::unique_ptr;
48 using std::wstring;
49
usage(const wchar_t * self)50 int usage(const wchar_t* self) {
51 fprintf(stderr, "Usage: %ws [--pe] [--i] <file.[pdb|exe|dll]>\n", self);
52 fprintf(stderr, "Options:\n");
53 fprintf(stderr,
54 "--pe:\tRead debugging information from PE file and do "
55 "not attempt to locate matching PDB file.\n"
56 "\tThis is only supported for PE32+ (64 bit) PE files.\n");
57 fprintf(stderr,
58 "--i:\tOutput INLINE/INLINE_ORIGIN record\n"
59 "\tThis cannot be used with [--pe].\n");
60 return 1;
61 }
62
wmain(int argc,wchar_t ** argv)63 int wmain(int argc, wchar_t** argv) {
64 bool success = false;
65 bool pe = false;
66 bool handle_inline = false;
67 int arg_index = 1;
68 while (arg_index < argc && wcslen(argv[arg_index]) > 0 &&
69 wcsncmp(L"--", argv[arg_index], 2) == 0) {
70 if (wcscmp(L"--pe", argv[arg_index]) == 0) {
71 pe = true;
72 } else if (wcscmp(L"--i", argv[arg_index]) == 0) {
73 handle_inline = true;
74 }
75 ++arg_index;
76 }
77
78 if ((pe && handle_inline) || arg_index == argc) {
79 usage(argv[0]);
80 return 1;
81 }
82
83 wchar_t* file_path = argv[arg_index];
84 if (pe) {
85 PESourceLineWriter pe_writer(file_path);
86 success = pe_writer.WriteSymbols(stdout);
87 } else {
88 PDBSourceLineWriter pdb_writer(handle_inline);
89 if (!pdb_writer.Open(wstring(file_path), PDBSourceLineWriter::ANY_FILE)) {
90 fprintf(stderr, "Open failed.\n");
91 return 1;
92 }
93 success = pdb_writer.WriteSymbols(stdout);
94 }
95
96 if (!success) {
97 fprintf(stderr, "WriteSymbols failed.\n");
98 return 1;
99 }
100
101 return 0;
102 }
103