3 This file is part of Cygwin.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License (file COPYING.dumper) for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <sys/cygwin.h>
21 #include <cygwin/version.h>
29 DEFINE_ENUM_FLAG_OPERATORS(MINIDUMP_TYPE
);
34 /* Not yet in dbghelp.h */
35 #define MiniDumpWithModuleHeaders (static_cast<MINIDUMP_TYPE>(0x00080000))
36 #define MiniDumpFilterTriage (static_cast<MINIDUMP_TYPE>(0x00100000))
39 filter_minidump_type(MINIDUMP_TYPE dump_type
)
41 API_VERSION build_version
= { 6, 0, API_VERSION_NUMBER
, 0 };
42 API_VERSION
*v
= ImagehlpApiVersionEx(&build_version
);
45 printf ("dbghelp version %d.%d.%d.%d\n", v
->MajorVersion
,
46 v
->MinorVersion
, v
->Revision
, v
->Reserved
);
48 MINIDUMP_TYPE supported_types
= MiniDumpNormal
| MiniDumpWithDataSegs
49 | MiniDumpWithFullMemory
| MiniDumpWithHandleData
| MiniDumpFilterMemory
53 This mainly trial and error and guesswork, as the MSDN documentation only
54 says what version of "Debugging Tools for Windows" added these flags, but
55 doesn't actually tell us the dbghelp.dll version which was contained in that
56 (and seems to have errors as well)
59 if (v
->MajorVersion
>= 5)
60 supported_types
|= MiniDumpWithUnloadedModules
61 | MiniDumpWithIndirectlyReferencedMemory
| MiniDumpFilterModulePaths
62 | MiniDumpWithProcessThreadData
| MiniDumpWithPrivateReadWriteMemory
;
64 if (v
->MajorVersion
>= 6)
65 supported_types
|= MiniDumpWithoutOptionalData
| MiniDumpWithFullMemoryInfo
66 | MiniDumpWithThreadInfo
| MiniDumpWithCodeSegs
67 | MiniDumpWithoutAuxiliaryState
| MiniDumpWithFullAuxiliaryState
// seems to be documentation error that these two aren't listed as 'Not supported prior to 6.1'
68 | MiniDumpWithPrivateWriteCopyMemory
| MiniDumpIgnoreInaccessibleMemory
69 | MiniDumpWithTokenInformation
;
71 if ((v
->MajorVersion
*10 + v
->MinorVersion
) >= 62)
72 supported_types
|= MiniDumpWithModuleHeaders
| MiniDumpFilterTriage
; // seems to be documentation error that these two are listed as 'Not supported prior to 6.1'
75 printf ("supported MINIDUMP_TYPE flags 0x%x\n", supported_types
);
77 return (dump_type
& supported_types
);
81 minidump(DWORD pid
, MINIDUMP_TYPE dump_type
, const char *minidump_file
)
86 dump_file
= CreateFile(minidump_file
,
87 GENERIC_READ
| GENERIC_WRITE
,
91 FILE_FLAG_BACKUP_SEMANTICS
,
93 if (dump_file
== INVALID_HANDLE_VALUE
)
95 fprintf (stderr
, "error opening file\n");
99 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| PROCESS_TERMINATE
,
104 fprintf (stderr
, "error opening process\n");
108 BOOL success
= MiniDumpWriteDump(process
,
118 printf ("minidump created successfully\n");
122 fprintf (stderr
, "error creating minidump\n");
125 /* Unless nokill is given, behave like dumper and terminate the dumped
129 TerminateProcess(process
, 128 + 9);
130 WaitForSingleObject(process
, INFINITE
);
133 CloseHandle(process
);
134 CloseHandle(dump_file
);
137 static void __attribute__ ((__noreturn__
))
138 usage (FILE *stream
, int status
)
141 Usage: %s [OPTION] FILENAME WIN32PID\n\
143 Write minidump from WIN32PID to FILENAME.dmp\n\
145 -t, --type minidump type flags\n\
146 -n, --nokill don't terminate the dumped process\n\
147 -d, --verbose be verbose while dumping\n\
148 -h, --help output help information and exit\n\
149 -q, --quiet be quiet while dumping (default)\n\
150 -V, --version output version information and exit\n\
151 \n", program_invocation_short_name
);
155 struct option longopts
[] = {
156 {"type", required_argument
, NULL
, 't'},
157 {"nokill", no_argument
, NULL
, 'n'},
158 {"verbose", no_argument
, NULL
, 'd'},
159 {"help", no_argument
, NULL
, 'h'},
160 {"quiet", no_argument
, NULL
, 'q'},
161 {"version", no_argument
, 0, 'V'},
162 {0, no_argument
, NULL
, 0}
164 const char *opts
= "t:ndhqV";
169 printf ("minidumper (cygwin) %d.%d.%d\n"
170 "Minidump write for Cygwin\n"
171 "Copyright (C) 1999 - %s Cygwin Authors\n"
172 "This is free software; see the source for copying conditions. There is NO\n"
173 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
174 CYGWIN_VERSION_DLL_MAJOR
/ 1000,
175 CYGWIN_VERSION_DLL_MAJOR
% 1000,
176 CYGWIN_VERSION_DLL_MINOR
,
177 strrchr (__DATE__
, ' ') + 1);
181 main (int argc
, char **argv
)
186 BOOL default_dump_type
= TRUE
;
187 MINIDUMP_TYPE dump_type
= MiniDumpNormal
;
189 while ((opt
= getopt_long (argc
, argv
, opts
, longopts
, NULL
) ) != EOF
)
195 default_dump_type
= FALSE
;
196 dump_type
= (MINIDUMP_TYPE
)strtoul(optarg
, &endptr
, 0);
199 fprintf (stderr
, "syntax error in minidump type \"%s\" near character #%d.\n", optarg
, (int) (endptr
- optarg
));
219 fprintf (stderr
, "Try `%s --help' for more information.\n",
220 program_invocation_short_name
);
224 if (argv
&& *(argv
+ optind
) && *(argv
+ optind
+1))
226 ssize_t len
= cygwin_conv_path (CCP_POSIX_TO_WIN_A
| CCP_RELATIVE
,
227 *(argv
+ optind
), NULL
, 0);
228 char *win32_name
= (char *) alloca (len
);
229 cygwin_conv_path (CCP_POSIX_TO_WIN_A
| CCP_RELATIVE
, *(argv
+ optind
),
231 if ((p
= strrchr (win32_name
, '\\')))
236 pid
= strtoul (*(argv
+ optind
+ 1), NULL
, 10);
244 char *minidump_file
= (char *) malloc (strlen (p
) + sizeof (".dmp"));
247 fprintf (stderr
, "error allocating memory\n");
250 sprintf (minidump_file
, "%s.dmp", p
);
252 if (default_dump_type
)
254 dump_type
= MiniDumpWithHandleData
| MiniDumpWithFullMemoryInfo
255 | MiniDumpWithThreadInfo
| MiniDumpWithFullAuxiliaryState
256 | MiniDumpIgnoreInaccessibleMemory
| MiniDumpWithTokenInformation
257 | MiniDumpWithIndirectlyReferencedMemory
;
260 Only filter out unsupported dump_type flags if we are using the default
261 dump type, so that future dump_type flags can be explicitly used even if
262 we don't know about them
264 dump_type
= filter_minidump_type(dump_type
);
268 printf ("dumping process %u to %s using dump type flags 0x%x\n", (unsigned int)pid
, minidump_file
, (unsigned int)dump_type
);
270 minidump(pid
, dump_type
, minidump_file
);
272 free (minidump_file
);