Commit | Line | Data |
---|---|---|
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" | |
c144c7a0 | 22 | #include "tracepoint.h" |
ce3a066d DJ |
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 | 33 | else |
943ca1dd | 34 | found = find_thread_ptid (cont_thread); |
0d62e5e8 DJ |
35 | |
36 | if (found == NULL) | |
37 | current_inferior = (struct thread_info *) all_threads.head; | |
38 | else | |
39 | current_inferior = found; | |
40 | } | |
41 | ||
c3e735a6 | 42 | int |
f450004a | 43 | read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
611cb4a5 | 44 | { |
c3e735a6 DJ |
45 | int res; |
46 | res = (*the_target->read_memory) (memaddr, myaddr, len); | |
611cb4a5 | 47 | check_mem_read (memaddr, myaddr, len); |
c3e735a6 | 48 | return res; |
611cb4a5 DJ |
49 | } |
50 | ||
51 | int | |
f450004a DJ |
52 | write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr, |
53 | int len) | |
0d62e5e8 DJ |
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. */ | |
f450004a | 58 | static unsigned char *buffer = 0; |
0d62e5e8 DJ |
59 | int res; |
60 | ||
61 | if (buffer != NULL) | |
62 | free (buffer); | |
63 | ||
bca929d3 | 64 | buffer = xmalloc (len); |
0d62e5e8 | 65 | memcpy (buffer, myaddr, len); |
b9fd1791 | 66 | check_mem_write (memaddr, buffer, myaddr, len); |
0d62e5e8 DJ |
67 | res = (*the_target->write_memory) (memaddr, buffer, len); |
68 | free (buffer); | |
69 | buffer = NULL; | |
70 | ||
71 | return res; | |
72 | } | |
73 | ||
95954743 PA |
74 | ptid_t |
75 | mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options, | |
bd99dc85 | 76 | int connected_wait) |
611cb4a5 | 77 | { |
95954743 | 78 | ptid_t ret; |
0d62e5e8 DJ |
79 | |
80 | if (connected_wait) | |
81 | server_waiting = 1; | |
82 | ||
95954743 | 83 | ret = (*the_target->wait) (ptid, ourstatus, options); |
bd99dc85 | 84 | |
1a3d890b PA |
85 | /* If GDB is connected through TCP/serial, then GDBserver will most |
86 | probably be running on its own terminal/console, so it's nice to | |
87 | print there why is GDBserver exiting. If however, GDB is | |
88 | connected through stdio, then there's no need to spam the GDB | |
89 | console with this -- the user will already see the exit through | |
90 | regular GDB output, in that same terminal. */ | |
91 | if (!remote_connection_is_stdio ()) | |
92 | { | |
93 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) | |
94 | fprintf (stderr, | |
95 | "\nChild exited with status %d\n", ourstatus->value.integer); | |
96 | else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED) | |
97 | fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", | |
98 | gdb_signal_to_host (ourstatus->value.sig), | |
99 | gdb_signal_to_name (ourstatus->value.sig)); | |
100 | } | |
0d62e5e8 DJ |
101 | |
102 | if (connected_wait) | |
103 | server_waiting = 0; | |
104 | ||
105 | return ret; | |
611cb4a5 DJ |
106 | } |
107 | ||
bd99dc85 PA |
108 | int |
109 | start_non_stop (int nonstop) | |
110 | { | |
111 | if (the_target->start_non_stop == NULL) | |
112 | { | |
113 | if (nonstop) | |
114 | return -1; | |
115 | else | |
116 | return 0; | |
117 | } | |
118 | ||
119 | return (*the_target->start_non_stop) (nonstop); | |
120 | } | |
121 | ||
ce3a066d DJ |
122 | void |
123 | set_target_ops (struct target_ops *target) | |
124 | { | |
bca929d3 | 125 | the_target = (struct target_ops *) xmalloc (sizeof (*the_target)); |
ce3a066d DJ |
126 | memcpy (the_target, target, sizeof (*the_target)); |
127 | } | |
95954743 PA |
128 | |
129 | /* Convert pid to printable format. */ | |
130 | ||
131 | const char * | |
132 | target_pid_to_str (ptid_t ptid) | |
133 | { | |
134 | static char buf[80]; | |
135 | ||
136 | if (ptid_equal (ptid, minus_one_ptid)) | |
6cebaf6e | 137 | xsnprintf (buf, sizeof (buf), "<all threads>"); |
95954743 | 138 | else if (ptid_equal (ptid, null_ptid)) |
6cebaf6e | 139 | xsnprintf (buf, sizeof (buf), "<null thread>"); |
95954743 | 140 | else if (ptid_get_tid (ptid) != 0) |
6cebaf6e | 141 | xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx", |
142 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
95954743 | 143 | else if (ptid_get_lwp (ptid) != 0) |
6cebaf6e | 144 | xsnprintf (buf, sizeof (buf), "LWP %d.%ld", |
145 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); | |
95954743 | 146 | else |
6cebaf6e | 147 | xsnprintf (buf, sizeof (buf), "Process %d", |
148 | ptid_get_pid (ptid)); | |
95954743 PA |
149 | |
150 | return buf; | |
151 | } | |
8336d594 | 152 | |
7255706c YQ |
153 | int |
154 | kill_inferior (int pid) | |
155 | { | |
156 | gdb_agent_about_to_close (pid); | |
157 | ||
158 | return (*the_target->kill) (pid); | |
159 | } |