1/* A client to make GDB return to command level in Mach 3.
2   Copyright 1992, 1993, 1994, 2000 Free Software Foundation, Inc.
3
4   This file is part of GDB.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.  */
20
21/* Authors: Jukka Virtanen <jtv@hut.fi> and Peter Stout <pds@cs.cmu.edu>.
22
23   A simple client to make GDB (versions 4.4 and later) on Mach 3 return
24   to the command level when it is waiting for the inferior to stop.
25
26   Actions: Lookup the send right to the GDB message port from the
27   NetMsgServer.
28
29   Send an asynchronous message with msgh_id
30   GDB_MESSAGE_ID_STOP to that port.
31 */
32
33#include <stdio.h>
34
35#include "defs.h"
36
37#include <mach.h>
38#include <mach/message.h>
39#include <mach_error.h>
40#include <servers/netname.h>
41#include <servers/netname_defs.h>
42
43void
44main (int argc, char **argv)
45{
46  kern_return_t kr;
47  mach_msg_header_t msg;
48  mach_port_t gdb_port;
49  char *host;
50  char *name;
51
52  if (argc == 1)
53    argv[argc++] = GDB_DEF_NAME;
54
55  if (argc != 2)
56    {
57      fprintf (stderr, "Usage : %s <GDB name>\n", argv[0]);
58      exit (1);
59    }
60
61  /* Allow the user to specify a remote host.  */
62  host = strchr (argv[1], '@');
63  if (host)
64    *(host++) = '\0';
65  else
66    host = (char *) "";
67
68  name = malloc (strlen (argv[1]) + sizeof (GDB_NAME_PREFIX));
69  if (name == NULL)
70    {
71      fprintf (stderr, "Unable to allocate memory for name.");
72      exit (1);
73    }
74
75  strcpy (name, GDB_NAME_PREFIX);
76  strcat (name, argv[1]);
77
78  /* Look up the GDB service port.  For convenience, add the
79     GDB_NAME_PREFIX the argument before looking up the name.
80     For backwards compatibility, do it without.  */
81
82  kr = netname_look_up (name_server_port, host, name, &gdb_port);
83  if (kr == NETNAME_NOT_CHECKED_IN)
84    kr = netname_look_up (name_server_port, host, argv[1], &gdb_port);
85  if (kr != KERN_SUCCESS)
86    {
87      fprintf (stderr, "Unable to lookup the GDB service port: %s.\n",
88	       mach_error_string (kr));
89      exit (1);
90    }
91
92  /* Code generated by mig stub generator, with minor cleanups :-)
93
94     simpleroutine stop_inferior(gdb_port : mach_port_t);  */
95
96  msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0);
97  msg.msgh_remote_port = gdb_port;
98  msg.msgh_local_port = MACH_PORT_NULL;
99  msg.msgh_size = sizeof (msg);
100  msg.msgh_seqno = 0;
101  msg.msgh_id = GDB_MESSAGE_ID_STOP;
102
103  kr = mach_msg_send (&msg);
104  if (kr != KERN_SUCCESS)
105    fprintf (stderr, "Message not sent, return code %d : %s\n", kr,
106	     mach_error_string (kr));
107
108  exit (kr != KERN_SUCCESS);
109}
110