]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/target.c
Move savestring to common/common-utils.c, make gdbserver use it.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / target.c
CommitLineData
ce3a066d 1/* Target operations for the remote server for GDB.
28e7fd62 2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
ce3a066d
DJ
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
ce3a066d
DJ
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ce3a066d
DJ
20
21#include "server.h"
22
23struct target_ops *the_target;
24
0d62e5e8
DJ
25void
26set_desired_inferior (int use_general)
27{
28 struct thread_info *found;
29
30 if (use_general == 1)
e09875d4 31 found = find_thread_ptid (general_thread);
0d62e5e8 32 else
943ca1dd 33 found = find_thread_ptid (cont_thread);
0d62e5e8
DJ
34
35 if (found == NULL)
36 current_inferior = (struct thread_info *) all_threads.head;
37 else
38 current_inferior = found;
39}
40
c3e735a6 41int
f450004a 42read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
611cb4a5 43{
c3e735a6
DJ
44 int res;
45 res = (*the_target->read_memory) (memaddr, myaddr, len);
611cb4a5 46 check_mem_read (memaddr, myaddr, len);
c3e735a6 47 return res;
611cb4a5
DJ
48}
49
50int
f450004a
DJ
51write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
52 int len)
0d62e5e8
DJ
53{
54 /* Lacking cleanups, there is some potential for a memory leak if the
55 write fails and we go through error(). Make sure that no more than
56 one buffer is ever pending by making BUFFER static. */
f450004a 57 static unsigned char *buffer = 0;
0d62e5e8
DJ
58 int res;
59
60 if (buffer != NULL)
61 free (buffer);
62
bca929d3 63 buffer = xmalloc (len);
0d62e5e8 64 memcpy (buffer, myaddr, len);
b9fd1791 65 check_mem_write (memaddr, buffer, myaddr, len);
0d62e5e8
DJ
66 res = (*the_target->write_memory) (memaddr, buffer, len);
67 free (buffer);
68 buffer = NULL;
69
70 return res;
71}
72
95954743
PA
73ptid_t
74mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
bd99dc85 75 int connected_wait)
611cb4a5 76{
95954743 77 ptid_t ret;
0d62e5e8
DJ
78
79 if (connected_wait)
80 server_waiting = 1;
81
95954743 82 ret = (*the_target->wait) (ptid, ourstatus, options);
bd99dc85 83
95954743
PA
84 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
85 fprintf (stderr,
423ec54c 86 "\nChild exited with status %d\n", ourstatus->value.integer);
95954743
PA
87 else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
88 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
2ea28649
PA
89 gdb_signal_to_host (ourstatus->value.sig),
90 gdb_signal_to_name (ourstatus->value.sig));
0d62e5e8
DJ
91
92 if (connected_wait)
93 server_waiting = 0;
94
95 return ret;
611cb4a5
DJ
96}
97
bd99dc85
PA
98int
99start_non_stop (int nonstop)
100{
101 if (the_target->start_non_stop == NULL)
102 {
103 if (nonstop)
104 return -1;
105 else
106 return 0;
107 }
108
109 return (*the_target->start_non_stop) (nonstop);
110}
111
ce3a066d
DJ
112void
113set_target_ops (struct target_ops *target)
114{
bca929d3 115 the_target = (struct target_ops *) xmalloc (sizeof (*the_target));
ce3a066d
DJ
116 memcpy (the_target, target, sizeof (*the_target));
117}
95954743
PA
118
119/* Convert pid to printable format. */
120
121const char *
122target_pid_to_str (ptid_t ptid)
123{
124 static char buf[80];
125
126 if (ptid_equal (ptid, minus_one_ptid))
6cebaf6e 127 xsnprintf (buf, sizeof (buf), "<all threads>");
95954743 128 else if (ptid_equal (ptid, null_ptid))
6cebaf6e 129 xsnprintf (buf, sizeof (buf), "<null thread>");
95954743 130 else if (ptid_get_tid (ptid) != 0)
6cebaf6e 131 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
132 ptid_get_pid (ptid), ptid_get_tid (ptid));
95954743 133 else if (ptid_get_lwp (ptid) != 0)
6cebaf6e 134 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
135 ptid_get_pid (ptid), ptid_get_lwp (ptid));
95954743 136 else
6cebaf6e 137 xsnprintf (buf, sizeof (buf), "Process %d",
138 ptid_get_pid (ptid));
95954743
PA
139
140 return buf;
141}
8336d594
PA
142
143/* Return a pretty printed form of target_waitstatus. */
144
145const char *
146target_waitstatus_to_string (const struct target_waitstatus *ws)
147{
148 static char buf[200];
149 const char *kind_str = "status->kind = ";
150
151 switch (ws->kind)
152 {
153 case TARGET_WAITKIND_EXITED:
154 sprintf (buf, "%sexited, status = %d",
155 kind_str, ws->value.integer);
156 break;
157 case TARGET_WAITKIND_STOPPED:
158 sprintf (buf, "%sstopped, signal = %s",
2ea28649 159 kind_str, gdb_signal_to_name (ws->value.sig));
8336d594
PA
160 break;
161 case TARGET_WAITKIND_SIGNALLED:
162 sprintf (buf, "%ssignalled, signal = %s",
2ea28649 163 kind_str, gdb_signal_to_name (ws->value.sig));
8336d594
PA
164 break;
165 case TARGET_WAITKIND_LOADED:
166 sprintf (buf, "%sloaded", kind_str);
167 break;
168 case TARGET_WAITKIND_EXECD:
169 sprintf (buf, "%sexecd", kind_str);
170 break;
171 case TARGET_WAITKIND_SPURIOUS:
172 sprintf (buf, "%sspurious", kind_str);
173 break;
174 case TARGET_WAITKIND_IGNORE:
175 sprintf (buf, "%signore", kind_str);
176 break;
177 default:
178 sprintf (buf, "%sunknown???", kind_str);
179 break;
180 }
181
182 return buf;
183}
7255706c
YQ
184
185int
186kill_inferior (int pid)
187{
188 gdb_agent_about_to_close (pid);
189
190 return (*the_target->kill) (pid);
191}