Add README.
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-server.c
1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include <setjmp.h>
22 #include <signal.h>
23
24 void read_inferior_memory ();
25 unsigned char mywait ();
26 void myresume();
27 void initialize ();
28 int create_inferior ();
29
30 extern char registers[];
31 int inferior_pid;
32 extern char **environ;
33
34 /* Descriptor for I/O to remote machine. */
35 int remote_desc;
36 int kiodebug = 0;
37 int remote_debugging;
38
39 void remote_send ();
40 void putpkt ();
41 void getpkt ();
42 void remote_open ();
43 void write_ok ();
44 void write_enn ();
45 void convert_ascii_to_int ();
46 void convert_int_to_ascii ();
47 void prepare_resume_reply ();
48 void decode_m_packet ();
49 void decode_M_packet ();
50 jmp_buf toplevel;
51
52 main (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;
60 char argvec[1024];
61
62 if (setjmp(toplevel))
63 {
64 fprintf(stderr, "Exiting\n");
65 exit(1);
66 }
67
68 if (argc < 3)
69 error("Usage: gdbserver tty prog [args ...]");
70
71 initialize ();
72 remote_open (argv[1], 0);
73
74 argvec[0] = '\000';
75 for (i = 2; i < argc; i++)
76 strcat(argvec, argv[i]);
77
78 inferior_pid = create_inferior (argvec, environ);
79 fprintf (stderr, "Process %s created; pid = %d\n", argv[1], inferior_pid);
80
81 signal = mywait (&status); /* Wait till we are at 1st instr in shell */
82 if (status != 'S' || signal != SIGTRAP)
83 error ("Bad status from shell\n");
84 myresume (0, 0); /* Start up the shell */
85 signal = mywait (&status); /* Wait for program to start */
86
87 /* We are now stopped at the first instruction of the target process */
88
89 setjmp(toplevel);
90 do
91 {
92 getpkt (own_buf);
93 i = 0;
94 ch = own_buf[i++];
95 switch (ch)
96 {
97 case '?':
98 prepare_resume_reply (own_buf, status, signal);
99 break;
100 case 'g':
101 convert_int_to_ascii (registers, own_buf, REGISTER_BYTES);
102 break;
103 case 'G':
104 convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES);
105 store_inferior_registers (-1);
106 write_ok (own_buf);
107 break;
108 case 'm':
109 decode_m_packet (&own_buf[1], &mem_addr, &len);
110 read_inferior_memory (mem_addr, mem_buf, len);
111 convert_int_to_ascii (mem_buf, own_buf, len);
112 break;
113 case 'M':
114 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
115 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
116 write_ok (own_buf);
117 else
118 write_enn (own_buf);
119 break;
120 case 'c':
121 myresume (0, 0);
122 signal = mywait (&status);
123 prepare_resume_reply (own_buf, status, signal);
124 break;
125 case 's':
126 myresume (1, 0);
127 signal = mywait (&status);
128 prepare_resume_reply (own_buf, status, signal);
129 break;
130 case 'k':
131 kill_inferior ();
132 sprintf (own_buf, "q");
133 putpkt (own_buf);
134 fprintf (stderr, "Obtained kill request...terminating\n");
135 close (remote_desc);
136 exit (0);
137 default:
138 printf ("\nUnknown option chosen by master\n");
139 write_enn (own_buf);
140 break;
141 }
142
143 putpkt (own_buf);
144 }
145 while (1);
146
147 close (remote_desc);
148 /** now get out of here**/
149 fprintf (stderr, "Finished reading data from serial link - Bye!\n");
150 exit (0);
151 }
This page took 0.0324 seconds and 4 git commands to generate.