summaryrefslogtreecommitdiff
path: root/utils/gcore.c
blob: 3d72492c1785b42060bbc1fd7fe3aea122adf6a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* `gcore' program for GNU Hurd: write a core dump from a running process.
   Copyright (C) 1992,2001,2002 Free Software Foundation, Inc.
   Written by Roland McGrath.

This file is part of the GNU Hurd.

The GNU Hurd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU Hurd 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 for more details.

You should have received a copy of the GNU General Public License
along with the GNU Hurd; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <stdio.h>
#include <stdlib.h>
#include <hurd.h>
#include <hurd/crash.h>
#include <hurd/paths.h>
#include <fcntl.h>
#include <argp.h>
#include <error.h>
#include <version.h>

const char *argp_program_version = STANDARD_HURD_VERSION (gcore);

static const struct argp argp =
{
  NULL, NULL,
  "PID...",
  "Generate a core dump file from a running process"
  "\vFor each PID, a core dump file \"core.PID\" is written."
};

int
main (int argc, char **argv)
{
  int status = 0;
  file_t crashserv;
  int argi;

  argp_parse (&argp, argc, argv, 0, &argi, 0);

  crashserv = file_name_lookup (_SERVERS_CRASH, 0, 0);
  if (crashserv == MACH_PORT_NULL)
    error (1, errno, "cannot reach crash server: %s", _SERVERS_CRASH);

  for (; argi < argc; ++argi)
    {
      char *end;
      pid_t pid;
      task_t task;

      pid = strtol (argv[argi], &end, 10);
      if (end == argv[argi] || *end != '\0')
	{
	  error (0, 0, "cannot parse process ID: %s", argv[argi]);
	  status = 1;
	  continue;
	}

      task = pid2task ((pid_t) pid);
      if (task == MACH_PORT_NULL)
	{
	  error (0, errno, "pid2task: %d", pid);
	  status = 1;
	}
      else
	{
	  file_t file;
	  char *name = 0;
	  asprintf (&name, "core.%d", pid);

	  file = file_name_lookup (name, O_WRONLY|O_CREAT,
					  0600 &~ getumask ());
	  if (file == MACH_PORT_NULL)
	    {
	      error (0, errno, "cannot create %s", name);
	      status = 1;
	    }
	  else
	    {
	      error_t err = crash_dump_task (crashserv, task, file,
					     0, 0, 0, 0, 0, 0,
					     MACH_PORT_NULL,
					     MACH_MSG_TYPE_COPY_SEND);
	      mach_port_deallocate (mach_task_self (), file);
	      if (err)
		{
		  (void) remove (name);
		  error (0, err, "crash_dump_task on %d to %s", pid, name);
		  status = 1;
		}
	      free (name);
	    }
	}
      mach_port_deallocate (mach_task_self (), task);
    }

  return status;
}