3 Copyright 2014 Red Hat Inc.
5 This file is part of Cygwin.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License (file COPYING.dumper) for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <sys/cygwin.h>
23 #include <cygwin/version.h>
33 typedef DWORD MINIDUMP_TYPE
;
35 typedef BOOL (WINAPI
*MiniDumpWriteDump_type
)(
39 MINIDUMP_TYPE DumpType
,
40 CONST
void *ExceptionParam
,
41 CONST
void *UserStreamParam
,
42 CONST
void *allbackParam
);
45 minidump(DWORD pid
, MINIDUMP_TYPE dump_type
, const char *minidump_file
)
49 MiniDumpWriteDump_type MiniDumpWriteDump_fp
;
52 module
= LoadLibrary("dbghelp.dll");
55 fprintf (stderr
, "error loading DbgHelp\n");
59 MiniDumpWriteDump_fp
= (MiniDumpWriteDump_type
)GetProcAddress(module
, "MiniDumpWriteDump");
60 if (!MiniDumpWriteDump_fp
)
62 fprintf (stderr
, "error getting the address of MiniDumpWriteDump\n");
66 dump_file
= CreateFile(minidump_file
,
67 GENERIC_READ
| GENERIC_WRITE
,
71 FILE_FLAG_BACKUP_SEMANTICS
,
73 if (dump_file
== INVALID_HANDLE_VALUE
)
75 fprintf (stderr
, "error opening file\n");
79 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| PROCESS_TERMINATE
,
82 if (dump_file
== INVALID_HANDLE_VALUE
)
84 fprintf (stderr
, "error opening process\n");
88 BOOL success
= (*MiniDumpWriteDump_fp
)(process
,
98 printf ("minidump created successfully\n");
102 fprintf (stderr
, "error creating minidump\n");
105 /* Unless nokill is given, behave like dumper and terminate the dumped
109 TerminateProcess(process
, 128 + 9);
110 WaitForSingleObject(process
, INFINITE
);
113 CloseHandle(process
);
114 CloseHandle(dump_file
);
119 usage (FILE *stream
, int status
)
122 Usage: %s [OPTION] FILENAME WIN32PID\n\
124 Write minidump from WIN32PID to FILENAME.dmp\n\
126 -t, --type minidump type flags\n\
127 -n, --nokill don't terminate the dumped process\n\
128 -d, --verbose be verbose while dumping\n\
129 -h, --help output help information and exit\n\
130 -q, --quiet be quiet while dumping (default)\n\
131 -V, --version output version information and exit\n\
132 \n", program_invocation_short_name
);
136 struct option longopts
[] = {
137 {"type", required_argument
, NULL
, 't'},
138 {"nokill", no_argument
, NULL
, 'n'},
139 {"verbose", no_argument
, NULL
, 'd'},
140 {"help", no_argument
, NULL
, 'h'},
141 {"quiet", no_argument
, NULL
, 'q'},
142 {"version", no_argument
, 0, 'V'},
143 {0, no_argument
, NULL
, 0}
145 const char *opts
= "tndhqV";
150 printf ("minidumper (cygwin) %d.%d.%d\n"
151 "Minidump write for Cygwin\n"
152 "Copyright (C) 1999 - %s Red Hat, Inc.\n"
153 "This is free software; see the source for copying conditions. There is NO\n"
154 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
155 CYGWIN_VERSION_DLL_MAJOR
/ 1000,
156 CYGWIN_VERSION_DLL_MAJOR
% 1000,
157 CYGWIN_VERSION_DLL_MINOR
,
158 strrchr (__DATE__
, ' ') + 1);
162 main (int argc
, char **argv
)
167 MINIDUMP_TYPE dump_type
= 0; // MINIDUMP_NORMAL
169 while ((opt
= getopt_long (argc
, argv
, opts
, longopts
, NULL
) ) != EOF
)
175 dump_type
= strtoul(optarg
, &endptr
, 0);
178 fprintf (stderr
, "syntax error in minidump type \"%s\" near character #%d.\n", optarg
, (int) (endptr
- optarg
));
198 fprintf (stderr
, "Try `%s --help' for more information.\n",
199 program_invocation_short_name
);
203 if (argv
&& *(argv
+ optind
) && *(argv
+ optind
+1))
205 ssize_t len
= cygwin_conv_path (CCP_POSIX_TO_WIN_A
| CCP_RELATIVE
,
206 *(argv
+ optind
), NULL
, 0);
207 char *win32_name
= (char *) alloca (len
);
208 cygwin_conv_path (CCP_POSIX_TO_WIN_A
| CCP_RELATIVE
, *(argv
+ optind
),
210 if ((p
= strrchr (win32_name
, '\\')))
215 pid
= strtoul (*(argv
+ optind
+ 1), NULL
, 10);
223 char *minidump_file
= (char *) malloc (strlen (p
) + sizeof (".dmp"));
226 fprintf (stderr
, "error allocating memory\n");
229 sprintf (minidump_file
, "%s.dmp", p
);
232 printf ("dumping process %u to %s using dump type flags 0x%x\n", (unsigned int)pid
, minidump_file
, (unsigned int)dump_type
);
234 minidump(pid
, dump_type
, minidump_file
);
236 free (minidump_file
);