2000-08-25 03:03:12 +08:00
|
|
|
/* dumper.cc
|
|
|
|
|
|
|
|
Written by Egor Duda <deo@logos-m.ru>
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
This file is part of Cygwin.
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2007-07-25 03:08:23 +08:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2016-05-24 17:16:39 +08:00
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
2007-07-25 03:08:23 +08:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License (file COPYING.dumper) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2006-12-12 03:17:17 +08:00
|
|
|
#include <ansidecl.h>
|
2012-11-23 21:22:47 +08:00
|
|
|
#define PACKAGE
|
2006-12-21 17:59:04 +08:00
|
|
|
#include <bfd.h>
|
2000-08-25 03:03:12 +08:00
|
|
|
#include <elf/common.h>
|
|
|
|
#include <elf/external.h>
|
|
|
|
#include <sys/procfs.h>
|
|
|
|
#include <sys/cygwin.h>
|
2011-10-10 22:57:48 +08:00
|
|
|
#include <cygwin/version.h>
|
2000-08-25 03:03:12 +08:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdarg.h>
|
2011-10-10 22:57:48 +08:00
|
|
|
#include <errno.h>
|
2000-08-25 03:03:12 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-07-06 22:52:33 +08:00
|
|
|
#include <sys/param.h>
|
2000-08-25 03:03:12 +08:00
|
|
|
#include <windows.h>
|
2021-05-21 01:28:01 +08:00
|
|
|
#define PSAPI_VERSION 1
|
2020-07-29 22:19:13 +08:00
|
|
|
#include <psapi.h>
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
#include "dumper.h"
|
|
|
|
|
|
|
|
#define NOTE_NAME_SIZE 16
|
|
|
|
|
2020-02-27 02:48:51 +08:00
|
|
|
#ifdef bfd_get_section_size
|
|
|
|
/* for bfd < 2.34 */
|
|
|
|
#define get_section_name(abfd, sect) bfd_get_section_name (abfd, sect)
|
|
|
|
#define get_section_size(sect) bfd_get_section_size(sect)
|
|
|
|
#define set_section_size(abfd, sect, size) bfd_set_section_size(abfd, sect, size)
|
|
|
|
#define set_section_flags(abfd, sect, flags) bfd_set_section_flags(abfd, sect, flags)
|
|
|
|
#else
|
|
|
|
/* otherwise bfd >= 2.34 */
|
|
|
|
#define get_section_name(afbd, sect) bfd_section_name (sect)
|
|
|
|
#define get_section_size(sect) bfd_section_size(sect)
|
|
|
|
#define set_section_size(abfd, sect, size) bfd_set_section_size(sect, size)
|
|
|
|
#define set_section_flags(abfd, sect, flags) bfd_set_section_flags(sect, flags)
|
|
|
|
#endif
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
typedef struct _note_header
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
|
|
|
Elf_External_Note elf_note_header;
|
|
|
|
char name[NOTE_NAME_SIZE - 1]; /* external note contains first byte of data */
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
#ifdef __GNUC__
|
2000-10-28 13:00:00 +08:00
|
|
|
__attribute__ ((packed))
|
2000-08-25 03:03:12 +08:00
|
|
|
#endif
|
2000-10-28 13:00:00 +08:00
|
|
|
note_header;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2001-09-09 21:18:51 +08:00
|
|
|
BOOL verbose = FALSE;
|
2020-07-06 21:51:32 +08:00
|
|
|
BOOL nokill = FALSE;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2001-09-09 21:18:51 +08:00
|
|
|
int deb_printf (const char *format,...)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!verbose)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
va_list va;
|
2000-10-28 13:00:00 +08:00
|
|
|
va_start (va, format);
|
|
|
|
int ret_val = vprintf (format, va);
|
|
|
|
va_end (va);
|
2000-08-25 03:03:12 +08:00
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper::dumper (DWORD pid, DWORD tid, const char *file_name)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
this->file_name = strdup (file_name);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
this->pid = pid;
|
|
|
|
this->tid = tid;
|
|
|
|
core_bfd = NULL;
|
|
|
|
|
|
|
|
list = last = NULL;
|
|
|
|
|
|
|
|
status_section = NULL;
|
|
|
|
|
|
|
|
memory_num = module_num = thread_num = 0;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
|
|
|
|
FALSE, /* no inheritance */
|
|
|
|
pid);
|
|
|
|
if (!hProcess)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "Failed to open process #%u, error %ld\n",
|
|
|
|
(unsigned int) pid, (long) GetLastError ());
|
2000-08-25 03:03:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_core_dump ();
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
dumper_abort ();
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
2000-10-28 13:39:38 +08:00
|
|
|
dumper::~dumper ()
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
|
|
|
close ();
|
2000-10-28 13:00:00 +08:00
|
|
|
free (file_name);
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dumper::dumper_abort ()
|
|
|
|
{
|
|
|
|
close ();
|
2000-10-28 13:00:00 +08:00
|
|
|
unlink (file_name);
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dumper::close ()
|
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (core_bfd)
|
|
|
|
bfd_close (core_bfd);
|
|
|
|
if (hProcess)
|
|
|
|
CloseHandle (hProcess);
|
2000-08-25 03:03:12 +08:00
|
|
|
core_bfd = NULL;
|
|
|
|
hProcess = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dumper::sane ()
|
|
|
|
{
|
2020-07-04 22:57:31 +08:00
|
|
|
if (hProcess == NULL || core_bfd == NULL)
|
2000-10-28 13:00:00 +08:00
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-08-31 00:47:51 +08:00
|
|
|
void
|
2024-02-12 20:43:03 +08:00
|
|
|
print_section_name (bfd* abfd, asection* sect, void* obj)
|
2001-08-31 00:47:51 +08:00
|
|
|
{
|
2020-02-27 02:48:51 +08:00
|
|
|
deb_printf (" %s", get_section_name (abfd, sect));
|
2001-08-31 00:47:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dumper::print_core_section_list ()
|
|
|
|
{
|
|
|
|
deb_printf ("current sections:");
|
|
|
|
bfd_map_over_sections (core_bfd, &print_section_name, NULL);
|
|
|
|
deb_printf ("\n");
|
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
process_entity *
|
|
|
|
dumper::add_process_entity_to_list (process_entity_type type)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return NULL;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
process_entity *new_entity = (process_entity *) malloc (sizeof (process_entity));
|
|
|
|
if (new_entity == NULL)
|
|
|
|
return NULL;
|
2000-08-25 03:03:12 +08:00
|
|
|
new_entity->next = NULL;
|
|
|
|
new_entity->section = NULL;
|
2000-10-28 13:00:00 +08:00
|
|
|
if (last == NULL)
|
2000-08-25 03:03:12 +08:00
|
|
|
list = new_entity;
|
|
|
|
else
|
|
|
|
last->next = new_entity;
|
|
|
|
last = new_entity;
|
|
|
|
return new_entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper::add_thread (DWORD tid, HANDLE hThread)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
CONTEXT *pcontext;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
process_entity *new_entity = add_process_entity_to_list (pr_ent_thread);
|
|
|
|
if (new_entity == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
new_entity->type = pr_ent_thread;
|
|
|
|
thread_num++;
|
|
|
|
|
|
|
|
new_entity->u.thread.tid = tid;
|
|
|
|
new_entity->u.thread.hThread = hThread;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
pcontext = &(new_entity->u.thread.context);
|
2000-08-25 03:03:12 +08:00
|
|
|
pcontext->ContextFlags = CONTEXT_FULL | CONTEXT_FLOATING_POINT;
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!GetThreadContext (hThread, pcontext))
|
2001-09-14 23:07:31 +08:00
|
|
|
{
|
|
|
|
deb_printf ("Failed to read thread context (tid=%x), error %ld\n", tid, GetLastError ());
|
|
|
|
return 0;
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
deb_printf ("added thread %u\n", tid);
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-23 17:44:36 +08:00
|
|
|
dumper::add_mem_region (LPBYTE base, SIZE_T size)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (base == NULL || size == 0)
|
|
|
|
return 1; // just ignore empty regions
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
process_entity *new_entity = add_process_entity_to_list (pr_ent_memory);
|
|
|
|
if (new_entity == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
new_entity->type = pr_ent_memory;
|
|
|
|
memory_num++;
|
|
|
|
|
|
|
|
new_entity->u.memory.base = base;
|
|
|
|
new_entity->u.memory.size = size;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
deb_printf ("added memory region %p-%p\n", base, base + size);
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper::add_module (LPVOID base_address)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
char *module_name = psapi_get_module_name (hProcess, base_address);
|
2000-10-28 13:00:00 +08:00
|
|
|
if (module_name == NULL)
|
|
|
|
return 1;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
process_entity *new_entity = add_process_entity_to_list (pr_ent_module);
|
|
|
|
if (new_entity == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
new_entity->type = pr_ent_module;
|
|
|
|
module_num++;
|
|
|
|
|
|
|
|
new_entity->u.module.base_address = base_address;
|
|
|
|
new_entity->u.module.name = module_name;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
deb_printf ("added module %p %s\n", base_address, module_name);
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PAGE_BUFFER_SIZE 4096
|
|
|
|
|
2020-07-01 23:08:59 +08:00
|
|
|
void protect_dump(DWORD protect, char *buf)
|
|
|
|
{
|
|
|
|
const char *pt[10];
|
|
|
|
pt[0] = (protect & PAGE_READONLY) ? "RO " : "";
|
|
|
|
pt[1] = (protect & PAGE_READWRITE) ? "RW " : "";
|
|
|
|
pt[2] = (protect & PAGE_WRITECOPY) ? "WC " : "";
|
|
|
|
pt[3] = (protect & PAGE_EXECUTE) ? "EX " : "";
|
|
|
|
pt[4] = (protect & PAGE_EXECUTE_READ) ? "EXRO " : "";
|
|
|
|
pt[5] = (protect & PAGE_EXECUTE_READWRITE) ? "EXRW " : "";
|
|
|
|
pt[6] = (protect & PAGE_EXECUTE_WRITECOPY) ? "EXWC " : "";
|
|
|
|
pt[7] = (protect & PAGE_GUARD) ? "GRD " : "";
|
|
|
|
pt[8] = (protect & PAGE_NOACCESS) ? "NA " : "";
|
|
|
|
pt[9] = (protect & PAGE_NOCACHE) ? "NC " : "";
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
strcat (buf, pt[i]);
|
|
|
|
}
|
|
|
|
|
2020-07-29 22:19:13 +08:00
|
|
|
#define PSWSEI_ATTRIB_SHARED (0x1 << 15)
|
2020-07-08 03:54:27 +08:00
|
|
|
|
|
|
|
static BOOL
|
|
|
|
getRegionAttributes(HANDLE hProcess, LPVOID address, DWORD &attribs)
|
|
|
|
{
|
2020-07-29 22:19:13 +08:00
|
|
|
PSAPI_WORKING_SET_EX_INFORMATION pswsei = { address };
|
2020-07-08 03:54:27 +08:00
|
|
|
|
2020-07-29 22:19:13 +08:00
|
|
|
if (QueryWorkingSetEx(hProcess, &pswsei, sizeof(pswsei)))
|
2020-07-08 03:54:27 +08:00
|
|
|
{
|
2020-07-29 22:19:13 +08:00
|
|
|
attribs = pswsei.VirtualAttributes.Flags;
|
2020-07-08 03:54:27 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-07-29 22:19:13 +08:00
|
|
|
deb_printf("QueryWorkingSetEx failed status %08x\n", GetLastError());
|
2020-07-08 03:54:27 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
int
|
|
|
|
dumper::collect_memory_sections ()
|
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
LPBYTE current_page_address;
|
2020-07-01 21:00:53 +08:00
|
|
|
LPBYTE last_base = (LPBYTE) -1;
|
2013-04-23 17:44:36 +08:00
|
|
|
SIZE_T last_size = (SIZE_T) 0;
|
|
|
|
SIZE_T done;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
char mem_buf[PAGE_BUFFER_SIZE];
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (hProcess == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2020-07-01 21:00:53 +08:00
|
|
|
for (current_page_address = 0; current_page_address < (LPBYTE) -1;)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!VirtualQueryEx (hProcess, current_page_address, &mbi, sizeof (mbi)))
|
|
|
|
break;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
int skip_region_p = 0;
|
2020-07-01 23:08:59 +08:00
|
|
|
const char *disposition = "dumped";
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2020-07-08 03:54:27 +08:00
|
|
|
if (mbi.Type & MEM_IMAGE)
|
2020-07-05 21:42:59 +08:00
|
|
|
{
|
2020-07-08 03:54:27 +08:00
|
|
|
DWORD attribs = 0;
|
|
|
|
if (getRegionAttributes(hProcess, current_page_address, attribs))
|
|
|
|
{
|
2020-07-29 22:19:13 +08:00
|
|
|
if (attribs & PSWSEI_ATTRIB_SHARED)
|
2020-07-08 03:54:27 +08:00
|
|
|
{
|
|
|
|
skip_region_p = 1;
|
|
|
|
disposition = "skipped due to shared MEM_IMAGE";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
The undocumented MemoryWorkingSetExInformation is allegedly
|
|
|
|
supported since XP, so should always succeed, but if it fails,
|
|
|
|
fallback to looking at region protection.
|
|
|
|
*/
|
|
|
|
else if (!(mbi.Protect & (PAGE_EXECUTE_READWRITE | PAGE_READWRITE)))
|
|
|
|
{
|
|
|
|
skip_region_p = 1;
|
|
|
|
disposition = "skipped due to non-writeable MEM_IMAGE";
|
|
|
|
}
|
2020-07-05 21:42:59 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:08:59 +08:00
|
|
|
if (mbi.Protect & PAGE_NOACCESS)
|
|
|
|
{
|
|
|
|
skip_region_p = 1;
|
|
|
|
disposition = "skipped due to noaccess";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbi.Protect & PAGE_GUARD)
|
|
|
|
{
|
|
|
|
skip_region_p = 1;
|
|
|
|
disposition = "skipped due to guardpage";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbi.State != MEM_COMMIT)
|
|
|
|
{
|
|
|
|
skip_region_p = 1;
|
|
|
|
disposition = "skipped due to uncommited";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char buf[10 * 6];
|
|
|
|
protect_dump(mbi.Protect, buf);
|
|
|
|
|
|
|
|
const char *state = "";
|
|
|
|
const char *type = "";
|
|
|
|
|
|
|
|
if (mbi.State & MEM_COMMIT)
|
|
|
|
{
|
|
|
|
state = "COMMIT";
|
|
|
|
}
|
|
|
|
else if (mbi.State & MEM_FREE)
|
|
|
|
{
|
|
|
|
state = "FREE";
|
|
|
|
type = "FREE";
|
|
|
|
}
|
|
|
|
else if (mbi.State & MEM_RESERVE)
|
|
|
|
{
|
|
|
|
state = "RESERVE";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbi.Type & MEM_IMAGE)
|
|
|
|
{
|
|
|
|
type = "IMAGE";
|
|
|
|
}
|
|
|
|
else if (mbi.Type & MEM_MAPPED)
|
|
|
|
{
|
|
|
|
type = "MAPPED";
|
|
|
|
}
|
|
|
|
else if (mbi.Type & MEM_PRIVATE)
|
|
|
|
{
|
|
|
|
type = "PRIVATE";
|
|
|
|
}
|
|
|
|
|
|
|
|
deb_printf ("region 0x%016lx-0x%016lx (protect = %-8s, state = %-7s, type = %-7s, %s)\n",
|
|
|
|
current_page_address,
|
|
|
|
current_page_address + mbi.RegionSize,
|
|
|
|
buf, state, type, disposition);
|
|
|
|
}
|
2000-10-28 13:00:00 +08:00
|
|
|
|
|
|
|
if (!skip_region_p)
|
|
|
|
{
|
|
|
|
/* just to make sure that later we'll be able to read it.
|
|
|
|
According to MS docs either region is all-readable or
|
|
|
|
all-nonreadable */
|
|
|
|
if (!ReadProcessMemory (hProcess, current_page_address, mem_buf, sizeof (mem_buf), &done))
|
|
|
|
{
|
2001-09-14 23:07:31 +08:00
|
|
|
DWORD err = GetLastError ();
|
2020-07-01 23:08:59 +08:00
|
|
|
|
|
|
|
deb_printf ("warning: failed to read memory at %p-%p, error %ld.\n",
|
2013-04-23 17:44:36 +08:00
|
|
|
current_page_address,
|
|
|
|
current_page_address + mbi.RegionSize,
|
2020-07-01 23:08:59 +08:00
|
|
|
err);
|
2000-08-25 03:03:12 +08:00
|
|
|
skip_region_p = 1;
|
2000-10-28 13:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skip_region_p)
|
|
|
|
{
|
|
|
|
if (last_base + last_size == current_page_address)
|
|
|
|
last_size += mbi.RegionSize;
|
|
|
|
else
|
|
|
|
{
|
2020-07-04 22:57:31 +08:00
|
|
|
add_mem_region (last_base, last_size);
|
2000-10-28 13:00:00 +08:00
|
|
|
last_base = (LPBYTE) mbi.BaseAddress;
|
|
|
|
last_size = mbi.RegionSize;
|
|
|
|
}
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
else
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
2020-07-04 22:57:31 +08:00
|
|
|
add_mem_region (last_base, last_size);
|
2000-08-25 03:03:12 +08:00
|
|
|
last_base = NULL;
|
2000-10-28 13:00:00 +08:00
|
|
|
last_size = 0;
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
current_page_address += mbi.RegionSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dump last sections, if any */
|
2020-07-04 22:57:31 +08:00
|
|
|
add_mem_region (last_base, last_size);
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper::dump_memory_region (asection * to, process_mem_region * memory)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
SIZE_T size = memory->size;
|
|
|
|
SIZE_T todo;
|
|
|
|
SIZE_T done;
|
2000-08-25 03:03:12 +08:00
|
|
|
LPBYTE pos = memory->base;
|
|
|
|
DWORD sect_pos = 0;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (to == NULL || memory == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
char mem_buf[PAGE_BUFFER_SIZE];
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
while (size > 0)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2012-07-06 22:52:33 +08:00
|
|
|
todo = MIN (size, PAGE_BUFFER_SIZE);
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!ReadProcessMemory (hProcess, pos, mem_buf, todo, &done))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2001-09-14 23:07:31 +08:00
|
|
|
deb_printf ("Failed to read process memory at %x(%x), error %ld\n", pos, todo, GetLastError ());
|
2000-08-25 03:03:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size -= done;
|
|
|
|
pos += done;
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!bfd_set_section_contents (core_bfd, to, mem_buf, sect_pos, done))
|
|
|
|
{
|
|
|
|
bfd_perror ("writing memory region to bfd");
|
|
|
|
dumper_abort ();
|
|
|
|
return 0;
|
|
|
|
};
|
2000-08-25 03:03:12 +08:00
|
|
|
sect_pos += done;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper::dump_thread (asection * to, process_thread * thread)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (to == NULL || thread == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
win32_pstatus thread_pstatus;
|
|
|
|
|
|
|
|
note_header header;
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_putl32 (NOTE_NAME_SIZE, header.elf_note_header.namesz);
|
|
|
|
bfd_putl32 (sizeof (thread_pstatus), header.elf_note_header.descsz);
|
|
|
|
bfd_putl32 (NT_WIN32PSTATUS, header.elf_note_header.type);
|
2018-06-26 23:20:48 +08:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
2019-07-16 04:50:54 +08:00
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
2018-06-26 23:20:48 +08:00
|
|
|
strncpy (header.elf_note_header.name, "win32thread", NOTE_NAME_SIZE);
|
|
|
|
#pragma GCC diagnostic pop
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
thread_pstatus.data_type = NOTE_INFO_THREAD;
|
|
|
|
thread_pstatus.data.thread_info.tid = thread->tid;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (tid == 0)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
|
|
|
/* this is a special case. we don't know, which thread
|
2004-10-25 23:49:36 +08:00
|
|
|
was active when exception occured, so let's blame
|
|
|
|
the first one */
|
2000-10-28 13:00:00 +08:00
|
|
|
thread_pstatus.data.thread_info.is_active_thread = TRUE;
|
|
|
|
tid = (DWORD) - 1;
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
2000-10-28 13:00:00 +08:00
|
|
|
else if (tid > 0 && thread->tid == tid)
|
2000-08-25 03:03:12 +08:00
|
|
|
thread_pstatus.data.thread_info.is_active_thread = TRUE;
|
|
|
|
else
|
|
|
|
thread_pstatus.data.thread_info.is_active_thread = FALSE;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
memcpy (&(thread_pstatus.data.thread_info.thread_context),
|
|
|
|
&(thread->context),
|
|
|
|
sizeof (thread->context));
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!bfd_set_section_contents (core_bfd, to, &header,
|
|
|
|
0,
|
|
|
|
sizeof (header)) ||
|
|
|
|
!bfd_set_section_contents (core_bfd, to, &thread_pstatus,
|
|
|
|
sizeof (header),
|
|
|
|
sizeof (thread_pstatus)))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_perror ("writing thread info to bfd");
|
2000-08-25 03:03:12 +08:00
|
|
|
dumper_abort ();
|
|
|
|
return 0;
|
2000-10-28 13:00:00 +08:00
|
|
|
};
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper::dump_module (asection * to, process_module * module)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (to == NULL || module == NULL)
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
struct win32_pstatus *module_pstatus_ptr;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
int note_length = sizeof (struct win32_pstatus) + strlen (module->name);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
char *buf = (char *) malloc (note_length);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!buf)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
fprintf (stderr, "Error alloating memory. Dumping aborted.\n");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto out;
|
2000-10-28 13:00:00 +08:00
|
|
|
};
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
module_pstatus_ptr = (struct win32_pstatus *) buf;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
note_header header;
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_putl32 (NOTE_NAME_SIZE, header.elf_note_header.namesz);
|
|
|
|
bfd_putl32 (note_length, header.elf_note_header.descsz);
|
|
|
|
bfd_putl32 (NT_WIN32PSTATUS, header.elf_note_header.type);
|
2018-06-26 23:20:48 +08:00
|
|
|
#pragma GCC diagnostic push
|
2021-08-27 04:15:49 +08:00
|
|
|
#pragma GCC diagnostic warning "-Wstringop-overflow=1"
|
2019-07-16 04:50:54 +08:00
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
2018-06-26 23:20:48 +08:00
|
|
|
strncpy (header.elf_note_header.name, "win32module", NOTE_NAME_SIZE);
|
|
|
|
#pragma GCC diagnostic pop
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2020-06-29 23:45:26 +08:00
|
|
|
module_pstatus_ptr->data_type = NOTE_INFO_MODULE64;
|
2000-08-25 03:03:12 +08:00
|
|
|
module_pstatus_ptr->data.module_info.base_address = module->base_address;
|
2000-10-28 13:00:00 +08:00
|
|
|
module_pstatus_ptr->data.module_info.module_name_size = strlen (module->name) + 1;
|
|
|
|
strcpy (module_pstatus_ptr->data.module_info.module_name, module->name);
|
|
|
|
|
|
|
|
if (!bfd_set_section_contents (core_bfd, to, &header,
|
|
|
|
0,
|
|
|
|
sizeof (header)) ||
|
|
|
|
!bfd_set_section_contents (core_bfd, to, module_pstatus_ptr,
|
|
|
|
sizeof (header),
|
|
|
|
note_length))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_perror ("writing module info to bfd");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto out;
|
|
|
|
};
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
out:
|
2000-10-28 13:00:00 +08:00
|
|
|
if (buf)
|
|
|
|
free (buf);
|
2000-08-25 03:03:12 +08:00
|
|
|
dumper_abort ();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dumper::collect_process_information ()
|
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!DebugActiveProcess (pid))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "Cannot attach to process #%u, error %ld",
|
|
|
|
(unsigned int) pid, (long) GetLastError ());
|
2000-08-25 03:03:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_EVENT current_event;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2020-07-06 22:10:40 +08:00
|
|
|
if (!WaitForDebugEvent (¤t_event, INFINITE))
|
2000-10-28 13:00:00 +08:00
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2001-08-31 00:47:51 +08:00
|
|
|
deb_printf ("got debug event %d\n", current_event.dwDebugEventCode);
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
switch (current_event.dwDebugEventCode)
|
|
|
|
{
|
|
|
|
case CREATE_THREAD_DEBUG_EVENT:
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!add_thread (current_event.dwThreadId,
|
|
|
|
current_event.u.CreateThread.hThread))
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE_PROCESS_DEBUG_EVENT:
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!add_module (current_event.u.CreateProcessInfo.lpBaseOfImage) ||
|
|
|
|
!add_thread (current_event.dwThreadId,
|
|
|
|
current_event.u.CreateProcessInfo.hThread))
|
|
|
|
goto failed;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXIT_PROCESS_DEBUG_EVENT:
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
deb_printf ("debugee quits");
|
|
|
|
ContinueDebugEvent (current_event.dwProcessId,
|
|
|
|
current_event.dwThreadId,
|
|
|
|
DBG_CONTINUE);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOAD_DLL_DEBUG_EVENT:
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!add_module (current_event.u.LoadDll.lpBaseOfDll))
|
|
|
|
goto failed;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXCEPTION_DEBUG_EVENT:
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
collect_memory_sections ();
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
/* got all info. time to dump */
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!prepare_core_dump ())
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
fprintf (stderr, "Failed to prepare core dump\n");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
};
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!write_core_dump ())
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
fprintf (stderr, "Failed to write core dump\n");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
};
|
|
|
|
|
2020-07-06 22:10:40 +08:00
|
|
|
/* We're done */
|
|
|
|
goto failed;
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
ContinueDebugEvent (current_event.dwProcessId,
|
|
|
|
current_event.dwThreadId,
|
|
|
|
DBG_CONTINUE);
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
2020-07-06 21:51:32 +08:00
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
failed:
|
2020-07-06 21:51:32 +08:00
|
|
|
if (nokill)
|
|
|
|
{
|
|
|
|
if (!DebugActiveProcessStop (pid))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Cannot detach from process #%u, error %ld",
|
|
|
|
(unsigned int) pid, (long) GetLastError ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Otherwise, the debuggee is terminated when this process exits
|
|
|
|
(as DebugSetProcessKillOnExit() defaults to TRUE) */
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dumper::init_core_dump ()
|
|
|
|
{
|
|
|
|
bfd_init ();
|
|
|
|
|
2020-06-29 21:36:00 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
const char *target = "elf64-x86-64";
|
|
|
|
#else
|
2022-10-28 17:37:02 +08:00
|
|
|
#error unimplemented for this target
|
2020-06-29 21:36:00 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
core_bfd = bfd_openw (file_name, target);
|
2000-10-28 13:00:00 +08:00
|
|
|
if (core_bfd == NULL)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_perror ("opening bfd");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!bfd_set_format (core_bfd, bfd_core))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_perror ("setting bfd format");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:36:00 +08:00
|
|
|
if (!bfd_set_arch_mach (core_bfd, bfd_arch_i386, 0 /* = default */))
|
2001-04-19 22:22:30 +08:00
|
|
|
{
|
|
|
|
bfd_perror ("setting bfd architecture");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
dumper_abort ();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dumper::prepare_core_dump ()
|
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
int sect_no = 0;
|
2000-10-28 13:00:00 +08:00
|
|
|
char sect_name[50];
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
flagword sect_flags;
|
2013-04-23 17:44:36 +08:00
|
|
|
SIZE_T sect_size;
|
2000-08-25 03:03:12 +08:00
|
|
|
bfd_vma sect_vma;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
asection *new_section;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
for (process_entity * p = list; p != NULL; p = p->next)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
|
|
|
sect_no++;
|
|
|
|
|
2007-05-29 16:08:06 +08:00
|
|
|
unsigned long phdr_type = PT_LOAD;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case pr_ent_memory:
|
|
|
|
sprintf (sect_name, ".mem/%u", sect_no);
|
|
|
|
sect_flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
|
|
|
|
sect_size = p->u.memory.size;
|
|
|
|
sect_vma = (bfd_vma) (p->u.memory.base);
|
2007-05-29 16:08:06 +08:00
|
|
|
phdr_type = PT_LOAD;
|
2000-10-28 13:00:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case pr_ent_thread:
|
|
|
|
sprintf (sect_name, ".note/%u", sect_no);
|
|
|
|
sect_flags = SEC_HAS_CONTENTS | SEC_LOAD;
|
|
|
|
sect_size = sizeof (note_header) + sizeof (struct win32_pstatus);
|
|
|
|
sect_vma = 0;
|
2007-05-29 16:08:06 +08:00
|
|
|
phdr_type = PT_NOTE;
|
2000-08-25 03:03:12 +08:00
|
|
|
break;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
case pr_ent_module:
|
|
|
|
sprintf (sect_name, ".note/%u", sect_no);
|
|
|
|
sect_flags = SEC_HAS_CONTENTS | SEC_LOAD;
|
|
|
|
sect_size = sizeof (note_header) + sizeof (struct win32_pstatus) +
|
|
|
|
(bfd_size_type) (strlen (p->u.module.name));
|
|
|
|
sect_vma = 0;
|
2007-05-29 16:08:06 +08:00
|
|
|
phdr_type = PT_NOTE;
|
2000-10-28 13:00:00 +08:00
|
|
|
break;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
default:
|
2000-08-25 03:03:12 +08:00
|
|
|
continue;
|
2000-10-28 13:00:00 +08:00
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (p->type == pr_ent_module && status_section != NULL)
|
|
|
|
{
|
2020-02-27 02:48:51 +08:00
|
|
|
if (!set_section_size (core_bfd,
|
|
|
|
status_section,
|
|
|
|
(get_section_size (status_section)
|
|
|
|
+ sect_size)))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
bfd_perror ("resizing status section");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
};
|
2000-10-28 13:00:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
deb_printf ("creating section (type%u) %s(%u), flags=%08x\n",
|
|
|
|
p->type, sect_name, sect_size, sect_flags);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2001-08-31 00:47:51 +08:00
|
|
|
bfd_set_error (bfd_error_no_error);
|
2000-10-28 13:00:00 +08:00
|
|
|
char *buf = strdup (sect_name);
|
|
|
|
new_section = bfd_make_section (core_bfd, buf);
|
2001-08-31 00:47:51 +08:00
|
|
|
if (new_section == NULL)
|
|
|
|
{
|
|
|
|
if (bfd_get_error () == bfd_error_no_error)
|
|
|
|
fprintf (stderr, "error creating new section (%s), section already exists.\n", buf);
|
|
|
|
else
|
|
|
|
bfd_perror ("creating section");
|
|
|
|
goto failed;
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2020-02-27 02:48:51 +08:00
|
|
|
if (!set_section_flags (core_bfd, new_section, sect_flags) ||
|
|
|
|
!set_section_size (core_bfd, new_section, sect_size))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2001-08-31 00:47:51 +08:00
|
|
|
bfd_perror ("setting section attributes");
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
};
|
|
|
|
|
|
|
|
new_section->vma = sect_vma;
|
2007-05-29 16:08:06 +08:00
|
|
|
new_section->lma = 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
new_section->output_section = new_section;
|
|
|
|
new_section->output_offset = 0;
|
|
|
|
p->section = new_section;
|
2007-05-29 16:08:06 +08:00
|
|
|
int section_count = 1;
|
|
|
|
|
|
|
|
bfd_boolean filehdr = 0;
|
|
|
|
bfd_boolean phdrs = 0;
|
|
|
|
|
|
|
|
bfd_vma at = 0;
|
|
|
|
bfd_boolean valid_at = 0;
|
|
|
|
|
|
|
|
flagword flags = 0;
|
|
|
|
bfd_boolean valid_flags = 1;
|
|
|
|
|
|
|
|
if (p->type == pr_ent_memory)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
if (!VirtualQueryEx (hProcess, (LPVOID)sect_vma, &mbi, sizeof (mbi)))
|
|
|
|
{
|
|
|
|
bfd_perror ("getting mem region flags");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
DWORD protect;
|
|
|
|
flagword flags;
|
|
|
|
} mappings[] =
|
|
|
|
{
|
|
|
|
{ PAGE_READONLY, PF_R },
|
|
|
|
{ PAGE_READWRITE, PF_R | PF_W },
|
|
|
|
{ PAGE_WRITECOPY, PF_W },
|
|
|
|
{ PAGE_EXECUTE, PF_X },
|
|
|
|
{ PAGE_EXECUTE_READ, PF_X | PF_R },
|
|
|
|
{ PAGE_EXECUTE_READWRITE, PF_X | PF_R | PF_W },
|
|
|
|
{ PAGE_EXECUTE_WRITECOPY, PF_X | PF_W }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0;
|
|
|
|
i < sizeof (mappings) / sizeof (mappings[0]);
|
|
|
|
i++)
|
|
|
|
if ((mbi.Protect & mappings[i].protect) != 0)
|
|
|
|
flags |= mappings[i].flags;
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2007-05-29 16:08:06 +08:00
|
|
|
if (!bfd_record_phdr (core_bfd, phdr_type,
|
|
|
|
valid_flags, flags,
|
|
|
|
valid_at, at,
|
|
|
|
filehdr, phdrs,
|
|
|
|
section_count, &new_section))
|
|
|
|
{
|
|
|
|
bfd_perror ("recording program headers");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
dumper_abort ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dumper::write_core_dump ()
|
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!sane ())
|
|
|
|
return 0;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
for (process_entity * p = list; p != NULL; p = p->next)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (p->section == NULL)
|
|
|
|
continue;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
deb_printf ("writing section type=%u base=%p size=%p flags=%08x\n",
|
2000-10-28 13:00:00 +08:00
|
|
|
p->type,
|
|
|
|
p->section->vma,
|
2020-02-27 02:48:51 +08:00
|
|
|
get_section_size (p->section),
|
2000-10-28 13:00:00 +08:00
|
|
|
p->section->flags);
|
|
|
|
|
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case pr_ent_memory:
|
|
|
|
dump_memory_region (p->section, &(p->u.memory));
|
2000-08-25 03:03:12 +08:00
|
|
|
break;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
case pr_ent_thread:
|
|
|
|
dump_thread (p->section, &(p->u.thread));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case pr_ent_module:
|
|
|
|
dump_module (p->section, &(p->u.module));
|
|
|
|
break;
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
default:
|
2000-08-25 03:03:12 +08:00
|
|
|
continue;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:29:43 +08:00
|
|
|
static void __attribute__ ((__noreturn__))
|
2002-05-08 09:55:56 +08:00
|
|
|
usage (FILE *stream, int status)
|
|
|
|
{
|
|
|
|
fprintf (stream, "\
|
2011-10-10 22:57:48 +08:00
|
|
|
Usage: %s [OPTION] FILENAME WIN32PID\n\
|
|
|
|
\n\
|
2002-05-08 09:55:56 +08:00
|
|
|
Dump core from WIN32PID to FILENAME.core\n\
|
2003-04-27 05:52:03 +08:00
|
|
|
\n\
|
2020-07-06 21:51:32 +08:00
|
|
|
-n, --nokill don't terminate the dumped process\n\
|
2002-05-08 09:55:56 +08:00
|
|
|
-d, --verbose be verbose while dumping\n\
|
|
|
|
-h, --help output help information and exit\n\
|
|
|
|
-q, --quiet be quiet while dumping (default)\n\
|
2011-10-10 22:57:48 +08:00
|
|
|
-V, --version output version information and exit\n\
|
|
|
|
\n", program_invocation_short_name);
|
2002-05-08 09:55:56 +08:00
|
|
|
exit (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct option longopts[] = {
|
2020-07-06 21:51:32 +08:00
|
|
|
{"nokill", no_argument, NULL, 'n'},
|
2002-05-08 09:55:56 +08:00
|
|
|
{"verbose", no_argument, NULL, 'd'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
2011-10-10 22:57:48 +08:00
|
|
|
{"version", no_argument, 0, 'V'},
|
2002-05-08 09:55:56 +08:00
|
|
|
{0, no_argument, NULL, 0}
|
|
|
|
};
|
2020-07-06 21:51:32 +08:00
|
|
|
const char *opts = "ndhqV";
|
2002-05-08 09:55:56 +08:00
|
|
|
|
2004-10-25 23:49:36 +08:00
|
|
|
static void
|
2002-05-08 09:55:56 +08:00
|
|
|
print_version ()
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
printf ("dumper (cygwin) %d.%d.%d\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"Core Dumper for Cygwin\n"
|
2016-05-24 17:16:39 +08:00
|
|
|
"Copyright (C) 1999 - %s Cygwin Authors\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 07:39:47 +08:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
main (int argc, char **argv)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
|
|
|
int opt;
|
2000-10-28 13:39:38 +08:00
|
|
|
const char *p = "";
|
2000-08-25 03:03:12 +08:00
|
|
|
DWORD pid;
|
|
|
|
|
2011-10-10 22:57:48 +08:00
|
|
|
while ((opt = getopt_long (argc, argv, opts, longopts, NULL) ) != EOF)
|
2000-08-25 03:03:12 +08:00
|
|
|
switch (opt)
|
|
|
|
{
|
2020-07-06 21:51:32 +08:00
|
|
|
case 'n':
|
|
|
|
nokill = TRUE;
|
|
|
|
break;
|
2000-08-25 03:03:12 +08:00
|
|
|
case 'd':
|
|
|
|
verbose = TRUE;
|
|
|
|
break;
|
2001-09-09 21:18:51 +08:00
|
|
|
case 'q':
|
|
|
|
verbose = FALSE;
|
|
|
|
break;
|
2002-05-08 09:55:56 +08:00
|
|
|
case 'h':
|
|
|
|
usage (stdout, 0);
|
2011-10-10 22:57:48 +08:00
|
|
|
case 'V':
|
|
|
|
print_version ();
|
|
|
|
exit (0);
|
2001-09-09 21:18:51 +08:00
|
|
|
default:
|
2011-10-10 22:57:48 +08:00
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
exit (1);
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
2001-09-09 21:18:51 +08:00
|
|
|
if (argv && *(argv + optind) && *(argv + optind +1))
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2008-03-12 20:47:09 +08:00
|
|
|
ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_RELATIVE,
|
|
|
|
*(argv + optind), NULL, 0);
|
|
|
|
char *win32_name = (char *) alloca (len);
|
|
|
|
cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_RELATIVE, *(argv + optind),
|
|
|
|
win32_name, len);
|
2001-09-09 21:18:51 +08:00
|
|
|
if ((p = strrchr (win32_name, '\\')))
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = win32_name;
|
2001-09-13 23:06:25 +08:00
|
|
|
pid = strtoul (*(argv + optind + 1), NULL, 10);
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-08 09:55:56 +08:00
|
|
|
usage (stderr, 1);
|
2000-08-25 03:03:12 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-09-09 21:18:51 +08:00
|
|
|
char *core_file = (char *) malloc (strlen (p) + sizeof (".core"));
|
|
|
|
if (!core_file)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "error allocating memory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sprintf (core_file, "%s.core", p);
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
DWORD tid = 0;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (verbose)
|
2013-04-23 17:44:36 +08:00
|
|
|
printf ("dumping process #%u to %s\n", (unsigned int) pid, core_file);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
dumper d (pid, tid, core_file);
|
|
|
|
if (!d.sane ())
|
2000-08-25 03:03:12 +08:00
|
|
|
return -1;
|
|
|
|
d.collect_process_information ();
|
2000-10-28 13:00:00 +08:00
|
|
|
free (core_file);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|