OSDN Git Service

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