* gdbserver/Makefile.in (gdbserver): Use -lbsd.
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-server.c
CommitLineData
e20520b8
SG
1/* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include <setjmp.h>
22#include <signal.h>
23
24void read_inferior_memory ();
25unsigned char mywait ();
26void myresume();
27void initialize ();
28int create_inferior ();
29
30extern char registers[];
31int inferior_pid;
32extern char **environ;
33
34/* Descriptor for I/O to remote machine. */
35int remote_desc;
36int kiodebug = 0;
37int remote_debugging;
38
39void remote_send ();
40void putpkt ();
41void getpkt ();
42void remote_open ();
43void write_ok ();
44void write_enn ();
45void convert_ascii_to_int ();
46void convert_int_to_ascii ();
47void prepare_resume_reply ();
48void decode_m_packet ();
49void decode_M_packet ();
50jmp_buf toplevel;
51
52main (argc, argv)
53 int argc;
54 char *argv[];
55{
56 char ch, status, own_buf[2000], mem_buf[2000];
57 int i = 0;
58 unsigned char signal;
59 unsigned int mem_addr, len;
e20520b8
SG
60
61 if (setjmp(toplevel))
62 {
63 fprintf(stderr, "Exiting\n");
64 exit(1);
65 }
66
67 if (argc < 3)
68 error("Usage: gdbserver tty prog [args ...]");
69
70 initialize ();
71 remote_open (argv[1], 0);
72
f450d101
SG
73 inferior_pid = create_inferior (argv[2], &argv[2]);
74 fprintf (stderr, "Process %s created; pid = %d\n", argv[2], inferior_pid);
e20520b8 75
f450d101 76 signal = mywait (&status); /* Wait till we are at 1st instr in prog */
e20520b8
SG
77
78 /* We are now stopped at the first instruction of the target process */
79
80 setjmp(toplevel);
81 do
82 {
83 getpkt (own_buf);
84 i = 0;
85 ch = own_buf[i++];
86 switch (ch)
87 {
88 case '?':
89 prepare_resume_reply (own_buf, status, signal);
90 break;
91 case 'g':
92 convert_int_to_ascii (registers, own_buf, REGISTER_BYTES);
93 break;
94 case 'G':
95 convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES);
96 store_inferior_registers (-1);
97 write_ok (own_buf);
98 break;
99 case 'm':
100 decode_m_packet (&own_buf[1], &mem_addr, &len);
101 read_inferior_memory (mem_addr, mem_buf, len);
102 convert_int_to_ascii (mem_buf, own_buf, len);
103 break;
104 case 'M':
105 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
106 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
107 write_ok (own_buf);
108 else
109 write_enn (own_buf);
110 break;
111 case 'c':
112 myresume (0, 0);
113 signal = mywait (&status);
114 prepare_resume_reply (own_buf, status, signal);
115 break;
116 case 's':
117 myresume (1, 0);
118 signal = mywait (&status);
119 prepare_resume_reply (own_buf, status, signal);
120 break;
121 case 'k':
122 kill_inferior ();
123 sprintf (own_buf, "q");
124 putpkt (own_buf);
125 fprintf (stderr, "Obtained kill request...terminating\n");
126 close (remote_desc);
127 exit (0);
128 default:
129 printf ("\nUnknown option chosen by master\n");
130 write_enn (own_buf);
131 break;
132 }
133
134 putpkt (own_buf);
135 }
136 while (1);
137
138 close (remote_desc);
139 /** now get out of here**/
140 fprintf (stderr, "Finished reading data from serial link - Bye!\n");
141 exit (0);
142}
This page took 0.028067 seconds and 4 git commands to generate.