Move linux_find_memory_regions_full & co.
[deliverable/binutils-gdb.git] / gdb / gdbserver / target.c
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "tracepoint.h"
23 #include "target/target-utils.h"
24 #include <fcntl.h>
25
26 struct target_ops *the_target;
27
28 void
29 set_desired_thread (int use_general)
30 {
31 struct thread_info *found;
32
33 if (use_general == 1)
34 found = find_thread_ptid (general_thread);
35 else
36 found = find_thread_ptid (cont_thread);
37
38 if (found == NULL)
39 current_thread = get_first_thread ();
40 else
41 current_thread = found;
42 }
43
44 int
45 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
46 {
47 int res;
48 res = (*the_target->read_memory) (memaddr, myaddr, len);
49 check_mem_read (memaddr, myaddr, len);
50 return res;
51 }
52
53 /* See target/target.h. */
54
55 int
56 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
57 {
58 return read_inferior_memory (memaddr, myaddr, len);
59 }
60
61 /* See target/target.h. */
62
63 int
64 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
65 {
66 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
67 }
68
69 int
70 write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
71 int len)
72 {
73 /* Lacking cleanups, there is some potential for a memory leak if the
74 write fails and we go through error(). Make sure that no more than
75 one buffer is ever pending by making BUFFER static. */
76 static unsigned char *buffer = 0;
77 int res;
78
79 if (buffer != NULL)
80 free (buffer);
81
82 buffer = xmalloc (len);
83 memcpy (buffer, myaddr, len);
84 check_mem_write (memaddr, buffer, myaddr, len);
85 res = (*the_target->write_memory) (memaddr, buffer, len);
86 free (buffer);
87 buffer = NULL;
88
89 return res;
90 }
91
92 /* See target/target.h. */
93
94 int
95 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
96 {
97 return write_inferior_memory (memaddr, myaddr, len);
98 }
99
100 ptid_t
101 mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
102 int connected_wait)
103 {
104 ptid_t ret;
105
106 if (connected_wait)
107 server_waiting = 1;
108
109 ret = (*the_target->wait) (ptid, ourstatus, options);
110
111 /* We don't expose _LOADED events to gdbserver core. See the
112 `dlls_changed' global. */
113 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
114 ourstatus->kind = TARGET_WAITKIND_STOPPED;
115
116 /* If GDB is connected through TCP/serial, then GDBserver will most
117 probably be running on its own terminal/console, so it's nice to
118 print there why is GDBserver exiting. If however, GDB is
119 connected through stdio, then there's no need to spam the GDB
120 console with this -- the user will already see the exit through
121 regular GDB output, in that same terminal. */
122 if (!remote_connection_is_stdio ())
123 {
124 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
125 fprintf (stderr,
126 "\nChild exited with status %d\n", ourstatus->value.integer);
127 else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
128 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
129 gdb_signal_to_host (ourstatus->value.sig),
130 gdb_signal_to_name (ourstatus->value.sig));
131 }
132
133 if (connected_wait)
134 server_waiting = 0;
135
136 return ret;
137 }
138
139 /* See target/target.h. */
140
141 void
142 target_stop_and_wait (ptid_t ptid)
143 {
144 struct target_waitstatus status;
145 int was_non_stop = non_stop;
146 struct thread_resume resume_info;
147
148 resume_info.thread = ptid;
149 resume_info.kind = resume_stop;
150 resume_info.sig = GDB_SIGNAL_0;
151 (*the_target->resume) (&resume_info, 1);
152
153 non_stop = 1;
154 mywait (ptid, &status, 0, 0);
155 non_stop = was_non_stop;
156 }
157
158 /* See target/target.h. */
159
160 void
161 target_continue_no_signal (ptid_t ptid)
162 {
163 struct thread_resume resume_info;
164
165 resume_info.thread = ptid;
166 resume_info.kind = resume_continue;
167 resume_info.sig = GDB_SIGNAL_0;
168 (*the_target->resume) (&resume_info, 1);
169 }
170
171 int
172 start_non_stop (int nonstop)
173 {
174 if (the_target->start_non_stop == NULL)
175 {
176 if (nonstop)
177 return -1;
178 else
179 return 0;
180 }
181
182 return (*the_target->start_non_stop) (nonstop);
183 }
184
185 void
186 set_target_ops (struct target_ops *target)
187 {
188 the_target = (struct target_ops *) xmalloc (sizeof (*the_target));
189 memcpy (the_target, target, sizeof (*the_target));
190 }
191
192 /* Convert pid to printable format. */
193
194 const char *
195 target_pid_to_str (ptid_t ptid)
196 {
197 static char buf[80];
198
199 if (ptid_equal (ptid, minus_one_ptid))
200 xsnprintf (buf, sizeof (buf), "<all threads>");
201 else if (ptid_equal (ptid, null_ptid))
202 xsnprintf (buf, sizeof (buf), "<null thread>");
203 else if (ptid_get_tid (ptid) != 0)
204 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
205 ptid_get_pid (ptid), ptid_get_tid (ptid));
206 else if (ptid_get_lwp (ptid) != 0)
207 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
208 ptid_get_pid (ptid), ptid_get_lwp (ptid));
209 else
210 xsnprintf (buf, sizeof (buf), "Process %d",
211 ptid_get_pid (ptid));
212
213 return buf;
214 }
215
216 int
217 kill_inferior (int pid)
218 {
219 gdb_agent_about_to_close (pid);
220
221 return (*the_target->kill) (pid);
222 }
223
224 static int
225 target_fileio_read_stralloc_1_pread (int handle, gdb_byte *read_buf, int len,
226 ULONGEST offset, int *target_errno)
227 {
228 int retval = pread (handle, read_buf, len, offset);
229
230 *target_errno = errno;
231 return retval;
232 }
233
234 static LONGEST
235 target_fileio_read_stralloc_1 (struct inferior *inf, const char *filename,
236 gdb_byte **buf_p, int padding)
237 {
238 int fd;
239 LONGEST retval;
240
241 fd = open (filename, O_RDONLY);
242 if (fd == -1)
243 return -1;
244
245 retval = read_alloc (buf_p, fd, target_fileio_read_stralloc_1_pread, padding);
246
247 close (fd);
248
249 return retval;
250 }
251
252 char *
253 target_fileio_read_stralloc (struct inferior *inf, const char *filename)
254 {
255 return read_stralloc (inf, filename, target_fileio_read_stralloc_1);
256 }
This page took 0.036382 seconds and 4 git commands to generate.