Commit | Line | Data |
---|---|---|
ce3a066d | 1 | /* Target operations for the remote server for GDB. |
7b6bb8da | 2 | Copyright (C) 2002, 2004, 2005, 2007, 2008, 2009, 2010, 2011 |
0fb0cc75 | 3 | Free Software Foundation, Inc. |
ce3a066d DJ |
4 | |
5 | Contributed by MontaVista Software. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
ce3a066d DJ |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
ce3a066d DJ |
21 | |
22 | #include "server.h" | |
23 | ||
24 | struct target_ops *the_target; | |
25 | ||
0d62e5e8 DJ |
26 | void |
27 | set_desired_inferior (int use_general) | |
28 | { | |
29 | struct thread_info *found; | |
30 | ||
31 | if (use_general == 1) | |
e09875d4 | 32 | found = find_thread_ptid (general_thread); |
0d62e5e8 DJ |
33 | else |
34 | { | |
35 | found = NULL; | |
36 | ||
37 | /* If we are continuing any (all) thread(s), use step_thread | |
38 | to decide which thread to step and/or send the specified | |
39 | signal to. */ | |
95954743 PA |
40 | if ((!ptid_equal (step_thread, null_ptid) |
41 | && !ptid_equal (step_thread, minus_one_ptid)) | |
42 | && (ptid_equal (cont_thread, null_ptid) | |
43 | || ptid_equal (cont_thread, minus_one_ptid))) | |
e09875d4 | 44 | found = find_thread_ptid (step_thread); |
0d62e5e8 DJ |
45 | |
46 | if (found == NULL) | |
e09875d4 | 47 | found = find_thread_ptid (cont_thread); |
0d62e5e8 DJ |
48 | } |
49 | ||
50 | if (found == NULL) | |
51 | current_inferior = (struct thread_info *) all_threads.head; | |
52 | else | |
53 | current_inferior = found; | |
54 | } | |
55 | ||
c3e735a6 | 56 | int |
f450004a | 57 | read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
611cb4a5 | 58 | { |
c3e735a6 DJ |
59 | int res; |
60 | res = (*the_target->read_memory) (memaddr, myaddr, len); | |
611cb4a5 | 61 | check_mem_read (memaddr, myaddr, len); |
c3e735a6 | 62 | return res; |
611cb4a5 DJ |
63 | } |
64 | ||
65 | int | |
f450004a DJ |
66 | write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr, |
67 | int len) | |
0d62e5e8 DJ |
68 | { |
69 | /* Lacking cleanups, there is some potential for a memory leak if the | |
70 | write fails and we go through error(). Make sure that no more than | |
71 | one buffer is ever pending by making BUFFER static. */ | |
f450004a | 72 | static unsigned char *buffer = 0; |
0d62e5e8 DJ |
73 | int res; |
74 | ||
75 | if (buffer != NULL) | |
76 | free (buffer); | |
77 | ||
bca929d3 | 78 | buffer = xmalloc (len); |
0d62e5e8 DJ |
79 | memcpy (buffer, myaddr, len); |
80 | check_mem_write (memaddr, buffer, len); | |
81 | res = (*the_target->write_memory) (memaddr, buffer, len); | |
82 | free (buffer); | |
83 | buffer = NULL; | |
84 | ||
85 | return res; | |
86 | } | |
87 | ||
95954743 PA |
88 | ptid_t |
89 | mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options, | |
bd99dc85 | 90 | int connected_wait) |
611cb4a5 | 91 | { |
95954743 | 92 | ptid_t ret; |
0d62e5e8 DJ |
93 | |
94 | if (connected_wait) | |
95 | server_waiting = 1; | |
96 | ||
95954743 | 97 | ret = (*the_target->wait) (ptid, ourstatus, options); |
bd99dc85 | 98 | |
95954743 PA |
99 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) |
100 | fprintf (stderr, | |
423ec54c | 101 | "\nChild exited with status %d\n", ourstatus->value.integer); |
95954743 PA |
102 | else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED) |
103 | fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", | |
104 | target_signal_to_host (ourstatus->value.sig), | |
105 | target_signal_to_name (ourstatus->value.sig)); | |
0d62e5e8 DJ |
106 | |
107 | if (connected_wait) | |
108 | server_waiting = 0; | |
109 | ||
110 | return ret; | |
611cb4a5 DJ |
111 | } |
112 | ||
bd99dc85 PA |
113 | int |
114 | start_non_stop (int nonstop) | |
115 | { | |
116 | if (the_target->start_non_stop == NULL) | |
117 | { | |
118 | if (nonstop) | |
119 | return -1; | |
120 | else | |
121 | return 0; | |
122 | } | |
123 | ||
124 | return (*the_target->start_non_stop) (nonstop); | |
125 | } | |
126 | ||
ce3a066d DJ |
127 | void |
128 | set_target_ops (struct target_ops *target) | |
129 | { | |
bca929d3 | 130 | the_target = (struct target_ops *) xmalloc (sizeof (*the_target)); |
ce3a066d DJ |
131 | memcpy (the_target, target, sizeof (*the_target)); |
132 | } | |
95954743 PA |
133 | |
134 | /* Convert pid to printable format. */ | |
135 | ||
136 | const char * | |
137 | target_pid_to_str (ptid_t ptid) | |
138 | { | |
139 | static char buf[80]; | |
140 | ||
141 | if (ptid_equal (ptid, minus_one_ptid)) | |
6cebaf6e | 142 | xsnprintf (buf, sizeof (buf), "<all threads>"); |
95954743 | 143 | else if (ptid_equal (ptid, null_ptid)) |
6cebaf6e | 144 | xsnprintf (buf, sizeof (buf), "<null thread>"); |
95954743 | 145 | else if (ptid_get_tid (ptid) != 0) |
6cebaf6e | 146 | xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx", |
147 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
95954743 | 148 | else if (ptid_get_lwp (ptid) != 0) |
6cebaf6e | 149 | xsnprintf (buf, sizeof (buf), "LWP %d.%ld", |
150 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); | |
95954743 | 151 | else |
6cebaf6e | 152 | xsnprintf (buf, sizeof (buf), "Process %d", |
153 | ptid_get_pid (ptid)); | |
95954743 PA |
154 | |
155 | return buf; | |
156 | } | |
8336d594 PA |
157 | |
158 | /* Return a pretty printed form of target_waitstatus. */ | |
159 | ||
160 | const char * | |
161 | target_waitstatus_to_string (const struct target_waitstatus *ws) | |
162 | { | |
163 | static char buf[200]; | |
164 | const char *kind_str = "status->kind = "; | |
165 | ||
166 | switch (ws->kind) | |
167 | { | |
168 | case TARGET_WAITKIND_EXITED: | |
169 | sprintf (buf, "%sexited, status = %d", | |
170 | kind_str, ws->value.integer); | |
171 | break; | |
172 | case TARGET_WAITKIND_STOPPED: | |
173 | sprintf (buf, "%sstopped, signal = %s", | |
174 | kind_str, target_signal_to_name (ws->value.sig)); | |
175 | break; | |
176 | case TARGET_WAITKIND_SIGNALLED: | |
177 | sprintf (buf, "%ssignalled, signal = %s", | |
178 | kind_str, target_signal_to_name (ws->value.sig)); | |
179 | break; | |
180 | case TARGET_WAITKIND_LOADED: | |
181 | sprintf (buf, "%sloaded", kind_str); | |
182 | break; | |
183 | case TARGET_WAITKIND_EXECD: | |
184 | sprintf (buf, "%sexecd", kind_str); | |
185 | break; | |
186 | case TARGET_WAITKIND_SPURIOUS: | |
187 | sprintf (buf, "%sspurious", kind_str); | |
188 | break; | |
189 | case TARGET_WAITKIND_IGNORE: | |
190 | sprintf (buf, "%signore", kind_str); | |
191 | break; | |
192 | default: | |
193 | sprintf (buf, "%sunknown???", kind_str); | |
194 | break; | |
195 | } | |
196 | ||
197 | return buf; | |
198 | } |