xref: /aosp_15_r20/external/google-breakpad/src/tools/linux/core2md/core2md.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2012 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 // core2md.cc: A utility to convert an ELF core file to a minidump file.
30 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>  // Must come first
33 #endif
34 
35 #include <stdio.h>
36 
37 #include "client/linux/minidump_writer/minidump_writer.h"
38 #include "client/linux/minidump_writer/linux_core_dumper.h"
39 #include "common/path_helper.h"
40 
41 using google_breakpad::AppMemoryList;
42 using google_breakpad::MappingList;
43 using google_breakpad::LinuxCoreDumper;
44 
ShowUsage(const char * argv0)45 static int ShowUsage(const char* argv0) {
46   fprintf(stderr, "Usage: %s <core file> <procfs dir> <output>\n",
47           google_breakpad::BaseName(argv0).c_str());
48   return 1;
49 }
50 
WriteMinidumpFromCore(const char * filename,const char * core_path,const char * procfs_override)51 bool WriteMinidumpFromCore(const char* filename,
52                            const char* core_path,
53                            const char* procfs_override) {
54   MappingList mappings;
55   AppMemoryList memory_list;
56   LinuxCoreDumper dumper(0, core_path, procfs_override);
57   return google_breakpad::WriteMinidump(filename, mappings, memory_list,
58                                         &dumper);
59 }
60 
main(int argc,char * argv[])61 int main(int argc, char *argv[]) {
62   if (argc != 4) {
63     return ShowUsage(argv[0]);
64   }
65 
66   const char* core_file = argv[1];
67   const char* procfs_dir = argv[2];
68   const char* minidump_file = argv[3];
69   if (!WriteMinidumpFromCore(minidump_file,
70                              core_file,
71                              procfs_dir)) {
72     perror("core2md: Unable to generate minidump");
73     return 1;
74   }
75 
76   return 0;
77 }
78