Commit | Line | Data |
---|---|---|
da59e081 | 1 | /* Interface GDB to the GNU Hurd. |
b6ba6518 KB |
2 | Copyright 1992, 1995, 1996, 1997, 1998, 1999, 2000 |
3 | Free Software Foundation, Inc. | |
c906108c SS |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | Written by Miles Bader <miles@gnu.ai.mit.edu> | |
8 | ||
9 | Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi> | |
10 | ||
11 | This program is free software; you can redistribute it and/or modify | |
12 | it under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2 of the License, or | |
14 | (at your option) any later version. | |
15 | ||
16 | This program is distributed in the hope that it will be useful, | |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with this program; if not, write to the Free Software | |
c5aa993b JM |
23 | Foundation, Inc., 59 Temple Place - Suite 330, |
24 | Boston, MA 02111-1307, USA. | |
25 | */ | |
c906108c | 26 | |
942fd805 | 27 | #include <errno.h> |
c906108c | 28 | #include <limits.h> |
942fd805 MK |
29 | #include <setjmp.h> |
30 | #include <signal.h> | |
31 | #include <stdio.h> | |
32 | #include <string.h> | |
c906108c SS |
33 | #include <sys/ptrace.h> |
34 | ||
c906108c | 35 | #include <mach.h> |
c906108c SS |
36 | #include <mach_error.h> |
37 | #include <mach/exception.h> | |
942fd805 MK |
38 | #include <mach/message.h> |
39 | #include <mach/notify.h> | |
c906108c SS |
40 | #include <mach/vm_attributes.h> |
41 | ||
942fd805 MK |
42 | #include <hurd.h> |
43 | #include <hurd/interrupt.h> | |
c906108c SS |
44 | #include <hurd/msg.h> |
45 | #include <hurd/msg_request.h> | |
942fd805 MK |
46 | #include <hurd/process.h> |
47 | #include <hurd/process_request.h> | |
c906108c | 48 | #include <hurd/signal.h> |
c906108c SS |
49 | #include <hurd/sigpreempt.h> |
50 | ||
51 | #include <portinfo.h> | |
52 | ||
53 | #include "defs.h" | |
54 | #include "inferior.h" | |
55 | #include "symtab.h" | |
56 | #include "value.h" | |
57 | #include "language.h" | |
58 | #include "target.h" | |
03f2053f | 59 | #include "gdb_wait.h" |
c906108c SS |
60 | #include "gdbcmd.h" |
61 | #include "gdbcore.h" | |
942fd805 | 62 | #include "gdbthread.h" |
bf62e5b4 | 63 | #include "gdb_assert.h" |
c906108c SS |
64 | |
65 | #include "gnu-nat.h" | |
66 | ||
67 | #include "exc_request_S.h" | |
68 | #include "notify_S.h" | |
69 | #include "process_reply_S.h" | |
70 | #include "msg_reply_S.h" | |
71 | #include "exc_request_U.h" | |
72 | #include "msg_U.h" | |
73 | ||
74 | static process_t proc_server = MACH_PORT_NULL; | |
75 | ||
76 | /* If we've sent a proc_wait_request to the proc server, the pid of the | |
77 | process we asked about. We can only ever have one outstanding. */ | |
78 | int proc_wait_pid = 0; | |
79 | ||
80 | /* The number of wait requests we've sent, and expect replies from. */ | |
81 | int proc_waits_pending = 0; | |
82 | ||
83 | int gnu_debug_flag = 0; | |
84 | ||
85 | /* Forward decls */ | |
86 | ||
87 | extern struct target_ops gnu_ops; | |
c906108c | 88 | |
c906108c SS |
89 | struct inf *make_inf (); |
90 | void inf_clear_wait (struct inf *inf); | |
91 | void inf_cleanup (struct inf *inf); | |
92 | void inf_startup (struct inf *inf, int pid); | |
93 | int inf_update_suspends (struct inf *inf); | |
94 | void inf_set_pid (struct inf *inf, pid_t pid); | |
95 | void inf_validate_procs (struct inf *inf); | |
96 | void inf_steal_exc_ports (struct inf *inf); | |
97 | void inf_restore_exc_ports (struct inf *inf); | |
98 | struct proc *inf_tid_to_proc (struct inf *inf, int tid); | |
c5aa993b JM |
99 | inline void inf_set_threads_resume_sc (struct inf *inf, |
100 | struct proc *run_thread, | |
c906108c SS |
101 | int run_others); |
102 | inline int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf); | |
103 | inline void inf_suspend (struct inf *inf); | |
104 | inline void inf_resume (struct inf *inf); | |
105 | void inf_set_step_thread (struct inf *inf, struct proc *proc); | |
106 | void inf_detach (struct inf *inf); | |
107 | void inf_attach (struct inf *inf, int pid); | |
108 | void inf_signal (struct inf *inf, enum target_signal sig); | |
cce74817 | 109 | void inf_continue (struct inf *inf); |
c906108c SS |
110 | |
111 | #define inf_debug(_inf, msg, args...) \ | |
112 | do { struct inf *__inf = (_inf); \ | |
113 | debug ("{inf %d %p}: " msg, __inf->pid, __inf , ##args); } while (0) | |
114 | ||
115 | void proc_abort (struct proc *proc, int force); | |
c906108c SS |
116 | struct proc *make_proc (struct inf *inf, mach_port_t port, int tid); |
117 | struct proc *_proc_free (struct proc *proc); | |
118 | int proc_update_sc (struct proc *proc); | |
c5aa993b | 119 | error_t proc_get_exception_port (struct proc *proc, mach_port_t * port); |
c906108c SS |
120 | error_t proc_set_exception_port (struct proc *proc, mach_port_t port); |
121 | static mach_port_t _proc_get_exc_port (struct proc *proc); | |
122 | void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port); | |
123 | void proc_restore_exc_port (struct proc *proc); | |
124 | int proc_trace (struct proc *proc, int set); | |
c906108c SS |
125 | |
126 | /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound | |
127 | to INF's msg port and task port respectively. If it has no msg port, | |
128 | EIEIO is returned. INF must refer to a running process! */ | |
129 | #define INF_MSGPORT_RPC(inf, rpc_expr) \ | |
130 | HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \ | |
131 | (refport = inf->task->port, 0), 0, \ | |
132 | msgport ? (rpc_expr) : EIEIO) | |
133 | ||
134 | /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure | |
135 | there's someone around to deal with the RPC (and resuspend things | |
136 | afterwards). This effects INF's threads' resume_sc count. */ | |
137 | #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \ | |
138 | (inf_set_threads_resume_sc_for_signal_thread (inf) \ | |
139 | ? ({ error_t __e; \ | |
140 | inf_resume (inf); \ | |
141 | __e = INF_MSGPORT_RPC (inf, rpc_expr); \ | |
142 | inf_suspend (inf); \ | |
143 | __e; }) \ | |
144 | : EIEIO) | |
145 | ||
c906108c SS |
146 | \f |
147 | /* The state passed by an exception message. */ | |
148 | struct exc_state | |
c5aa993b JM |
149 | { |
150 | int exception; /* The exception code */ | |
151 | int code, subcode; | |
152 | mach_port_t handler; /* The real exception port to handle this. */ | |
153 | mach_port_t reply; /* The reply port from the exception call. */ | |
154 | }; | |
c906108c SS |
155 | |
156 | /* The results of the last wait an inf did. */ | |
157 | struct inf_wait | |
c5aa993b JM |
158 | { |
159 | struct target_waitstatus status; /* The status returned to gdb. */ | |
160 | struct exc_state exc; /* The exception that caused us to return. */ | |
161 | struct proc *thread; /* The thread in question. */ | |
162 | int suppress; /* Something trivial happened. */ | |
163 | }; | |
c906108c SS |
164 | |
165 | /* The state of an inferior. */ | |
166 | struct inf | |
c5aa993b JM |
167 | { |
168 | /* Fields describing the current inferior. */ | |
c906108c | 169 | |
c5aa993b JM |
170 | struct proc *task; /* The mach task. */ |
171 | struct proc *threads; /* A linked list of all threads in TASK. */ | |
c906108c | 172 | |
c5aa993b JM |
173 | /* True if THREADS needn't be validated by querying the task. We assume that |
174 | we and the task in question are the only ones frobbing the thread list, | |
175 | so as long as we don't let any code run, we don't have to worry about | |
176 | THREADS changing. */ | |
177 | int threads_up_to_date; | |
c906108c | 178 | |
c5aa993b | 179 | pid_t pid; /* The real system PID. */ |
c906108c | 180 | |
c5aa993b | 181 | struct inf_wait wait; /* What to return from target_wait. */ |
c906108c | 182 | |
c5aa993b JM |
183 | /* One thread proc in INF may be in `single-stepping mode'. This is it. */ |
184 | struct proc *step_thread; | |
c906108c | 185 | |
c5aa993b JM |
186 | /* The thread we think is the signal thread. */ |
187 | struct proc *signal_thread; | |
c906108c | 188 | |
c5aa993b | 189 | mach_port_t event_port; /* Where we receive various msgs. */ |
c906108c | 190 | |
c5aa993b JM |
191 | /* True if we think at least one thread in the inferior could currently be |
192 | running. */ | |
193 | unsigned int running:1; | |
c906108c | 194 | |
c5aa993b JM |
195 | /* True if the process has stopped (in the proc server sense). Note that |
196 | since a proc server `stop' leaves the signal thread running, the inf can | |
197 | be RUNNING && STOPPED... */ | |
198 | unsigned int stopped:1; | |
cce74817 | 199 | |
c5aa993b JM |
200 | /* True if the inferior has no message port. */ |
201 | unsigned int nomsg:1; | |
c906108c | 202 | |
c5aa993b JM |
203 | /* True if the inferior is traced. */ |
204 | unsigned int traced:1; | |
c906108c | 205 | |
c5aa993b JM |
206 | /* True if we shouldn't try waiting for the inferior, usually because we |
207 | can't for some reason. */ | |
208 | unsigned int no_wait:1; | |
c906108c | 209 | |
c5aa993b JM |
210 | /* When starting a new inferior, we don't try to validate threads until all |
211 | the proper execs have been done. This is a count of how many execs we | |
212 | expect to happen. */ | |
213 | unsigned pending_execs; | |
c906108c | 214 | |
c5aa993b | 215 | /* Fields describing global state */ |
c906108c | 216 | |
c5aa993b JM |
217 | /* The task suspend count used when gdb has control. This is normally 1 to |
218 | make things easier for us, but sometimes (like when attaching to vital | |
219 | system servers) it may be desirable to let the task continue to run | |
220 | (pausing individual threads as necessary). */ | |
221 | int pause_sc; | |
c906108c | 222 | |
c5aa993b JM |
223 | /* The task suspend count left when detaching from a task. */ |
224 | int detach_sc; | |
c906108c | 225 | |
c5aa993b JM |
226 | /* The initial values used for the run_sc and pause_sc of newly discovered |
227 | threads -- see the definition of those fields in struct proc. */ | |
228 | int default_thread_run_sc; | |
229 | int default_thread_pause_sc; | |
230 | int default_thread_detach_sc; | |
c906108c | 231 | |
c5aa993b JM |
232 | /* True if the process should be traced when started/attached. Newly |
233 | started processes *must* be traced at first to exec them properly, but | |
234 | if this is false, tracing is turned off as soon it has done so. */ | |
235 | int want_signals; | |
c906108c | 236 | |
c5aa993b JM |
237 | /* True if exceptions from the inferior process should be trapped. This |
238 | must be on to use breakpoints. */ | |
239 | int want_exceptions; | |
240 | }; | |
c906108c SS |
241 | |
242 | ||
c5aa993b JM |
243 | int |
244 | __proc_pid (struct proc *proc) | |
c906108c SS |
245 | { |
246 | return proc->inf->pid; | |
247 | } | |
942fd805 | 248 | |
c906108c SS |
249 | \f |
250 | /* Update PROC's real suspend count to match it's desired one. Returns true | |
251 | if we think PROC is now in a runnable state. */ | |
252 | int | |
253 | proc_update_sc (struct proc *proc) | |
254 | { | |
255 | int running; | |
c5aa993b | 256 | int err = 0; |
c906108c SS |
257 | int delta = proc->sc - proc->cur_sc; |
258 | ||
259 | if (delta) | |
260 | proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc); | |
261 | ||
262 | if (proc->sc == 0 && proc->state_changed) | |
263 | /* Since PROC may start running, we must write back any state changes. */ | |
264 | { | |
bf62e5b4 | 265 | gdb_assert (proc_is_thread (proc)); |
c906108c SS |
266 | proc_debug (proc, "storing back changed thread state"); |
267 | err = thread_set_state (proc->port, THREAD_STATE_FLAVOR, | |
f75c77fc | 268 | (thread_state_t) &proc->state, THREAD_STATE_SIZE); |
c5aa993b | 269 | if (!err) |
c906108c SS |
270 | proc->state_changed = 0; |
271 | } | |
272 | ||
273 | if (delta > 0) | |
942fd805 MK |
274 | { |
275 | while (delta-- > 0 && !err) | |
276 | { | |
277 | if (proc_is_task (proc)) | |
278 | err = task_suspend (proc->port); | |
279 | else | |
280 | err = thread_suspend (proc->port); | |
281 | } | |
282 | } | |
c906108c | 283 | else |
942fd805 MK |
284 | { |
285 | while (delta++ < 0 && !err) | |
286 | { | |
287 | if (proc_is_task (proc)) | |
288 | err = task_resume (proc->port); | |
289 | else | |
290 | err = thread_resume (proc->port); | |
291 | } | |
292 | } | |
c5aa993b | 293 | if (!err) |
c906108c SS |
294 | proc->cur_sc = proc->sc; |
295 | ||
296 | /* If we got an error, then the task/thread has disappeared. */ | |
297 | running = !err && proc->sc == 0; | |
298 | ||
299 | proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended"); | |
300 | if (err) | |
301 | proc_debug (proc, "err = %s", strerror (err)); | |
302 | ||
303 | if (running) | |
304 | { | |
305 | proc->aborted = 0; | |
306 | proc->state_valid = proc->state_changed = 0; | |
307 | proc->fetched_regs = 0; | |
308 | } | |
309 | ||
310 | return running; | |
311 | } | |
942fd805 | 312 | |
c906108c SS |
313 | \f |
314 | /* Thread_abort is called on PROC if needed. PROC must be a thread proc. | |
315 | If PROC is deemed `precious', then nothing is done unless FORCE is true. | |
316 | In particular, a thread is precious if it's running (in which case forcing | |
317 | it includes suspending it first), or if it has an exception pending. */ | |
318 | void | |
319 | proc_abort (struct proc *proc, int force) | |
320 | { | |
bf62e5b4 | 321 | gdb_assert (proc_is_thread (proc)); |
c906108c | 322 | |
c5aa993b | 323 | if (!proc->aborted) |
c906108c SS |
324 | { |
325 | struct inf *inf = proc->inf; | |
326 | int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0); | |
327 | ||
328 | if (running && force) | |
329 | { | |
330 | proc->sc = 1; | |
331 | inf_update_suspends (proc->inf); | |
332 | running = 0; | |
333 | warning ("Stopped %s.", proc_string (proc)); | |
334 | } | |
335 | else if (proc == inf->wait.thread && inf->wait.exc.reply && !force) | |
336 | /* An exception is pending on PROC, which don't mess with. */ | |
337 | running = 1; | |
338 | ||
c5aa993b | 339 | if (!running) |
c906108c SS |
340 | /* We only abort the thread if it's not actually running. */ |
341 | { | |
342 | thread_abort (proc->port); | |
343 | proc_debug (proc, "aborted"); | |
344 | proc->aborted = 1; | |
345 | } | |
346 | else | |
347 | proc_debug (proc, "not aborting"); | |
348 | } | |
349 | } | |
350 | ||
351 | /* Make sure that the state field in PROC is up to date, and return a pointer | |
352 | to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure | |
353 | that the thread is stopped and aborted first, and sets the state_changed | |
354 | field in PROC to true. */ | |
355 | thread_state_t | |
356 | proc_get_state (struct proc *proc, int will_modify) | |
357 | { | |
358 | int was_aborted = proc->aborted; | |
359 | ||
360 | proc_debug (proc, "updating state info%s", | |
361 | will_modify ? " (with intention to modify)" : ""); | |
362 | ||
363 | proc_abort (proc, will_modify); | |
364 | ||
c5aa993b | 365 | if (!was_aborted && proc->aborted) |
c906108c SS |
366 | /* PROC's state may have changed since we last fetched it. */ |
367 | proc->state_valid = 0; | |
368 | ||
c5aa993b | 369 | if (!proc->state_valid) |
c906108c SS |
370 | { |
371 | mach_msg_type_number_t state_size = THREAD_STATE_SIZE; | |
372 | error_t err = | |
c5aa993b | 373 | thread_get_state (proc->port, THREAD_STATE_FLAVOR, |
f75c77fc | 374 | (thread_state_t) &proc->state, &state_size); |
c906108c SS |
375 | proc_debug (proc, "getting thread state"); |
376 | proc->state_valid = !err; | |
377 | } | |
378 | ||
379 | if (proc->state_valid) | |
380 | { | |
381 | if (will_modify) | |
382 | proc->state_changed = 1; | |
f75c77fc | 383 | return (thread_state_t) &proc->state; |
c906108c SS |
384 | } |
385 | else | |
386 | return 0; | |
387 | } | |
942fd805 | 388 | |
c906108c SS |
389 | \f |
390 | /* Set PORT to PROC's exception port. */ | |
391 | error_t | |
c5aa993b | 392 | proc_get_exception_port (struct proc * proc, mach_port_t * port) |
c906108c SS |
393 | { |
394 | if (proc_is_task (proc)) | |
395 | return task_get_exception_port (proc->port, port); | |
396 | else | |
397 | return thread_get_exception_port (proc->port, port); | |
398 | } | |
399 | ||
400 | /* Set PROC's exception port to PORT. */ | |
401 | error_t | |
c5aa993b | 402 | proc_set_exception_port (struct proc * proc, mach_port_t port) |
c906108c SS |
403 | { |
404 | proc_debug (proc, "setting exception port: %d", port); | |
405 | if (proc_is_task (proc)) | |
406 | return task_set_exception_port (proc->port, port); | |
407 | else | |
408 | return thread_set_exception_port (proc->port, port); | |
409 | } | |
410 | ||
411 | /* Get PROC's exception port, cleaning up a bit if proc has died. */ | |
412 | static mach_port_t | |
413 | _proc_get_exc_port (struct proc *proc) | |
414 | { | |
415 | mach_port_t exc_port; | |
416 | error_t err = proc_get_exception_port (proc, &exc_port); | |
417 | ||
418 | if (err) | |
419 | /* PROC must be dead. */ | |
420 | { | |
421 | if (proc->exc_port) | |
422 | mach_port_deallocate (mach_task_self (), proc->exc_port); | |
423 | proc->exc_port = MACH_PORT_NULL; | |
424 | if (proc->saved_exc_port) | |
425 | mach_port_deallocate (mach_task_self (), proc->saved_exc_port); | |
426 | proc->saved_exc_port = MACH_PORT_NULL; | |
427 | } | |
428 | ||
429 | return exc_port; | |
430 | } | |
431 | ||
432 | /* Replace PROC's exception port with EXC_PORT, unless it's already been | |
433 | done. Stash away any existing exception port so we can restore it later. */ | |
434 | void | |
435 | proc_steal_exc_port (struct proc *proc, mach_port_t exc_port) | |
436 | { | |
437 | mach_port_t cur_exc_port = _proc_get_exc_port (proc); | |
438 | ||
439 | if (cur_exc_port) | |
440 | { | |
942fd805 | 441 | error_t err = 0; |
c906108c SS |
442 | |
443 | proc_debug (proc, "inserting exception port: %d", exc_port); | |
444 | ||
445 | if (cur_exc_port != exc_port) | |
446 | /* Put in our exception port. */ | |
447 | err = proc_set_exception_port (proc, exc_port); | |
448 | ||
449 | if (err || cur_exc_port == proc->exc_port) | |
450 | /* We previously set the exception port, and it's still set. So we | |
451 | just keep the old saved port which is what the proc set. */ | |
452 | { | |
453 | if (cur_exc_port) | |
454 | mach_port_deallocate (mach_task_self (), cur_exc_port); | |
455 | } | |
456 | else | |
457 | /* Keep a copy of PROC's old exception port so it can be restored. */ | |
458 | { | |
459 | if (proc->saved_exc_port) | |
460 | mach_port_deallocate (mach_task_self (), proc->saved_exc_port); | |
461 | proc->saved_exc_port = cur_exc_port; | |
462 | } | |
463 | ||
464 | proc_debug (proc, "saved exception port: %d", proc->saved_exc_port); | |
465 | ||
466 | if (!err) | |
467 | proc->exc_port = exc_port; | |
468 | else | |
469 | warning ("Error setting exception port for %s: %s", | |
470 | proc_string (proc), strerror (err)); | |
471 | } | |
472 | } | |
473 | ||
474 | /* If we previously replaced PROC's exception port, put back what we | |
475 | found there at the time, unless *our* exception port has since been | |
476 | overwritten, in which case who knows what's going on. */ | |
477 | void | |
478 | proc_restore_exc_port (struct proc *proc) | |
479 | { | |
480 | mach_port_t cur_exc_port = _proc_get_exc_port (proc); | |
481 | ||
482 | if (cur_exc_port) | |
483 | { | |
484 | error_t err = 0; | |
485 | ||
486 | proc_debug (proc, "restoring real exception port"); | |
487 | ||
488 | if (proc->exc_port == cur_exc_port) | |
489 | /* Our's is still there. */ | |
490 | err = proc_set_exception_port (proc, proc->saved_exc_port); | |
491 | ||
492 | if (proc->saved_exc_port) | |
493 | mach_port_deallocate (mach_task_self (), proc->saved_exc_port); | |
494 | proc->saved_exc_port = MACH_PORT_NULL; | |
495 | ||
496 | if (!err) | |
497 | proc->exc_port = MACH_PORT_NULL; | |
498 | else | |
499 | warning ("Error setting exception port for %s: %s", | |
500 | proc_string (proc), strerror (err)); | |
501 | } | |
502 | } | |
942fd805 | 503 | |
c906108c SS |
504 | \f |
505 | /* Turns hardware tracing in PROC on or off when SET is true or false, | |
506 | respectively. Returns true on success. */ | |
507 | int | |
508 | proc_trace (struct proc *proc, int set) | |
509 | { | |
510 | thread_state_t state = proc_get_state (proc, 1); | |
511 | ||
c5aa993b | 512 | if (!state) |
c906108c SS |
513 | return 0; /* the thread must be dead. */ |
514 | ||
515 | proc_debug (proc, "tracing %s", set ? "on" : "off"); | |
c5aa993b | 516 | |
c906108c SS |
517 | if (set) |
518 | { | |
519 | /* XXX We don't get the exception unless the thread has its own | |
c5aa993b | 520 | exception port???? */ |
c906108c SS |
521 | if (proc->exc_port == MACH_PORT_NULL) |
522 | proc_steal_exc_port (proc, proc->inf->event_port); | |
523 | THREAD_STATE_SET_TRACED (state); | |
524 | } | |
525 | else | |
526 | THREAD_STATE_CLEAR_TRACED (state); | |
527 | ||
528 | return 1; | |
529 | } | |
942fd805 | 530 | |
c906108c SS |
531 | \f |
532 | /* A variable from which to assign new TIDs. */ | |
533 | static int next_thread_id = 1; | |
534 | ||
535 | /* Returns a new proc structure with the given fields. Also adds a | |
536 | notification for PORT becoming dead to be sent to INF's notify port. */ | |
537 | struct proc * | |
538 | make_proc (struct inf *inf, mach_port_t port, int tid) | |
539 | { | |
540 | error_t err; | |
541 | mach_port_t prev_port = MACH_PORT_NULL; | |
542 | struct proc *proc = malloc (sizeof (struct proc)); | |
543 | ||
544 | proc->port = port; | |
545 | proc->tid = tid; | |
546 | proc->inf = inf; | |
547 | proc->next = 0; | |
548 | proc->saved_exc_port = MACH_PORT_NULL; | |
549 | proc->exc_port = MACH_PORT_NULL; | |
550 | ||
551 | proc->sc = 0; | |
552 | proc->cur_sc = 0; | |
553 | ||
554 | /* Note that these are all the values for threads; the task simply uses the | |
555 | corresponding field in INF directly. */ | |
556 | proc->run_sc = inf->default_thread_run_sc; | |
557 | proc->pause_sc = inf->default_thread_pause_sc; | |
558 | proc->detach_sc = inf->default_thread_detach_sc; | |
559 | proc->resume_sc = proc->run_sc; | |
560 | ||
561 | proc->aborted = 0; | |
562 | proc->dead = 0; | |
563 | proc->state_valid = 0; | |
564 | proc->state_changed = 0; | |
565 | ||
566 | proc_debug (proc, "is new"); | |
567 | ||
568 | /* Get notified when things die. */ | |
569 | err = | |
c5aa993b | 570 | mach_port_request_notification (mach_task_self (), port, |
c906108c SS |
571 | MACH_NOTIFY_DEAD_NAME, 1, |
572 | inf->event_port, | |
573 | MACH_MSG_TYPE_MAKE_SEND_ONCE, | |
574 | &prev_port); | |
575 | if (err) | |
576 | warning ("Couldn't request notification for port %d: %s", | |
577 | port, strerror (err)); | |
578 | else | |
579 | { | |
580 | proc_debug (proc, "notifications to: %d", inf->event_port); | |
581 | if (prev_port != MACH_PORT_NULL) | |
582 | mach_port_deallocate (mach_task_self (), prev_port); | |
583 | } | |
584 | ||
585 | if (inf->want_exceptions) | |
942fd805 MK |
586 | { |
587 | if (proc_is_task (proc)) | |
588 | /* Make the task exception port point to us. */ | |
589 | proc_steal_exc_port (proc, inf->event_port); | |
590 | else | |
591 | /* Just clear thread exception ports -- they default to the | |
592 | task one. */ | |
593 | proc_steal_exc_port (proc, MACH_PORT_NULL); | |
594 | } | |
c906108c SS |
595 | |
596 | return proc; | |
597 | } | |
598 | ||
599 | /* Frees PROC and any resources it uses, and returns the value of PROC's | |
600 | next field. */ | |
601 | struct proc * | |
602 | _proc_free (struct proc *proc) | |
603 | { | |
604 | struct inf *inf = proc->inf; | |
605 | struct proc *next = proc->next; | |
606 | ||
607 | proc_debug (proc, "freeing..."); | |
608 | ||
609 | if (proc == inf->step_thread) | |
610 | /* Turn off single stepping. */ | |
611 | inf_set_step_thread (inf, 0); | |
612 | if (proc == inf->wait.thread) | |
613 | inf_clear_wait (inf); | |
614 | if (proc == inf->signal_thread) | |
615 | inf->signal_thread = 0; | |
616 | ||
617 | if (proc->port != MACH_PORT_NULL) | |
618 | { | |
619 | if (proc->exc_port != MACH_PORT_NULL) | |
620 | /* Restore the original exception port. */ | |
621 | proc_restore_exc_port (proc); | |
622 | if (proc->cur_sc != 0) | |
623 | /* Resume the thread/task. */ | |
624 | { | |
625 | proc->sc = 0; | |
626 | proc_update_sc (proc); | |
627 | } | |
628 | mach_port_deallocate (mach_task_self (), proc->port); | |
629 | } | |
630 | ||
b8c9b27d | 631 | xfree (proc); |
c906108c SS |
632 | return next; |
633 | } | |
942fd805 | 634 | |
c906108c | 635 | \f |
c5aa993b | 636 | struct inf * |
fba45db2 | 637 | make_inf (void) |
c906108c SS |
638 | { |
639 | struct inf *inf = malloc (sizeof (struct inf)); | |
640 | ||
641 | if (!inf) | |
642 | return 0; | |
643 | ||
644 | inf->task = 0; | |
645 | inf->threads = 0; | |
646 | inf->threads_up_to_date = 0; | |
647 | inf->pid = 0; | |
648 | inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS; | |
649 | inf->wait.thread = 0; | |
650 | inf->wait.exc.handler = MACH_PORT_NULL; | |
651 | inf->wait.exc.reply = MACH_PORT_NULL; | |
652 | inf->step_thread = 0; | |
653 | inf->signal_thread = 0; | |
654 | inf->event_port = MACH_PORT_NULL; | |
c906108c | 655 | inf->running = 0; |
cce74817 JM |
656 | inf->stopped = 0; |
657 | inf->nomsg = 1; | |
c906108c SS |
658 | inf->traced = 0; |
659 | inf->no_wait = 0; | |
660 | inf->pending_execs = 0; | |
661 | inf->pause_sc = 1; | |
662 | inf->detach_sc = 0; | |
663 | inf->default_thread_run_sc = 0; | |
664 | inf->default_thread_pause_sc = 0; | |
665 | inf->default_thread_detach_sc = 0; | |
666 | inf->want_signals = 1; /* By default */ | |
667 | inf->want_exceptions = 1; /* By default */ | |
668 | ||
669 | return inf; | |
670 | } | |
671 | ||
942fd805 | 672 | /* Clear INF's target wait status. */ |
c906108c SS |
673 | void |
674 | inf_clear_wait (struct inf *inf) | |
675 | { | |
676 | inf_debug (inf, "clearing wait"); | |
677 | inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS; | |
678 | inf->wait.thread = 0; | |
679 | inf->wait.suppress = 0; | |
680 | if (inf->wait.exc.handler != MACH_PORT_NULL) | |
681 | { | |
682 | mach_port_deallocate (mach_task_self (), inf->wait.exc.handler); | |
683 | inf->wait.exc.handler = MACH_PORT_NULL; | |
684 | } | |
685 | if (inf->wait.exc.reply != MACH_PORT_NULL) | |
686 | { | |
687 | mach_port_deallocate (mach_task_self (), inf->wait.exc.reply); | |
688 | inf->wait.exc.reply = MACH_PORT_NULL; | |
689 | } | |
690 | } | |
942fd805 | 691 | |
c906108c SS |
692 | \f |
693 | void | |
694 | inf_cleanup (struct inf *inf) | |
695 | { | |
696 | inf_debug (inf, "cleanup"); | |
697 | ||
698 | inf_clear_wait (inf); | |
699 | ||
700 | inf_set_pid (inf, -1); | |
701 | inf->pid = 0; | |
cce74817 JM |
702 | inf->running = 0; |
703 | inf->stopped = 0; | |
704 | inf->nomsg = 1; | |
c906108c SS |
705 | inf->traced = 0; |
706 | inf->no_wait = 0; | |
c906108c SS |
707 | inf->pending_execs = 0; |
708 | ||
709 | if (inf->event_port) | |
710 | { | |
711 | mach_port_destroy (mach_task_self (), inf->event_port); | |
712 | inf->event_port = MACH_PORT_NULL; | |
713 | } | |
714 | } | |
715 | ||
716 | void | |
717 | inf_startup (struct inf *inf, int pid) | |
718 | { | |
719 | error_t err; | |
720 | ||
721 | inf_debug (inf, "startup: pid = %d", pid); | |
722 | ||
723 | inf_cleanup (inf); | |
724 | ||
725 | /* Make the port on which we receive all events. */ | |
726 | err = mach_port_allocate (mach_task_self (), | |
727 | MACH_PORT_RIGHT_RECEIVE, &inf->event_port); | |
728 | if (err) | |
729 | error ("Error allocating event port: %s", strerror (err)); | |
730 | ||
731 | /* Make a send right for it, so we can easily copy it for other people. */ | |
732 | mach_port_insert_right (mach_task_self (), inf->event_port, | |
733 | inf->event_port, MACH_MSG_TYPE_MAKE_SEND); | |
734 | inf_set_pid (inf, pid); | |
735 | } | |
942fd805 | 736 | |
c906108c | 737 | \f |
942fd805 | 738 | /* Close current process, if any, and attach INF to process PORT. */ |
c5aa993b | 739 | void |
c906108c SS |
740 | inf_set_pid (struct inf *inf, pid_t pid) |
741 | { | |
742 | task_t task_port; | |
743 | struct proc *task = inf->task; | |
744 | ||
745 | inf_debug (inf, "setting pid: %d", pid); | |
746 | ||
747 | if (pid < 0) | |
748 | task_port = MACH_PORT_NULL; | |
749 | else | |
750 | { | |
751 | error_t err = proc_pid2task (proc_server, pid, &task_port); | |
752 | if (err) | |
753 | error ("Error getting task for pid %d: %s", pid, strerror (err)); | |
754 | } | |
755 | ||
756 | inf_debug (inf, "setting task: %d", task_port); | |
757 | ||
758 | if (inf->pause_sc) | |
759 | task_suspend (task_port); | |
760 | ||
761 | if (task && task->port != task_port) | |
762 | { | |
763 | inf->task = 0; | |
764 | inf_validate_procs (inf); /* Trash all the threads. */ | |
765 | _proc_free (task); /* And the task. */ | |
766 | } | |
767 | ||
768 | if (task_port != MACH_PORT_NULL) | |
769 | { | |
770 | inf->task = make_proc (inf, task_port, PROC_TID_TASK); | |
771 | inf->threads_up_to_date = 0; | |
772 | } | |
773 | ||
774 | if (inf->task) | |
775 | { | |
776 | inf->pid = pid; | |
777 | if (inf->pause_sc) | |
942fd805 MK |
778 | /* Reflect task_suspend above. */ |
779 | inf->task->sc = inf->task->cur_sc = 1; | |
c906108c SS |
780 | } |
781 | else | |
782 | inf->pid = -1; | |
783 | } | |
942fd805 | 784 | |
c906108c | 785 | \f |
cce74817 JM |
786 | /* Validates INF's stopped, nomsg and traced field from the actual |
787 | proc server state. Note that the traced field is only updated from | |
788 | the proc server state if we do not have a message port. If we do | |
789 | have a message port we'd better look at the tracemask itself. */ | |
c906108c | 790 | static void |
cce74817 | 791 | inf_validate_procinfo (struct inf *inf) |
c906108c SS |
792 | { |
793 | char *noise; | |
794 | mach_msg_type_number_t noise_len = 0; | |
795 | struct procinfo *pi; | |
796 | mach_msg_type_number_t pi_len = 0; | |
797 | int info_flags = 0; | |
798 | error_t err = | |
c5aa993b | 799 | proc_getprocinfo (proc_server, inf->pid, &info_flags, |
f75c77fc | 800 | (procinfo_t *) &pi, &pi_len, &noise, &noise_len); |
c906108c | 801 | |
c5aa993b | 802 | if (!err) |
c906108c SS |
803 | { |
804 | inf->stopped = !!(pi->state & PI_STOPPED); | |
cce74817 JM |
805 | inf->nomsg = !!(pi->state & PI_NOMSG); |
806 | if (inf->nomsg) | |
807 | inf->traced = !!(pi->state & PI_TRACED); | |
c5aa993b | 808 | vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len); |
c906108c | 809 | if (noise_len > 0) |
c5aa993b | 810 | vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len); |
c906108c SS |
811 | } |
812 | } | |
813 | ||
f90b2b1d JK |
814 | /* Validates INF's task suspend count. If it's higher than we expect, |
815 | verify with the user before `stealing' the extra count. */ | |
c906108c SS |
816 | static void |
817 | inf_validate_task_sc (struct inf *inf) | |
818 | { | |
f90b2b1d JK |
819 | char *noise; |
820 | mach_msg_type_number_t noise_len = 0; | |
821 | struct procinfo *pi; | |
822 | mach_msg_type_number_t pi_len = 0; | |
823 | int info_flags = PI_FETCH_TASKINFO; | |
824 | int suspend_count = -1; | |
825 | error_t err; | |
c906108c | 826 | |
f90b2b1d JK |
827 | retry: |
828 | err = proc_getprocinfo (proc_server, inf->pid, &info_flags, | |
942fd805 | 829 | (procinfo_t *) &pi, &pi_len, &noise, &noise_len); |
c906108c | 830 | if (err) |
f90b2b1d JK |
831 | { |
832 | inf->task->dead = 1; /* oh well */ | |
833 | return; | |
834 | } | |
835 | ||
836 | if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1) | |
837 | { | |
838 | /* The proc server might have suspended the task while stopping | |
839 | it. This happens when the task is handling a traced signal. | |
840 | Refetch the suspend count. The proc server should be | |
841 | finished stopping the task by now. */ | |
842 | suspend_count = pi->taskinfo.suspend_count; | |
843 | goto retry; | |
844 | } | |
845 | ||
846 | suspend_count = pi->taskinfo.suspend_count; | |
847 | ||
848 | vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len); | |
849 | if (noise_len > 0) | |
850 | vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len); | |
851 | ||
852 | if (inf->task->cur_sc < suspend_count) | |
c906108c SS |
853 | { |
854 | int abort; | |
855 | ||
856 | target_terminal_ours (); /* Allow I/O. */ | |
f90b2b1d JK |
857 | abort = !query ("Pid %d has an additional task suspend count of %d;" |
858 | " clear it? ", inf->pid, | |
859 | suspend_count - inf->task->cur_sc); | |
c5aa993b | 860 | target_terminal_inferior (); /* Give it back to the child. */ |
c906108c SS |
861 | |
862 | if (abort) | |
863 | error ("Additional task suspend count left untouched."); | |
864 | ||
f90b2b1d | 865 | inf->task->cur_sc = suspend_count; |
c906108c SS |
866 | } |
867 | } | |
868 | ||
942fd805 MK |
869 | /* Turns tracing for INF on or off, depending on ON, unless it already |
870 | is. If INF is running, the resume_sc count of INF's threads will | |
871 | be modified, and the signal thread will briefly be run to change | |
872 | the trace state. */ | |
c906108c SS |
873 | void |
874 | inf_set_traced (struct inf *inf, int on) | |
875 | { | |
942fd805 MK |
876 | if (on == inf->traced) |
877 | return; | |
878 | ||
879 | if (inf->task && !inf->task->dead) | |
880 | /* Make it take effect immediately. */ | |
881 | { | |
882 | sigset_t mask = on ? ~(sigset_t) 0 : 0; | |
883 | error_t err = | |
c5aa993b | 884 | INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport, |
942fd805 MK |
885 | INIT_TRACEMASK, mask)); |
886 | if (err == EIEIO) | |
887 | { | |
888 | if (on) | |
889 | warning ("Can't modify tracing state for pid %d: %s", | |
890 | inf->pid, "No signal thread"); | |
c906108c | 891 | inf->traced = on; |
942fd805 MK |
892 | } |
893 | else if (err) | |
894 | warning ("Can't modify tracing state for pid %d: %s", | |
895 | inf->pid, strerror (err)); | |
896 | else | |
897 | inf->traced = on; | |
898 | } | |
899 | else | |
900 | inf->traced = on; | |
c906108c | 901 | } |
942fd805 | 902 | |
c906108c | 903 | \f |
942fd805 MK |
904 | /* Makes all the real suspend count deltas of all the procs in INF |
905 | match the desired values. Careful to always do thread/task suspend | |
906 | counts in the safe order. Returns true if at least one thread is | |
907 | thought to be running. */ | |
c906108c SS |
908 | int |
909 | inf_update_suspends (struct inf *inf) | |
910 | { | |
911 | struct proc *task = inf->task; | |
912 | /* We don't have to update INF->threads even though we're iterating over it | |
913 | because we'll change a thread only if it already has an existing proc | |
914 | entry. */ | |
915 | ||
916 | inf_debug (inf, "updating suspend counts"); | |
917 | ||
918 | if (task) | |
919 | { | |
920 | struct proc *thread; | |
921 | int task_running = (task->sc == 0), thread_running = 0; | |
922 | ||
923 | if (task->sc > task->cur_sc) | |
924 | /* The task is becoming _more_ suspended; do before any threads. */ | |
925 | task_running = proc_update_sc (task); | |
926 | ||
927 | if (inf->pending_execs) | |
928 | /* When we're waiting for an exec, things may be happening behind our | |
929 | back, so be conservative. */ | |
930 | thread_running = 1; | |
931 | ||
932 | /* Do all the thread suspend counts. */ | |
933 | for (thread = inf->threads; thread; thread = thread->next) | |
934 | thread_running |= proc_update_sc (thread); | |
935 | ||
936 | if (task->sc != task->cur_sc) | |
937 | /* We didn't do the task first, because we wanted to wait for the | |
938 | threads; do it now. */ | |
939 | task_running = proc_update_sc (task); | |
940 | ||
941 | inf_debug (inf, "%srunning...", | |
942 | (thread_running && task_running) ? "" : "not "); | |
943 | ||
944 | inf->running = thread_running && task_running; | |
945 | ||
946 | /* Once any thread has executed some code, we can't depend on the | |
c5aa993b | 947 | threads list any more. */ |
c906108c SS |
948 | if (inf->running) |
949 | inf->threads_up_to_date = 0; | |
950 | ||
951 | return inf->running; | |
952 | } | |
953 | ||
954 | return 0; | |
955 | } | |
942fd805 | 956 | |
c906108c SS |
957 | \f |
958 | /* Converts a GDB pid to a struct proc. */ | |
959 | struct proc * | |
960 | inf_tid_to_thread (struct inf *inf, int tid) | |
961 | { | |
962 | struct proc *thread = inf->threads; | |
963 | ||
964 | while (thread) | |
965 | if (thread->tid == tid) | |
966 | return thread; | |
967 | else | |
968 | thread = thread->next; | |
969 | return 0; | |
970 | } | |
971 | ||
972 | /* Converts a thread port to a struct proc. */ | |
973 | struct proc * | |
974 | inf_port_to_thread (struct inf *inf, mach_port_t port) | |
975 | { | |
976 | struct proc *thread = inf->threads; | |
977 | while (thread) | |
978 | if (thread->port == port) | |
979 | return thread; | |
980 | else | |
981 | thread = thread->next; | |
982 | return 0; | |
983 | } | |
942fd805 | 984 | |
c906108c SS |
985 | \f |
986 | /* Make INF's list of threads be consistent with reality of TASK. */ | |
987 | void | |
988 | inf_validate_procs (struct inf *inf) | |
989 | { | |
c906108c | 990 | thread_array_t threads; |
942fd805 | 991 | mach_msg_type_number_t num_threads, i; |
c906108c SS |
992 | struct proc *task = inf->task; |
993 | ||
994 | /* If no threads are currently running, this function will guarantee that | |
995 | things are up to date. The exception is if there are zero threads -- | |
996 | then it is almost certainly in an odd state, and probably some outside | |
997 | agent will create threads. */ | |
998 | inf->threads_up_to_date = inf->threads ? !inf->running : 0; | |
999 | ||
1000 | if (task) | |
1001 | { | |
1002 | error_t err = task_threads (task->port, &threads, &num_threads); | |
1003 | inf_debug (inf, "fetching threads"); | |
1004 | if (err) | |
1005 | /* TASK must be dead. */ | |
1006 | { | |
1007 | task->dead = 1; | |
1008 | task = 0; | |
1009 | } | |
1010 | } | |
1011 | ||
1012 | if (!task) | |
1013 | { | |
1014 | num_threads = 0; | |
1015 | inf_debug (inf, "no task"); | |
1016 | } | |
1017 | ||
1018 | { | |
942fd805 MK |
1019 | /* Make things normally linear. */ |
1020 | mach_msg_type_number_t search_start = 0; | |
c906108c SS |
1021 | /* Which thread in PROCS corresponds to each task thread, & the task. */ |
1022 | struct proc *matched[num_threads + 1]; | |
1023 | /* The last thread in INF->threads, so we can add to the end. */ | |
1024 | struct proc *last = 0; | |
1025 | /* The current thread we're considering. */ | |
1026 | struct proc *thread = inf->threads; | |
1027 | ||
1028 | bzero (matched, sizeof (matched)); | |
1029 | ||
1030 | while (thread) | |
1031 | { | |
942fd805 | 1032 | mach_msg_type_number_t left; |
c906108c SS |
1033 | |
1034 | for (i = search_start, left = num_threads; left; i++, left--) | |
1035 | { | |
1036 | if (i >= num_threads) | |
c5aa993b | 1037 | i -= num_threads; /* I wrapped around. */ |
c906108c SS |
1038 | if (thread->port == threads[i]) |
1039 | /* We already know about this thread. */ | |
1040 | { | |
1041 | matched[i] = thread; | |
1042 | last = thread; | |
1043 | thread = thread->next; | |
1044 | search_start++; | |
1045 | break; | |
1046 | } | |
1047 | } | |
1048 | ||
c5aa993b | 1049 | if (!left) |
c906108c SS |
1050 | { |
1051 | proc_debug (thread, "died!"); | |
1052 | thread->port = MACH_PORT_NULL; | |
c5aa993b | 1053 | thread = _proc_free (thread); /* THREAD is dead. */ |
c906108c SS |
1054 | (last ? last->next : inf->threads) = thread; |
1055 | } | |
1056 | } | |
1057 | ||
1058 | for (i = 0; i < num_threads; i++) | |
942fd805 MK |
1059 | { |
1060 | if (matched[i]) | |
1061 | /* Throw away the duplicate send right. */ | |
1062 | mach_port_deallocate (mach_task_self (), threads[i]); | |
1063 | else | |
1064 | /* THREADS[I] is a thread we don't know about yet! */ | |
1065 | { | |
1066 | thread = make_proc (inf, threads[i], next_thread_id++); | |
1067 | (last ? last->next : inf->threads) = thread; | |
1068 | last = thread; | |
1069 | proc_debug (thread, "new thread: %d", threads[i]); | |
1070 | add_thread (thread->tid); /* Tell GDB's generic thread code. */ | |
1071 | } | |
1072 | } | |
c906108c | 1073 | |
c5aa993b | 1074 | vm_deallocate (mach_task_self (), |
942fd805 | 1075 | (vm_address_t) threads, (num_threads * sizeof (thread_t))); |
c906108c SS |
1076 | } |
1077 | } | |
942fd805 | 1078 | |
c906108c SS |
1079 | \f |
1080 | /* Makes sure that INF's thread list is synced with the actual process. */ | |
1081 | inline int | |
1082 | inf_update_procs (struct inf *inf) | |
1083 | { | |
c5aa993b | 1084 | if (!inf->task) |
c906108c | 1085 | return 0; |
c5aa993b | 1086 | if (!inf->threads_up_to_date) |
c906108c SS |
1087 | inf_validate_procs (inf); |
1088 | return !!inf->task; | |
1089 | } | |
1090 | ||
1091 | /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0, | |
1092 | and others are set to their run_sc if RUN_OTHERS is true, and otherwise | |
1093 | their pause_sc. */ | |
c5aa993b | 1094 | inline void |
c906108c SS |
1095 | inf_set_threads_resume_sc (struct inf *inf, |
1096 | struct proc *run_thread, int run_others) | |
1097 | { | |
1098 | struct proc *thread; | |
1099 | inf_update_procs (inf); | |
1100 | for (thread = inf->threads; thread; thread = thread->next) | |
1101 | if (thread == run_thread) | |
1102 | thread->resume_sc = 0; | |
1103 | else if (run_others) | |
1104 | thread->resume_sc = thread->run_sc; | |
1105 | else | |
1106 | thread->resume_sc = thread->pause_sc; | |
1107 | } | |
942fd805 | 1108 | |
c906108c SS |
1109 | \f |
1110 | /* Cause INF to continue execution immediately; individual threads may still | |
1111 | be suspended (but their suspend counts will be updated). */ | |
1112 | inline void | |
1113 | inf_resume (struct inf *inf) | |
1114 | { | |
1115 | struct proc *thread; | |
1116 | ||
1117 | inf_update_procs (inf); | |
1118 | ||
1119 | for (thread = inf->threads; thread; thread = thread->next) | |
1120 | thread->sc = thread->resume_sc; | |
1121 | ||
1122 | if (inf->task) | |
1123 | { | |
c5aa993b | 1124 | if (!inf->pending_execs) |
c906108c SS |
1125 | /* Try to make sure our task count is correct -- in the case where |
1126 | we're waiting for an exec though, things are too volatile, so just | |
1127 | assume things will be reasonable (which they usually will be). */ | |
1128 | inf_validate_task_sc (inf); | |
1129 | inf->task->sc = 0; | |
1130 | } | |
1131 | ||
1132 | inf_update_suspends (inf); | |
1133 | } | |
1134 | ||
1135 | /* Cause INF to stop execution immediately; individual threads may still | |
1136 | be running. */ | |
1137 | inline void | |
1138 | inf_suspend (struct inf *inf) | |
1139 | { | |
1140 | struct proc *thread; | |
1141 | ||
1142 | inf_update_procs (inf); | |
1143 | ||
1144 | for (thread = inf->threads; thread; thread = thread->next) | |
1145 | thread->sc = thread->pause_sc; | |
1146 | ||
1147 | if (inf->task) | |
1148 | inf->task->sc = inf->pause_sc; | |
1149 | ||
1150 | inf_update_suspends (inf); | |
1151 | } | |
942fd805 | 1152 | |
c906108c | 1153 | \f |
942fd805 MK |
1154 | /* INF has one thread PROC that is in single-stepping mode. This |
1155 | function changes it to be PROC, changing any old step_thread to be | |
1156 | a normal one. A PROC of 0 clears any existing value. */ | |
c906108c SS |
1157 | void |
1158 | inf_set_step_thread (struct inf *inf, struct proc *thread) | |
1159 | { | |
bf62e5b4 | 1160 | gdb_assert (!thread || proc_is_thread (thread)); |
c906108c SS |
1161 | |
1162 | if (thread) | |
1163 | inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid); | |
1164 | else | |
1165 | inf_debug (inf, "clearing step thread"); | |
1166 | ||
1167 | if (inf->step_thread != thread) | |
1168 | { | |
1169 | if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL) | |
c5aa993b | 1170 | if (!proc_trace (inf->step_thread, 0)) |
c906108c SS |
1171 | return; |
1172 | if (thread && proc_trace (thread, 1)) | |
1173 | inf->step_thread = thread; | |
1174 | else | |
1175 | inf->step_thread = 0; | |
1176 | } | |
1177 | } | |
942fd805 | 1178 | |
c906108c SS |
1179 | \f |
1180 | /* Set up the thread resume_sc's so that only the signal thread is running | |
1181 | (plus whatever other thread are set to always run). Returns true if we | |
1182 | did so, or false if we can't find a signal thread. */ | |
1183 | inline int | |
1184 | inf_set_threads_resume_sc_for_signal_thread (struct inf *inf) | |
1185 | { | |
1186 | if (inf->signal_thread) | |
1187 | { | |
1188 | inf_set_threads_resume_sc (inf, inf->signal_thread, 0); | |
1189 | return 1; | |
1190 | } | |
1191 | else | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | static void | |
1196 | inf_update_signal_thread (struct inf *inf) | |
1197 | { | |
1198 | /* XXX for now we assume that if there's a msgport, the 2nd thread is | |
1199 | the signal thread. */ | |
1200 | inf->signal_thread = inf->threads ? inf->threads->next : 0; | |
1201 | } | |
942fd805 | 1202 | |
c906108c SS |
1203 | \f |
1204 | /* Detachs from INF's inferior task, letting it run once again... */ | |
1205 | void | |
1206 | inf_detach (struct inf *inf) | |
1207 | { | |
1208 | struct proc *task = inf->task; | |
1209 | ||
1210 | inf_debug (inf, "detaching..."); | |
1211 | ||
1212 | inf_clear_wait (inf); | |
1213 | inf_set_step_thread (inf, 0); | |
1214 | ||
1215 | if (task) | |
1216 | { | |
1217 | struct proc *thread; | |
1218 | ||
cce74817 JM |
1219 | inf_validate_procinfo (inf); |
1220 | ||
c906108c SS |
1221 | inf_set_traced (inf, 0); |
1222 | if (inf->stopped) | |
cce74817 JM |
1223 | { |
1224 | if (inf->nomsg) | |
1225 | inf_continue (inf); | |
1226 | else | |
1227 | inf_signal (inf, TARGET_SIGNAL_0); | |
1228 | } | |
c906108c SS |
1229 | |
1230 | proc_restore_exc_port (task); | |
1231 | task->sc = inf->detach_sc; | |
1232 | ||
1233 | for (thread = inf->threads; thread; thread = thread->next) | |
1234 | { | |
1235 | proc_restore_exc_port (thread); | |
1236 | thread->sc = thread->detach_sc; | |
1237 | } | |
1238 | ||
1239 | inf_update_suspends (inf); | |
1240 | } | |
1241 | ||
1242 | inf_cleanup (inf); | |
1243 | } | |
1244 | ||
942fd805 MK |
1245 | /* Attaches INF to the process with process id PID, returning it in a |
1246 | suspended state suitable for debugging. */ | |
c906108c SS |
1247 | void |
1248 | inf_attach (struct inf *inf, int pid) | |
1249 | { | |
1250 | inf_debug (inf, "attaching: %d", pid); | |
1251 | ||
1252 | if (inf->pid) | |
1253 | inf_detach (inf); | |
1254 | ||
1255 | inf_startup (inf, pid); | |
1256 | } | |
942fd805 | 1257 | |
c906108c SS |
1258 | \f |
1259 | /* Makes sure that we've got our exception ports entrenched in the process. */ | |
c5aa993b JM |
1260 | void |
1261 | inf_steal_exc_ports (struct inf *inf) | |
c906108c SS |
1262 | { |
1263 | struct proc *thread; | |
1264 | ||
1265 | inf_debug (inf, "stealing exception ports"); | |
1266 | ||
1267 | inf_set_step_thread (inf, 0); /* The step thread is special. */ | |
1268 | ||
1269 | proc_steal_exc_port (inf->task, inf->event_port); | |
1270 | for (thread = inf->threads; thread; thread = thread->next) | |
1271 | proc_steal_exc_port (thread, MACH_PORT_NULL); | |
1272 | } | |
1273 | ||
1274 | /* Makes sure the process has its own exception ports. */ | |
c5aa993b JM |
1275 | void |
1276 | inf_restore_exc_ports (struct inf *inf) | |
c906108c SS |
1277 | { |
1278 | struct proc *thread; | |
1279 | ||
1280 | inf_debug (inf, "restoring exception ports"); | |
1281 | ||
1282 | inf_set_step_thread (inf, 0); /* The step thread is special. */ | |
1283 | ||
1284 | proc_restore_exc_port (inf->task); | |
1285 | for (thread = inf->threads; thread; thread = thread->next) | |
1286 | proc_restore_exc_port (thread); | |
1287 | } | |
942fd805 | 1288 | |
c906108c SS |
1289 | \f |
1290 | /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even | |
1291 | signal 0, will continue it. INF is assumed to be in a paused state, and | |
1292 | the resume_sc's of INF's threads may be affected. */ | |
1293 | void | |
1294 | inf_signal (struct inf *inf, enum target_signal sig) | |
1295 | { | |
1296 | error_t err = 0; | |
1297 | int host_sig = target_signal_to_host (sig); | |
1298 | ||
1299 | #define NAME target_signal_to_name (sig) | |
1300 | ||
1301 | if (host_sig >= _NSIG) | |
1302 | /* A mach exception. Exceptions are encoded in the signal space by | |
1303 | putting them after _NSIG; this assumes they're positive (and not | |
1304 | extremely large)! */ | |
1305 | { | |
1306 | struct inf_wait *w = &inf->wait; | |
1307 | if (w->status.kind == TARGET_WAITKIND_STOPPED | |
1308 | && w->status.value.sig == sig | |
1309 | && w->thread && !w->thread->aborted) | |
1310 | /* We're passing through the last exception we received. This is | |
1311 | kind of bogus, because exceptions are per-thread whereas gdb | |
1312 | treats signals as per-process. We just forward the exception to | |
1313 | the correct handler, even it's not for the same thread as TID -- | |
1314 | i.e., we pretend it's global. */ | |
1315 | { | |
1316 | struct exc_state *e = &w->exc; | |
1317 | inf_debug (inf, "passing through exception:" | |
1318 | " task = %d, thread = %d, exc = %d" | |
1319 | ", code = %d, subcode = %d", | |
1320 | w->thread->port, inf->task->port, | |
1321 | e->exception, e->code, e->subcode); | |
1322 | err = | |
1323 | exception_raise_request (e->handler, | |
1324 | e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE, | |
1325 | w->thread->port, inf->task->port, | |
1326 | e->exception, e->code, e->subcode); | |
1327 | } | |
1328 | else | |
1329 | error ("Can't forward spontaneous exception (%s).", NAME); | |
1330 | } | |
1331 | else | |
1332 | /* A Unix signal. */ | |
c5aa993b JM |
1333 | if (inf->stopped) |
1334 | /* The process is stopped and expecting a signal. Just send off a | |
1335 | request and let it get handled when we resume everything. */ | |
1336 | { | |
1337 | inf_debug (inf, "sending %s to stopped process", NAME); | |
1338 | err = | |
1339 | INF_MSGPORT_RPC (inf, | |
1340 | msg_sig_post_untraced_request (msgport, | |
1341 | inf->event_port, | |
1342 | MACH_MSG_TYPE_MAKE_SEND_ONCE, | |
1343 | host_sig, 0, | |
1344 | refport)); | |
1345 | if (!err) | |
1346 | /* Posting an untraced signal automatically continues it. | |
1347 | We clear this here rather than when we get the reply | |
1348 | because we'd rather assume it's not stopped when it | |
1349 | actually is, than the reverse. */ | |
1350 | inf->stopped = 0; | |
1351 | } | |
1352 | else | |
1353 | /* It's not expecting it. We have to let just the signal thread | |
1354 | run, and wait for it to get into a reasonable state before we | |
1355 | can continue the rest of the process. When we finally resume the | |
1356 | process the signal we request will be the very first thing that | |
1357 | happens. */ | |
1358 | { | |
942fd805 MK |
1359 | inf_debug (inf, "sending %s to unstopped process" |
1360 | " (so resuming signal thread)", NAME); | |
c5aa993b | 1361 | err = |
942fd805 MK |
1362 | INF_RESUME_MSGPORT_RPC (inf, |
1363 | msg_sig_post_untraced (msgport, host_sig, | |
1364 | 0, refport)); | |
c5aa993b | 1365 | } |
c906108c SS |
1366 | |
1367 | if (err == EIEIO) | |
1368 | /* Can't do too much... */ | |
1369 | warning ("Can't deliver signal %s: No signal thread.", NAME); | |
1370 | else if (err) | |
1371 | warning ("Delivering signal %s: %s", NAME, strerror (err)); | |
1372 | ||
1373 | #undef NAME | |
1374 | } | |
942fd805 | 1375 | |
c906108c | 1376 | \f |
cce74817 JM |
1377 | /* Continue INF without delivering a signal. This is meant to be used |
1378 | when INF does not have a message port. */ | |
1379 | void | |
1380 | inf_continue (struct inf *inf) | |
1381 | { | |
1382 | process_t proc; | |
1383 | error_t err = proc_pid2proc (proc_server, inf->pid, &proc); | |
1384 | ||
c5aa993b | 1385 | if (!err) |
cce74817 JM |
1386 | { |
1387 | inf_debug (inf, "continuing process"); | |
1388 | ||
1389 | err = proc_mark_cont (proc); | |
c5aa993b | 1390 | if (!err) |
cce74817 JM |
1391 | { |
1392 | struct proc *thread; | |
1393 | ||
1394 | for (thread = inf->threads; thread; thread = thread->next) | |
1395 | thread_resume (thread->port); | |
c5aa993b | 1396 | |
cce74817 JM |
1397 | inf->stopped = 0; |
1398 | } | |
1399 | } | |
1400 | ||
1401 | if (err) | |
1402 | warning ("Can't continue process: %s", strerror (err)); | |
1403 | } | |
942fd805 | 1404 | |
cce74817 | 1405 | \f |
c906108c SS |
1406 | /* The inferior used for all gdb target ops. */ |
1407 | struct inf *current_inferior = 0; | |
1408 | ||
1409 | /* The inferior being waited for by gnu_wait. Since GDB is decidely not | |
1410 | multi-threaded, we don't bother to lock this. */ | |
1411 | struct inf *waiting_inf; | |
1412 | ||
1413 | /* Wait for something to happen in the inferior, returning what in STATUS. */ | |
1414 | static int | |
1415 | gnu_wait (int tid, struct target_waitstatus *status) | |
1416 | { | |
c5aa993b JM |
1417 | struct msg |
1418 | { | |
1419 | mach_msg_header_t hdr; | |
1420 | mach_msg_type_t type; | |
1421 | int data[8000]; | |
942fd805 | 1422 | } msg; |
c906108c SS |
1423 | error_t err; |
1424 | struct proc *thread; | |
1425 | struct inf *inf = current_inferior; | |
1426 | ||
942fd805 MK |
1427 | extern int exc_server (mach_msg_header_t *, mach_msg_header_t *); |
1428 | extern int msg_reply_server (mach_msg_header_t *, mach_msg_header_t *); | |
1429 | extern int notify_server (mach_msg_header_t *, mach_msg_header_t *); | |
1430 | extern int process_reply_server (mach_msg_header_t *, mach_msg_header_t *); | |
1431 | ||
bf62e5b4 | 1432 | gdb_assert (inf->task); |
c906108c SS |
1433 | |
1434 | if (!inf->threads && !inf->pending_execs) | |
1435 | /* No threads! Assume that maybe some outside agency is frobbing our | |
1436 | task, and really look for new threads. If we can't find any, just tell | |
1437 | the user to try again later. */ | |
1438 | { | |
1439 | inf_validate_procs (inf); | |
1440 | if (!inf->threads && !inf->task->dead) | |
1441 | error ("There are no threads; try again later."); | |
1442 | } | |
1443 | ||
1444 | waiting_inf = inf; | |
1445 | ||
1446 | inf_debug (inf, "waiting for: %d", tid); | |
1447 | ||
c5aa993b | 1448 | rewait: |
c906108c SS |
1449 | if (proc_wait_pid != inf->pid && !inf->no_wait) |
1450 | /* Always get information on events from the proc server. */ | |
1451 | { | |
1452 | inf_debug (inf, "requesting wait on pid %d", inf->pid); | |
1453 | ||
1454 | if (proc_wait_pid) | |
1455 | /* The proc server is single-threaded, and only allows a single | |
1456 | outstanding wait request, so we have to cancel the previous one. */ | |
1457 | { | |
1458 | inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid); | |
1459 | interrupt_operation (proc_server, 0); | |
1460 | } | |
1461 | ||
1462 | err = | |
1463 | proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED); | |
1464 | if (err) | |
1465 | warning ("wait request failed: %s", strerror (err)); | |
1466 | else | |
1467 | { | |
1468 | inf_debug (inf, "waits pending: %d", proc_waits_pending); | |
1469 | proc_wait_pid = inf->pid; | |
942fd805 MK |
1470 | /* Even if proc_waits_pending was > 0 before, we still won't |
1471 | get any other replies, because it was either from a | |
1472 | different INF, or a different process attached to INF -- | |
1473 | and the event port, which is the wait reply port, changes | |
1474 | when you switch processes. */ | |
c906108c SS |
1475 | proc_waits_pending = 1; |
1476 | } | |
1477 | } | |
1478 | ||
1479 | inf_clear_wait (inf); | |
1480 | ||
1481 | /* What can happen? (1) Dead name notification; (2) Exceptions arrive; | |
1482 | (3) wait reply from the proc server. */ | |
1483 | ||
1484 | inf_debug (inf, "waiting for an event..."); | |
1485 | err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT, | |
1486 | 0, sizeof (struct msg), inf->event_port, | |
1487 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
1488 | ||
1489 | /* Re-suspend the task. */ | |
1490 | inf_suspend (inf); | |
1491 | ||
1492 | if (!inf->task && inf->pending_execs) | |
1493 | /* When doing an exec, it's possible that the old task wasn't reused | |
1494 | (e.g., setuid execs). So if the task seems to have disappeared, | |
1495 | attempt to refetch it, as the pid should still be the same. */ | |
1496 | inf_set_pid (inf, inf->pid); | |
1497 | ||
1498 | if (err == EMACH_RCV_INTERRUPTED) | |
1499 | inf_debug (inf, "interrupted"); | |
1500 | else if (err) | |
1501 | error ("Couldn't wait for an event: %s", strerror (err)); | |
1502 | else | |
1503 | { | |
c5aa993b JM |
1504 | struct |
1505 | { | |
1506 | mach_msg_header_t hdr; | |
1507 | mach_msg_type_t err_type; | |
1508 | kern_return_t err; | |
1509 | char noise[200]; | |
1510 | } | |
1511 | reply; | |
c906108c SS |
1512 | |
1513 | inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id); | |
1514 | ||
1515 | /* Handle what we got. */ | |
c5aa993b JM |
1516 | if (!notify_server (&msg.hdr, &reply.hdr) |
1517 | && !exc_server (&msg.hdr, &reply.hdr) | |
1518 | && !process_reply_server (&msg.hdr, &reply.hdr) | |
1519 | && !msg_reply_server (&msg.hdr, &reply.hdr)) | |
c906108c SS |
1520 | /* Whatever it is, it's something strange. */ |
1521 | error ("Got a strange event, msg id = %d.", msg.hdr.msgh_id); | |
1522 | ||
1523 | if (reply.err) | |
1524 | error ("Handling event, msgid = %d: %s", | |
1525 | msg.hdr.msgh_id, strerror (reply.err)); | |
1526 | } | |
1527 | ||
1528 | if (inf->pending_execs) | |
1529 | /* We're waiting for the inferior to finish execing. */ | |
1530 | { | |
1531 | struct inf_wait *w = &inf->wait; | |
1532 | enum target_waitkind kind = w->status.kind; | |
1533 | ||
1534 | if (kind == TARGET_WAITKIND_SPURIOUS) | |
1535 | /* Since gdb is actually counting the number of times the inferior | |
1536 | stops, expecting one stop per exec, we only return major events | |
1537 | while execing. */ | |
1538 | { | |
1539 | w->suppress = 1; | |
1540 | inf_debug (inf, "pending_execs = %d, ignoring minor event", | |
1541 | inf->pending_execs); | |
1542 | } | |
1543 | else if (kind == TARGET_WAITKIND_STOPPED | |
1544 | && w->status.value.sig == TARGET_SIGNAL_TRAP) | |
1545 | /* Ah hah! A SIGTRAP from the inferior while starting up probably | |
1546 | means we've succesfully completed an exec! */ | |
1547 | { | |
1548 | if (--inf->pending_execs == 0) | |
1549 | /* We're done! */ | |
1550 | { | |
c5aa993b | 1551 | #if 0 /* do we need this? */ |
c906108c | 1552 | prune_threads (1); /* Get rid of the old shell threads */ |
c5aa993b | 1553 | renumber_threads (0); /* Give our threads reasonable names. */ |
c906108c SS |
1554 | #endif |
1555 | } | |
1556 | inf_debug (inf, "pending exec completed, pending_execs => %d", | |
1557 | inf->pending_execs); | |
1558 | } | |
1559 | else if (kind == TARGET_WAITKIND_STOPPED) | |
1560 | /* It's possible that this signal is because of a crashed process | |
1561 | being handled by the hurd crash server; in this case, the process | |
1562 | will have an extra task suspend, which we need to know about. | |
1563 | Since the code in inf_resume that normally checks for this is | |
1564 | disabled while INF->pending_execs, we do the check here instead. */ | |
1565 | inf_validate_task_sc (inf); | |
1566 | } | |
1567 | ||
1568 | if (inf->wait.suppress) | |
1569 | /* Some totally spurious event happened that we don't consider | |
1570 | worth returning to gdb. Just keep waiting. */ | |
1571 | { | |
1572 | inf_debug (inf, "suppressing return, rewaiting..."); | |
1573 | inf_resume (inf); | |
1574 | goto rewait; | |
1575 | } | |
1576 | ||
1577 | /* Pass back out our results. */ | |
1578 | bcopy (&inf->wait.status, status, sizeof (*status)); | |
1579 | ||
1580 | thread = inf->wait.thread; | |
1581 | if (thread) | |
1582 | tid = thread->tid; | |
1583 | else | |
1584 | thread = inf_tid_to_thread (inf, tid); | |
1585 | ||
1586 | if (!thread || thread->port == MACH_PORT_NULL) | |
942fd805 MK |
1587 | { |
1588 | /* TID is dead; try and find a new thread. */ | |
1589 | if (inf_update_procs (inf) && inf->threads) | |
1590 | tid = inf->threads->tid; /* The first available thread. */ | |
1591 | else | |
1592 | tid = inferior_pid; /* let wait_for_inferior handle exit case */ | |
1593 | } | |
c906108c SS |
1594 | |
1595 | if (thread && tid >= 0 && status->kind != TARGET_WAITKIND_SPURIOUS | |
1596 | && inf->pause_sc == 0 && thread->pause_sc == 0) | |
942fd805 MK |
1597 | /* If something actually happened to THREAD, make sure we |
1598 | suspend it. */ | |
c906108c SS |
1599 | { |
1600 | thread->sc = 1; | |
1601 | inf_update_suspends (inf); | |
c5aa993b | 1602 | } |
c906108c SS |
1603 | |
1604 | inf_debug (inf, "returning tid = %d, status = %s (%d)", tid, | |
1605 | status->kind == TARGET_WAITKIND_EXITED ? "EXITED" | |
1606 | : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED" | |
1607 | : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED" | |
1608 | : status->kind == TARGET_WAITKIND_LOADED ? "LOADED" | |
1609 | : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS" | |
1610 | : "?", | |
1611 | status->value.integer); | |
1612 | ||
1613 | return tid; | |
1614 | } | |
942fd805 | 1615 | |
c906108c SS |
1616 | \f |
1617 | /* The rpc handler called by exc_server. */ | |
1618 | error_t | |
1619 | S_exception_raise_request (mach_port_t port, mach_port_t reply_port, | |
1620 | thread_t thread_port, task_t task_port, | |
1621 | int exception, int code, int subcode) | |
1622 | { | |
1623 | struct inf *inf = waiting_inf; | |
1624 | struct proc *thread = inf_port_to_thread (inf, thread_port); | |
1625 | ||
1626 | inf_debug (waiting_inf, | |
1627 | "thread = %d, task = %d, exc = %d, code = %d, subcode = %d", | |
942fd805 | 1628 | thread_port, task_port, exception, code, subcode); |
c906108c SS |
1629 | |
1630 | if (!thread) | |
1631 | /* We don't know about thread? */ | |
1632 | { | |
1633 | inf_update_procs (inf); | |
1634 | thread = inf_port_to_thread (inf, thread_port); | |
1635 | if (!thread) | |
1636 | /* Give up, the generating thread is gone. */ | |
1637 | return 0; | |
1638 | } | |
1639 | ||
1640 | mach_port_deallocate (mach_task_self (), thread_port); | |
1641 | mach_port_deallocate (mach_task_self (), task_port); | |
1642 | ||
c5aa993b | 1643 | if (!thread->aborted) |
c906108c SS |
1644 | /* THREAD hasn't been aborted since this exception happened (abortion |
1645 | clears any exception state), so it must be real. */ | |
1646 | { | |
1647 | /* Store away the details; this will destroy any previous info. */ | |
1648 | inf->wait.thread = thread; | |
1649 | ||
1650 | inf->wait.status.kind = TARGET_WAITKIND_STOPPED; | |
1651 | ||
1652 | if (exception == EXC_BREAKPOINT) | |
1653 | /* GDB likes to get SIGTRAP for breakpoints. */ | |
1654 | { | |
1655 | inf->wait.status.value.sig = TARGET_SIGNAL_TRAP; | |
1656 | mach_port_deallocate (mach_task_self (), reply_port); | |
1657 | } | |
1658 | else | |
1659 | /* Record the exception so that we can forward it later. */ | |
1660 | { | |
1661 | if (thread->exc_port == port) | |
1662 | { | |
8e1a459b | 1663 | inf_debug (waiting_inf, "Handler is thread exception port <%d>", |
c906108c SS |
1664 | thread->saved_exc_port); |
1665 | inf->wait.exc.handler = thread->saved_exc_port; | |
1666 | } | |
1667 | else | |
1668 | { | |
8e1a459b | 1669 | inf_debug (waiting_inf, "Handler is task exception port <%d>", |
c906108c SS |
1670 | inf->task->saved_exc_port); |
1671 | inf->wait.exc.handler = inf->task->saved_exc_port; | |
bf62e5b4 | 1672 | gdb_assert (inf->task->exc_port == port); |
c906108c SS |
1673 | } |
1674 | if (inf->wait.exc.handler != MACH_PORT_NULL) | |
1675 | /* Add a reference to the exception handler. */ | |
1676 | mach_port_mod_refs (mach_task_self (), | |
1677 | inf->wait.exc.handler, MACH_PORT_RIGHT_SEND, | |
1678 | 1); | |
1679 | ||
1680 | inf->wait.exc.exception = exception; | |
1681 | inf->wait.exc.code = code; | |
1682 | inf->wait.exc.subcode = subcode; | |
1683 | inf->wait.exc.reply = reply_port; | |
1684 | ||
1685 | /* Exceptions are encoded in the signal space by putting them after | |
1686 | _NSIG; this assumes they're positive (and not extremely large)! */ | |
1687 | inf->wait.status.value.sig = | |
1688 | target_signal_from_host (_NSIG + exception); | |
1689 | } | |
1690 | } | |
1691 | else | |
1692 | /* A supppressed exception, which ignore. */ | |
1693 | { | |
1694 | inf->wait.suppress = 1; | |
1695 | mach_port_deallocate (mach_task_self (), reply_port); | |
1696 | } | |
1697 | ||
1698 | return 0; | |
1699 | } | |
942fd805 | 1700 | |
c906108c SS |
1701 | \f |
1702 | /* Fill in INF's wait field after a task has died without giving us more | |
1703 | detailed information. */ | |
1704 | void | |
1705 | inf_task_died_status (struct inf *inf) | |
1706 | { | |
1707 | warning ("Pid %d died with unknown exit status, using SIGKILL.", inf->pid); | |
1708 | inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED; | |
1709 | inf->wait.status.value.sig = TARGET_SIGNAL_KILL; | |
1710 | } | |
1711 | ||
1712 | /* Notify server routines. The only real one is dead name notification. */ | |
1713 | error_t | |
1714 | do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port) | |
1715 | { | |
1716 | struct inf *inf = waiting_inf; | |
1717 | ||
1718 | inf_debug (waiting_inf, "port = %d", dead_port); | |
1719 | ||
1720 | if (inf->task && inf->task->port == dead_port) | |
1721 | { | |
1722 | proc_debug (inf->task, "is dead"); | |
1723 | inf->task->port = MACH_PORT_NULL; | |
1724 | if (proc_wait_pid == inf->pid) | |
1725 | /* We have a wait outstanding on the process, which will return more | |
1726 | detailed information, so delay until we get that. */ | |
1727 | inf->wait.suppress = 1; | |
1728 | else | |
1729 | /* We never waited for the process (maybe it wasn't a child), so just | |
1730 | pretend it got a SIGKILL. */ | |
1731 | inf_task_died_status (inf); | |
1732 | } | |
1733 | else | |
1734 | { | |
1735 | struct proc *thread = inf_port_to_thread (inf, dead_port); | |
1736 | if (thread) | |
1737 | { | |
1738 | proc_debug (thread, "is dead"); | |
1739 | thread->port = MACH_PORT_NULL; | |
1740 | } | |
f90b2b1d JK |
1741 | |
1742 | if (inf->task->dead) | |
1743 | /* Since the task is dead, its threads are dying with it. */ | |
1744 | inf->wait.suppress = 1; | |
c906108c SS |
1745 | } |
1746 | ||
1747 | mach_port_deallocate (mach_task_self (), dead_port); | |
c5aa993b | 1748 | inf->threads_up_to_date = 0; /* Just in case */ |
c906108c SS |
1749 | |
1750 | return 0; | |
1751 | } | |
942fd805 | 1752 | |
c906108c SS |
1753 | \f |
1754 | static error_t | |
1755 | ill_rpc (char *fun) | |
1756 | { | |
1757 | warning ("illegal rpc: %s", fun); | |
1758 | return 0; | |
1759 | } | |
1760 | ||
1761 | error_t | |
1762 | do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count) | |
1763 | { | |
1764 | return ill_rpc (__FUNCTION__); | |
1765 | } | |
1766 | ||
1767 | error_t | |
1768 | do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name) | |
1769 | { | |
1770 | return ill_rpc (__FUNCTION__); | |
1771 | } | |
1772 | ||
1773 | error_t | |
1774 | do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name) | |
1775 | { | |
1776 | return ill_rpc (__FUNCTION__); | |
1777 | } | |
1778 | ||
1779 | error_t | |
1780 | do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name) | |
1781 | { | |
1782 | return ill_rpc (__FUNCTION__); | |
1783 | } | |
1784 | ||
1785 | error_t | |
1786 | do_mach_notify_send_once (mach_port_t notify) | |
1787 | { | |
1788 | return ill_rpc (__FUNCTION__); | |
1789 | } | |
942fd805 | 1790 | |
c906108c SS |
1791 | \f |
1792 | /* Process_reply server routines. We only use process_wait_reply. */ | |
1793 | ||
1794 | error_t | |
1795 | S_proc_wait_reply (mach_port_t reply, error_t err, | |
1796 | int status, int sigcode, rusage_t rusage, pid_t pid) | |
1797 | { | |
1798 | struct inf *inf = waiting_inf; | |
1799 | ||
1800 | inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d", | |
1801 | err ? strerror (err) : "0", pid, status, sigcode); | |
1802 | ||
1803 | if (err && proc_wait_pid && (!inf->task || !inf->task->port)) | |
1804 | /* Ack. The task has died, but the task-died notification code didn't | |
1805 | tell anyone because it thought a more detailed reply from the | |
1806 | procserver was forthcoming. However, we now learn that won't | |
1807 | happen... So we have to act like the task just died, and this time, | |
1808 | tell the world. */ | |
1809 | inf_task_died_status (inf); | |
1810 | ||
1811 | if (--proc_waits_pending == 0) | |
1812 | /* PROC_WAIT_PID represents the most recent wait. We will always get | |
1813 | replies in order because the proc server is single threaded. */ | |
1814 | proc_wait_pid = 0; | |
1815 | ||
1816 | inf_debug (inf, "waits pending now: %d", proc_waits_pending); | |
1817 | ||
1818 | if (err) | |
1819 | { | |
1820 | if (err != EINTR) | |
1821 | { | |
1822 | warning ("Can't wait for pid %d: %s", inf->pid, strerror (err)); | |
1823 | inf->no_wait = 1; | |
1824 | ||
1825 | /* Since we can't see the inferior's signals, don't trap them. */ | |
1826 | inf_set_traced (inf, 0); | |
1827 | } | |
1828 | } | |
1829 | else if (pid == inf->pid) | |
1830 | { | |
1831 | store_waitstatus (&inf->wait.status, status); | |
1832 | if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED) | |
1833 | /* The process has sent us a signal, and stopped itself in a sane | |
1834 | state pending our actions. */ | |
1835 | { | |
1836 | inf_debug (inf, "process has stopped itself"); | |
1837 | inf->stopped = 1; | |
1838 | } | |
1839 | } | |
1840 | else | |
1841 | inf->wait.suppress = 1; /* Something odd happened. Ignore. */ | |
1842 | ||
1843 | return 0; | |
1844 | } | |
1845 | ||
1846 | error_t | |
1847 | S_proc_setmsgport_reply (mach_port_t reply, error_t err, | |
1848 | mach_port_t old_msg_port) | |
1849 | { | |
1850 | return ill_rpc (__FUNCTION__); | |
1851 | } | |
1852 | ||
1853 | error_t | |
1854 | S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port) | |
1855 | { | |
1856 | return ill_rpc (__FUNCTION__); | |
1857 | } | |
942fd805 | 1858 | |
c906108c SS |
1859 | \f |
1860 | /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */ | |
1861 | ||
1862 | error_t | |
1863 | S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err) | |
1864 | { | |
1865 | struct inf *inf = waiting_inf; | |
1866 | ||
1867 | if (err == EBUSY) | |
1868 | /* EBUSY is what we get when the crash server has grabbed control of the | |
1869 | process and doesn't like what signal we tried to send it. Just act | |
1870 | like the process stopped (using a signal of 0 should mean that the | |
1871 | *next* time the user continues, it will pass signal 0, which the crash | |
1872 | server should like). */ | |
1873 | { | |
1874 | inf->wait.status.kind = TARGET_WAITKIND_STOPPED; | |
1875 | inf->wait.status.value.sig = TARGET_SIGNAL_0; | |
1876 | } | |
1877 | else if (err) | |
1878 | warning ("Signal delivery failed: %s", strerror (err)); | |
1879 | ||
1880 | if (err) | |
1881 | /* We only get this reply when we've posted a signal to a process which we | |
1882 | thought was stopped, and which we expected to continue after the signal. | |
1883 | Given that the signal has failed for some reason, it's reasonable to | |
1884 | assume it's still stopped. */ | |
1885 | inf->stopped = 1; | |
1886 | else | |
1887 | inf->wait.suppress = 1; | |
1888 | ||
1889 | return 0; | |
1890 | } | |
1891 | ||
1892 | error_t | |
1893 | S_msg_sig_post_reply (mach_port_t reply, error_t err) | |
1894 | { | |
1895 | return ill_rpc (__FUNCTION__); | |
1896 | } | |
942fd805 | 1897 | |
c906108c SS |
1898 | \f |
1899 | /* Returns the number of messages queued for the receive right PORT. */ | |
1900 | static mach_port_msgcount_t | |
1901 | port_msgs_queued (mach_port_t port) | |
1902 | { | |
1903 | struct mach_port_status status; | |
1904 | error_t err = | |
c5aa993b | 1905 | mach_port_get_receive_status (mach_task_self (), port, &status); |
c906108c SS |
1906 | |
1907 | if (err) | |
1908 | return 0; | |
1909 | else | |
1910 | return status.mps_msgcount; | |
1911 | } | |
942fd805 | 1912 | |
c906108c SS |
1913 | \f |
1914 | /* Resume execution of the inferior process. | |
1915 | ||
1916 | If STEP is nonzero, single-step it. | |
1917 | If SIGNAL is nonzero, give it that signal. | |
1918 | ||
1919 | TID STEP: | |
1920 | -1 true Single step the current thread allowing other threads to run. | |
1921 | -1 false Continue the current thread allowing other threads to run. | |
1922 | X true Single step the given thread, don't allow any others to run. | |
1923 | X false Continue the given thread, do not allow any others to run. | |
1924 | (Where X, of course, is anything except -1) | |
1925 | ||
1926 | Note that a resume may not `take' if there are pending exceptions/&c | |
1927 | still unprocessed from the last resume we did (any given resume may result | |
1928 | in multiple events returned by wait). | |
c5aa993b | 1929 | */ |
c906108c SS |
1930 | static void |
1931 | gnu_resume (int tid, int step, enum target_signal sig) | |
1932 | { | |
1933 | struct proc *step_thread = 0; | |
1934 | struct inf *inf = current_inferior; | |
1935 | ||
1936 | inf_debug (inf, "tid = %d, step = %d, sig = %d", tid, step, sig); | |
1937 | ||
cce74817 | 1938 | inf_validate_procinfo (inf); |
c5aa993b | 1939 | |
c906108c | 1940 | if (sig != TARGET_SIGNAL_0 || inf->stopped) |
cce74817 JM |
1941 | { |
1942 | if (sig == TARGET_SIGNAL_0 && inf->nomsg) | |
1943 | inf_continue (inf); | |
1944 | else | |
1945 | inf_signal (inf, sig); | |
1946 | } | |
c906108c SS |
1947 | else if (inf->wait.exc.reply != MACH_PORT_NULL) |
1948 | /* We received an exception to which we have chosen not to forward, so | |
1949 | abort the faulting thread, which will perhaps retake it. */ | |
1950 | { | |
1951 | proc_abort (inf->wait.thread, 1); | |
1952 | warning ("Aborting %s with unforwarded exception %s.", | |
1953 | proc_string (inf->wait.thread), | |
1954 | target_signal_to_name (inf->wait.status.value.sig)); | |
1955 | } | |
1956 | ||
1957 | if (port_msgs_queued (inf->event_port)) | |
1958 | /* If there are still messages in our event queue, don't bother resuming | |
1959 | the process, as we're just going to stop it right away anyway. */ | |
1960 | return; | |
1961 | ||
1962 | inf_update_procs (inf); | |
1963 | ||
1964 | if (tid < 0) | |
1965 | /* Allow all threads to run, except perhaps single-stepping one. */ | |
1966 | { | |
1967 | inf_debug (inf, "running all threads; tid = %d", inferior_pid); | |
1968 | tid = inferior_pid; /* What to step. */ | |
1969 | inf_set_threads_resume_sc (inf, 0, 1); | |
1970 | } | |
1971 | else | |
1972 | /* Just allow a single thread to run. */ | |
1973 | { | |
1974 | struct proc *thread = inf_tid_to_thread (inf, tid); | |
c5aa993b | 1975 | if (!thread) |
c906108c SS |
1976 | error ("Can't run single thread id %d: no such thread!"); |
1977 | inf_debug (inf, "running one thread: %d/%d", inf->pid, thread->tid); | |
1978 | inf_set_threads_resume_sc (inf, thread, 0); | |
1979 | } | |
1980 | ||
1981 | if (step) | |
1982 | { | |
1983 | step_thread = inf_tid_to_thread (inf, tid); | |
c5aa993b | 1984 | if (!step_thread) |
c906108c SS |
1985 | warning ("Can't step thread id %d: no such thread.", tid); |
1986 | else | |
1987 | inf_debug (inf, "stepping thread: %d/%d", inf->pid, step_thread->tid); | |
1988 | } | |
1989 | if (step_thread != inf->step_thread) | |
1990 | inf_set_step_thread (inf, step_thread); | |
1991 | ||
1992 | inf_debug (inf, "here we go..."); | |
1993 | inf_resume (inf); | |
1994 | } | |
942fd805 | 1995 | |
c906108c SS |
1996 | \f |
1997 | static void | |
fba45db2 | 1998 | gnu_kill_inferior (void) |
c906108c SS |
1999 | { |
2000 | struct proc *task = current_inferior->task; | |
2001 | if (task) | |
2002 | { | |
2003 | proc_debug (task, "terminating..."); | |
2004 | task_terminate (task->port); | |
2005 | inf_set_pid (current_inferior, -1); | |
2006 | } | |
2007 | target_mourn_inferior (); | |
2008 | } | |
2009 | ||
2010 | /* Clean up after the inferior dies. */ | |
c906108c | 2011 | static void |
fba45db2 | 2012 | gnu_mourn_inferior (void) |
c906108c SS |
2013 | { |
2014 | inf_debug (current_inferior, "rip"); | |
2015 | inf_detach (current_inferior); | |
2016 | unpush_target (&gnu_ops); | |
2017 | generic_mourn_inferior (); | |
2018 | } | |
942fd805 | 2019 | |
c906108c SS |
2020 | \f |
2021 | /* Fork an inferior process, and start debugging it. */ | |
2022 | ||
2023 | /* Set INFERIOR_PID to the first thread available in the child, if any. */ | |
2024 | static int | |
fba45db2 | 2025 | inf_pick_first_thread (void) |
c906108c SS |
2026 | { |
2027 | if (current_inferior->task && current_inferior->threads) | |
2028 | /* The first thread. */ | |
2029 | return current_inferior->threads->tid; | |
2030 | else | |
2031 | /* What may be the next thread. */ | |
2032 | return next_thread_id; | |
2033 | } | |
2034 | ||
2035 | static struct inf * | |
fba45db2 | 2036 | cur_inf (void) |
c906108c | 2037 | { |
c5aa993b | 2038 | if (!current_inferior) |
c906108c SS |
2039 | current_inferior = make_inf (); |
2040 | return current_inferior; | |
2041 | } | |
2042 | ||
2043 | static void | |
fba45db2 | 2044 | gnu_create_inferior (char *exec_file, char *allargs, char **env) |
c906108c SS |
2045 | { |
2046 | struct inf *inf = cur_inf (); | |
2047 | ||
2048 | void trace_me () | |
c5aa993b JM |
2049 | { |
2050 | /* We're in the child; make this process stop as soon as it execs. */ | |
2051 | inf_debug (inf, "tracing self"); | |
2052 | if (ptrace (PTRACE_TRACEME) != 0) | |
2053 | error ("ptrace (PTRACE_TRACEME) failed!"); | |
2054 | } | |
7a292a7a | 2055 | void attach_to_child (int pid) |
c5aa993b JM |
2056 | { |
2057 | /* Attach to the now stopped child, which is actually a shell... */ | |
2058 | inf_debug (inf, "attaching to child: %d", pid); | |
c906108c | 2059 | |
c5aa993b | 2060 | inf_attach (inf, pid); |
c906108c | 2061 | |
c5aa993b JM |
2062 | attach_flag = 0; |
2063 | push_target (&gnu_ops); | |
c906108c | 2064 | |
c5aa993b JM |
2065 | inf->pending_execs = 2; |
2066 | inf->nomsg = 1; | |
2067 | inf->traced = 1; | |
c906108c | 2068 | |
c5aa993b JM |
2069 | /* Now let the child run again, knowing that it will stop immediately |
2070 | because of the ptrace. */ | |
2071 | inf_resume (inf); | |
2072 | inferior_pid = inf_pick_first_thread (); | |
c906108c | 2073 | |
c5aa993b JM |
2074 | startup_inferior (inf->pending_execs); |
2075 | } | |
c906108c SS |
2076 | |
2077 | inf_debug (inf, "creating inferior"); | |
2078 | ||
7a292a7a SS |
2079 | fork_inferior (exec_file, allargs, env, trace_me, attach_to_child, |
2080 | NULL, NULL); | |
c906108c | 2081 | |
cce74817 | 2082 | inf_validate_procinfo (inf); |
c906108c SS |
2083 | inf_update_signal_thread (inf); |
2084 | inf_set_traced (inf, inf->want_signals); | |
2085 | ||
2086 | /* Execing the process will have trashed our exception ports; steal them | |
2087 | back (or make sure they're restored if the user wants that). */ | |
2088 | if (inf->want_exceptions) | |
2089 | inf_steal_exc_ports (inf); | |
2090 | else | |
2091 | inf_restore_exc_ports (inf); | |
2092 | ||
2093 | /* Here we go! */ | |
2acceee2 | 2094 | proceed ((CORE_ADDR) -1, 0, 0); |
c906108c SS |
2095 | } |
2096 | ||
2097 | /* Mark our target-struct as eligible for stray "run" and "attach" | |
2098 | commands. */ | |
2099 | static int | |
fba45db2 | 2100 | gnu_can_run (void) |
c906108c SS |
2101 | { |
2102 | return 1; | |
2103 | } | |
942fd805 | 2104 | |
c906108c SS |
2105 | \f |
2106 | #ifdef ATTACH_DETACH | |
2107 | ||
2108 | /* Attach to process PID, then initialize for debugging it | |
2109 | and wait for the trace-trap that results from attaching. */ | |
2110 | static void | |
fba45db2 | 2111 | gnu_attach (char *args, int from_tty) |
c906108c SS |
2112 | { |
2113 | int pid; | |
2114 | char *exec_file; | |
2115 | struct inf *inf = cur_inf (); | |
2116 | ||
2117 | if (!args) | |
f75c77fc | 2118 | error_no_arg ("process-id to attach"); |
c906108c SS |
2119 | |
2120 | pid = atoi (args); | |
2121 | ||
c5aa993b | 2122 | if (pid == getpid ()) /* Trying to masturbate? */ |
c906108c SS |
2123 | error ("I refuse to debug myself!"); |
2124 | ||
2125 | if (from_tty) | |
2126 | { | |
2127 | exec_file = (char *) get_exec_file (0); | |
2128 | ||
2129 | if (exec_file) | |
2130 | printf_unfiltered ("Attaching to program `%s', pid %d\n", | |
2131 | exec_file, pid); | |
2132 | else | |
2133 | printf_unfiltered ("Attaching to pid %d\n", pid); | |
2134 | ||
2135 | gdb_flush (gdb_stdout); | |
2136 | } | |
2137 | ||
2138 | inf_debug (inf, "attaching to pid: %d", pid); | |
2139 | ||
2140 | inf_attach (inf, pid); | |
2141 | inf_update_procs (inf); | |
2142 | ||
2143 | inferior_pid = inf_pick_first_thread (); | |
2144 | ||
2145 | attach_flag = 1; | |
2146 | push_target (&gnu_ops); | |
2147 | ||
b83266a0 SS |
2148 | /* We have to initialize the terminal settings now, since the code |
2149 | below might try to restore them. */ | |
2150 | target_terminal_init (); | |
c5aa993b | 2151 | |
c906108c SS |
2152 | /* If the process was stopped before we attached, make it continue the next |
2153 | time the user does a continue. */ | |
cce74817 JM |
2154 | inf_validate_procinfo (inf); |
2155 | ||
2156 | inf_update_signal_thread (inf); | |
2157 | inf_set_traced (inf, inf->want_signals); | |
c906108c | 2158 | |
c5aa993b | 2159 | #if 0 /* Do we need this? */ |
c906108c SS |
2160 | renumber_threads (0); /* Give our threads reasonable names. */ |
2161 | #endif | |
2162 | } | |
942fd805 | 2163 | |
c906108c SS |
2164 | \f |
2165 | /* Take a program previously attached to and detaches it. | |
2166 | The program resumes execution and will no longer stop | |
2167 | on signals, etc. We'd better not have left any breakpoints | |
2168 | in the program or it'll die when it hits one. For this | |
2169 | to work, it may be necessary for the process to have been | |
2170 | previously attached. It *might* work if the program was | |
2171 | started via fork. */ | |
2172 | static void | |
fba45db2 | 2173 | gnu_detach (char *args, int from_tty) |
c906108c SS |
2174 | { |
2175 | if (from_tty) | |
2176 | { | |
2177 | char *exec_file = get_exec_file (0); | |
2178 | if (exec_file) | |
2179 | printf_unfiltered ("Detaching from program `%s' pid %d\n", | |
2180 | exec_file, current_inferior->pid); | |
2181 | else | |
2182 | printf_unfiltered ("Detaching from pid %d\n", current_inferior->pid); | |
2183 | gdb_flush (gdb_stdout); | |
2184 | } | |
c5aa993b | 2185 | |
c906108c SS |
2186 | inf_detach (current_inferior); |
2187 | ||
2188 | inferior_pid = 0; | |
2189 | ||
c5aa993b | 2190 | unpush_target (&gnu_ops); /* Pop out of handling an inferior */ |
c906108c SS |
2191 | } |
2192 | #endif /* ATTACH_DETACH */ | |
2193 | ||
942fd805 | 2194 | \f |
c906108c | 2195 | static void |
fba45db2 | 2196 | gnu_terminal_init_inferior (void) |
c906108c | 2197 | { |
bf62e5b4 | 2198 | gdb_assert (current_inferior); |
c906108c SS |
2199 | terminal_init_inferior_with_pgrp (current_inferior->pid); |
2200 | } | |
2201 | ||
2202 | /* Get ready to modify the registers array. On machines which store | |
2203 | individual registers, this doesn't need to do anything. On machines | |
2204 | which store all the registers in one fell swoop, this makes sure | |
2205 | that registers contains all the registers from the program being | |
2206 | debugged. */ | |
c906108c | 2207 | static void |
fba45db2 | 2208 | gnu_prepare_to_store (void) |
c906108c SS |
2209 | { |
2210 | #ifdef CHILD_PREPARE_TO_STORE | |
2211 | CHILD_PREPARE_TO_STORE (); | |
2212 | #endif | |
2213 | } | |
2214 | ||
2215 | static void | |
fba45db2 | 2216 | gnu_open (char *arg, int from_tty) |
c906108c SS |
2217 | { |
2218 | error ("Use the \"run\" command to start a Unix child process."); | |
2219 | } | |
2220 | ||
2221 | static void | |
fba45db2 | 2222 | gnu_stop (void) |
c906108c SS |
2223 | { |
2224 | error ("to_stop target function not implemented"); | |
2225 | } | |
2226 | ||
7a292a7a | 2227 | static char * |
fba45db2 | 2228 | gnu_pid_to_exec_file (void) |
c906108c SS |
2229 | { |
2230 | error ("to_pid_to_exec_file target function not implemented"); | |
7a292a7a | 2231 | return NULL; |
c906108c | 2232 | } |
c5aa993b | 2233 | |
c906108c SS |
2234 | |
2235 | static int | |
2236 | gnu_thread_alive (int tid) | |
2237 | { | |
2238 | inf_update_procs (current_inferior); | |
2239 | return !!inf_tid_to_thread (current_inferior, tid); | |
2240 | } | |
942fd805 | 2241 | |
c906108c | 2242 | \f |
942fd805 MK |
2243 | /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in |
2244 | gdb's address space. Return 0 on failure; number of bytes read | |
2245 | otherwise. */ | |
c906108c | 2246 | int |
fba45db2 | 2247 | gnu_read_inferior (task_t task, CORE_ADDR addr, char *myaddr, int length) |
c906108c SS |
2248 | { |
2249 | error_t err; | |
2250 | vm_address_t low_address = (vm_address_t) trunc_page (addr); | |
2251 | vm_size_t aligned_length = | |
c5aa993b JM |
2252 | (vm_size_t) round_page (addr + length) - low_address; |
2253 | pointer_t copied; | |
2254 | int copy_count; | |
c906108c SS |
2255 | |
2256 | /* Get memory from inferior with page aligned addresses */ | |
2257 | err = vm_read (task, low_address, aligned_length, &copied, ©_count); | |
2258 | if (err) | |
2259 | return 0; | |
2260 | ||
c5aa993b | 2261 | err = hurd_safe_copyin (myaddr, (void *) addr - low_address + copied, length); |
c906108c SS |
2262 | if (err) |
2263 | { | |
2264 | warning ("Read from inferior faulted: %s", strerror (err)); | |
2265 | length = 0; | |
2266 | } | |
2267 | ||
2268 | err = vm_deallocate (mach_task_self (), copied, copy_count); | |
2269 | if (err) | |
2270 | warning ("gnu_read_inferior vm_deallocate failed: %s", strerror (err)); | |
2271 | ||
2272 | return length; | |
2273 | } | |
2274 | ||
2275 | #define CHK_GOTO_OUT(str,ret) \ | |
2276 | do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0) | |
2277 | ||
c5aa993b JM |
2278 | struct vm_region_list |
2279 | { | |
c906108c | 2280 | struct vm_region_list *next; |
c5aa993b JM |
2281 | vm_prot_t protection; |
2282 | vm_address_t start; | |
2283 | vm_size_t length; | |
c906108c SS |
2284 | }; |
2285 | ||
c5aa993b | 2286 | struct obstack region_obstack; |
c906108c | 2287 | |
942fd805 MK |
2288 | /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior |
2289 | task's address space. */ | |
c906108c | 2290 | int |
fba45db2 | 2291 | gnu_write_inferior (task_t task, CORE_ADDR addr, char *myaddr, int length) |
c906108c SS |
2292 | { |
2293 | error_t err = 0; | |
c5aa993b JM |
2294 | vm_address_t low_address = (vm_address_t) trunc_page (addr); |
2295 | vm_size_t aligned_length = | |
2296 | (vm_size_t) round_page (addr + length) - low_address; | |
2297 | pointer_t copied; | |
2298 | int copy_count; | |
2299 | int deallocate = 0; | |
c906108c | 2300 | |
c5aa993b | 2301 | char *errstr = "Bug in gnu_write_inferior"; |
c906108c SS |
2302 | |
2303 | struct vm_region_list *region_element; | |
c5aa993b | 2304 | struct vm_region_list *region_head = (struct vm_region_list *) NULL; |
c906108c SS |
2305 | |
2306 | /* Get memory from inferior with page aligned addresses */ | |
2307 | err = vm_read (task, | |
2308 | low_address, | |
2309 | aligned_length, | |
2310 | &copied, | |
2311 | ©_count); | |
2312 | CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err); | |
2313 | ||
2314 | deallocate++; | |
2315 | ||
942fd805 MK |
2316 | err = hurd_safe_copyout ((void *) addr - low_address + copied, |
2317 | myaddr, length); | |
c906108c SS |
2318 | CHK_GOTO_OUT ("Write to inferior faulted", err); |
2319 | ||
2320 | obstack_init (®ion_obstack); | |
2321 | ||
2322 | /* Do writes atomically. | |
942fd805 | 2323 | First check for holes and unwritable memory. */ |
c906108c | 2324 | { |
c5aa993b JM |
2325 | vm_size_t remaining_length = aligned_length; |
2326 | vm_address_t region_address = low_address; | |
c906108c SS |
2327 | |
2328 | struct vm_region_list *scan; | |
2329 | ||
c5aa993b | 2330 | while (region_address < low_address + aligned_length) |
c906108c SS |
2331 | { |
2332 | vm_prot_t protection; | |
2333 | vm_prot_t max_protection; | |
2334 | vm_inherit_t inheritance; | |
2335 | boolean_t shared; | |
2336 | mach_port_t object_name; | |
2337 | vm_offset_t offset; | |
c5aa993b JM |
2338 | vm_size_t region_length = remaining_length; |
2339 | vm_address_t old_address = region_address; | |
2340 | ||
c906108c SS |
2341 | err = vm_region (task, |
2342 | ®ion_address, | |
2343 | ®ion_length, | |
2344 | &protection, | |
2345 | &max_protection, | |
2346 | &inheritance, | |
2347 | &shared, | |
2348 | &object_name, | |
2349 | &offset); | |
2350 | CHK_GOTO_OUT ("vm_region failed", err); | |
2351 | ||
2352 | /* Check for holes in memory */ | |
2353 | if (old_address != region_address) | |
2354 | { | |
2355 | warning ("No memory at 0x%x. Nothing written", | |
2356 | old_address); | |
2357 | err = KERN_SUCCESS; | |
2358 | length = 0; | |
2359 | goto out; | |
2360 | } | |
2361 | ||
2362 | if (!(max_protection & VM_PROT_WRITE)) | |
2363 | { | |
2364 | warning ("Memory at address 0x%x is unwritable. Nothing written", | |
2365 | old_address); | |
2366 | err = KERN_SUCCESS; | |
2367 | length = 0; | |
2368 | goto out; | |
2369 | } | |
2370 | ||
2371 | /* Chain the regions for later use */ | |
c5aa993b | 2372 | region_element = |
c906108c | 2373 | (struct vm_region_list *) |
c5aa993b JM |
2374 | obstack_alloc (®ion_obstack, sizeof (struct vm_region_list)); |
2375 | ||
c906108c | 2376 | region_element->protection = protection; |
c5aa993b JM |
2377 | region_element->start = region_address; |
2378 | region_element->length = region_length; | |
c906108c SS |
2379 | |
2380 | /* Chain the regions along with protections */ | |
2381 | region_element->next = region_head; | |
c5aa993b JM |
2382 | region_head = region_element; |
2383 | ||
c906108c SS |
2384 | region_address += region_length; |
2385 | remaining_length = remaining_length - region_length; | |
2386 | } | |
2387 | ||
2388 | /* If things fail after this, we give up. | |
942fd805 | 2389 | Somebody is messing up inferior_task's mappings. */ |
c5aa993b | 2390 | |
c906108c SS |
2391 | /* Enable writes to the chained vm regions */ |
2392 | for (scan = region_head; scan; scan = scan->next) | |
2393 | { | |
c906108c SS |
2394 | if (!(scan->protection & VM_PROT_WRITE)) |
2395 | { | |
2396 | err = vm_protect (task, | |
2397 | scan->start, | |
2398 | scan->length, | |
2399 | FALSE, | |
2400 | scan->protection | VM_PROT_WRITE); | |
2401 | CHK_GOTO_OUT ("vm_protect: enable write failed", err); | |
2402 | } | |
2403 | } | |
2404 | ||
2405 | err = vm_write (task, | |
2406 | low_address, | |
2407 | copied, | |
2408 | aligned_length); | |
2409 | CHK_GOTO_OUT ("vm_write failed", err); | |
c5aa993b | 2410 | |
c906108c SS |
2411 | /* Set up the original region protections, if they were changed */ |
2412 | for (scan = region_head; scan; scan = scan->next) | |
2413 | { | |
c906108c SS |
2414 | if (!(scan->protection & VM_PROT_WRITE)) |
2415 | { | |
2416 | err = vm_protect (task, | |
2417 | scan->start, | |
2418 | scan->length, | |
2419 | FALSE, | |
2420 | scan->protection); | |
2421 | CHK_GOTO_OUT ("vm_protect: enable write failed", err); | |
2422 | } | |
2423 | } | |
2424 | } | |
2425 | ||
c5aa993b | 2426 | out: |
c906108c SS |
2427 | if (deallocate) |
2428 | { | |
2429 | obstack_free (®ion_obstack, 0); | |
c5aa993b | 2430 | |
c906108c SS |
2431 | (void) vm_deallocate (mach_task_self (), |
2432 | copied, | |
2433 | copy_count); | |
2434 | } | |
2435 | ||
2436 | if (err != KERN_SUCCESS) | |
2437 | { | |
2438 | warning ("%s: %s", errstr, mach_error_string (err)); | |
2439 | return 0; | |
2440 | } | |
2441 | ||
2442 | return length; | |
2443 | } | |
942fd805 | 2444 | |
c906108c | 2445 | \f |
0359ddd6 KB |
2446 | /* Return 0 on failure, number of bytes handled otherwise. TARGET |
2447 | is ignored. */ | |
c906108c | 2448 | static int |
0359ddd6 | 2449 | gnu_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
043780a1 | 2450 | struct mem_attrib *attrib, |
0359ddd6 | 2451 | struct target_ops *target) |
c906108c | 2452 | { |
942fd805 MK |
2453 | task_t task = (current_inferior |
2454 | ? (current_inferior->task | |
2455 | ? current_inferior->task->port : 0) | |
2456 | : 0); | |
c906108c SS |
2457 | |
2458 | if (task == MACH_PORT_NULL) | |
2459 | return 0; | |
2460 | else | |
2461 | { | |
2462 | inf_debug (current_inferior, "%s %p[%d] %s %p", | |
2463 | write ? "writing" : "reading", memaddr, len, | |
2464 | write ? "<--" : "-->", myaddr); | |
2465 | if (write) | |
2466 | return gnu_write_inferior (task, memaddr, myaddr, len); | |
2467 | else | |
c5aa993b | 2468 | return gnu_read_inferior (task, memaddr, myaddr, len); |
c906108c SS |
2469 | } |
2470 | } | |
942fd805 | 2471 | |
c906108c | 2472 | \f |
ed9a39eb | 2473 | /* Return printable description of proc. */ |
f90b2b1d | 2474 | char * |
ed9a39eb JM |
2475 | proc_string (struct proc *proc) |
2476 | { | |
2477 | static char tid_str[80]; | |
2478 | if (proc_is_task (proc)) | |
2479 | sprintf (tid_str, "process %d", proc->inf->pid); | |
2480 | else | |
2481 | sprintf (tid_str, "thread %d.%d", | |
2482 | proc->inf->pid, pid_to_thread_id (proc->tid)); | |
2483 | return tid_str; | |
2484 | } | |
2485 | ||
2486 | static char * | |
2487 | gnu_pid_to_str (int tid) | |
2488 | { | |
2489 | struct inf *inf = current_inferior; | |
2490 | struct proc *thread = inf_tid_to_thread (inf, tid); | |
2491 | ||
2492 | if (thread) | |
2493 | return proc_string (thread); | |
2494 | else | |
2495 | { | |
2496 | static char tid_str[80]; | |
2497 | sprintf (tid_str, "bogus thread id %d", tid); | |
2498 | return tid_str; | |
2499 | } | |
2500 | } | |
942fd805 | 2501 | |
ed9a39eb | 2502 | \f |
c906108c SS |
2503 | extern void gnu_store_registers (int regno); |
2504 | extern void gnu_fetch_registers (int regno); | |
2505 | ||
c5aa993b | 2506 | struct target_ops gnu_ops; |
c906108c SS |
2507 | |
2508 | static void | |
c5aa993b JM |
2509 | init_gnu_ops (void) |
2510 | { | |
942fd805 MK |
2511 | gnu_ops.to_shortname = "GNU"; /* to_shortname */ |
2512 | gnu_ops.to_longname = "GNU Hurd process"; /* to_longname */ | |
c5aa993b | 2513 | gnu_ops.to_doc = "GNU Hurd process"; /* to_doc */ |
942fd805 MK |
2514 | gnu_ops.to_open = gnu_open; /* to_open */ |
2515 | gnu_ops.to_close = 0; /* to_close */ | |
c5aa993b | 2516 | gnu_ops.to_attach = gnu_attach; /* to_attach */ |
c906108c | 2517 | gnu_ops.to_post_attach = NULL; |
c5aa993b JM |
2518 | gnu_ops.to_require_attach = NULL; /* to_require_attach */ |
2519 | gnu_ops.to_detach = gnu_detach; /* to_detach */ | |
2520 | gnu_ops.to_require_detach = NULL; /* to_require_detach */ | |
2521 | gnu_ops.to_resume = gnu_resume; /* to_resume */ | |
942fd805 MK |
2522 | gnu_ops.to_wait = gnu_wait; /* to_wait */ |
2523 | gnu_ops.to_post_wait = NULL; /* to_post_wait */ | |
2524 | gnu_ops.to_fetch_registers = gnu_fetch_registers; /* to_fetch_registers */ | |
2525 | gnu_ops.to_store_registers = gnu_store_registers; /* to_store_registers */ | |
2526 | gnu_ops.to_prepare_to_store = gnu_prepare_to_store; /* to_prepare_to_store */ | |
2527 | gnu_ops.to_xfer_memory = gnu_xfer_memory; /* to_xfer_memory */ | |
2528 | gnu_ops.to_files_info = 0; /* to_files_info */ | |
c5aa993b JM |
2529 | gnu_ops.to_insert_breakpoint = memory_insert_breakpoint; |
2530 | gnu_ops.to_remove_breakpoint = memory_remove_breakpoint; | |
2531 | gnu_ops.to_terminal_init = gnu_terminal_init_inferior; | |
2532 | gnu_ops.to_terminal_inferior = terminal_inferior; | |
2533 | gnu_ops.to_terminal_ours_for_output = terminal_ours_for_output; | |
2534 | gnu_ops.to_terminal_ours = terminal_ours; | |
2535 | gnu_ops.to_terminal_info = child_terminal_info; | |
2536 | gnu_ops.to_kill = gnu_kill_inferior; /* to_kill */ | |
942fd805 MK |
2537 | gnu_ops.to_load = 0; /* to_load */ |
2538 | gnu_ops.to_lookup_symbol = 0; /* to_lookup_symbol */ | |
2539 | gnu_ops.to_create_inferior = gnu_create_inferior; /* to_create_inferior */ | |
2540 | gnu_ops.to_post_startup_inferior = NULL; /* to_post_startup_inferior */ | |
2541 | /* to_acknowledge_created_inferior */ | |
2542 | gnu_ops.to_acknowledge_created_inferior = NULL; | |
2543 | /* to_clone_and_follow_inferior */ | |
2544 | gnu_ops.to_clone_and_follow_inferior = NULL; | |
2545 | /* to_post_follow_inferior_by_clone */ | |
2546 | gnu_ops.to_post_follow_inferior_by_clone = NULL; | |
c906108c SS |
2547 | gnu_ops.to_insert_fork_catchpoint = NULL; |
2548 | gnu_ops.to_remove_fork_catchpoint = NULL; | |
2549 | gnu_ops.to_insert_vfork_catchpoint = NULL; | |
2550 | gnu_ops.to_remove_vfork_catchpoint = NULL; | |
942fd805 | 2551 | gnu_ops.to_has_forked = NULL; /* to_has_forked */ |
c5aa993b | 2552 | gnu_ops.to_has_vforked = NULL; /* to_has_vforked */ |
c906108c | 2553 | gnu_ops.to_can_follow_vfork_prior_to_exec = NULL; |
c5aa993b | 2554 | gnu_ops.to_post_follow_vfork = NULL; /* to_post_follow_vfork */ |
c906108c SS |
2555 | gnu_ops.to_insert_exec_catchpoint = NULL; |
2556 | gnu_ops.to_remove_exec_catchpoint = NULL; | |
2557 | gnu_ops.to_has_execd = NULL; | |
2558 | gnu_ops.to_reported_exec_events_per_exec_call = NULL; | |
2559 | gnu_ops.to_has_exited = NULL; | |
c5aa993b JM |
2560 | gnu_ops.to_mourn_inferior = gnu_mourn_inferior; /* to_mourn_inferior */ |
2561 | gnu_ops.to_can_run = gnu_can_run; /* to_can_run */ | |
2562 | gnu_ops.to_notice_signals = 0; /* to_notice_signals */ | |
2563 | gnu_ops.to_thread_alive = gnu_thread_alive; /* to_thread_alive */ | |
ed9a39eb | 2564 | gnu_ops.to_pid_to_str = gnu_pid_to_str; /* to_pid_to_str */ |
c5aa993b | 2565 | gnu_ops.to_stop = gnu_stop; /* to_stop */ |
942fd805 | 2566 | gnu_ops.to_pid_to_exec_file = gnu_pid_to_exec_file; /* to_pid_to_exec_file */ |
c906108c | 2567 | gnu_ops.to_core_file_to_sym_file = NULL; |
c5aa993b | 2568 | gnu_ops.to_stratum = process_stratum; /* to_stratum */ |
942fd805 | 2569 | gnu_ops.DONT_USE = 0; /* to_next */ |
c5aa993b | 2570 | gnu_ops.to_has_all_memory = 1; /* to_has_all_memory */ |
942fd805 MK |
2571 | gnu_ops.to_has_memory = 1; /* to_has_memory */ |
2572 | gnu_ops.to_has_stack = 1; /* to_has_stack */ | |
2573 | gnu_ops.to_has_registers = 1; /* to_has_registers */ | |
2574 | gnu_ops.to_has_execution = 1; /* to_has_execution */ | |
2575 | gnu_ops.to_sections = 0; /* sections */ | |
2576 | gnu_ops.to_sections_end = 0; /* sections_end */ | |
2577 | gnu_ops.to_magic = OPS_MAGIC; /* to_magic */ | |
c5aa993b | 2578 | } /* init_gnu_ops */ |
942fd805 | 2579 | |
c906108c | 2580 | \f |
c906108c SS |
2581 | /* User task commands. */ |
2582 | ||
2583 | struct cmd_list_element *set_task_cmd_list = 0; | |
2584 | struct cmd_list_element *show_task_cmd_list = 0; | |
2585 | /* User thread commands. */ | |
2586 | ||
2587 | /* Commands with a prefix of `set/show thread'. */ | |
2588 | extern struct cmd_list_element *thread_cmd_list; | |
2589 | struct cmd_list_element *set_thread_cmd_list = NULL; | |
2590 | struct cmd_list_element *show_thread_cmd_list = NULL; | |
2591 | ||
2592 | /* Commands with a prefix of `set/show thread default'. */ | |
2593 | struct cmd_list_element *set_thread_default_cmd_list = NULL; | |
2594 | struct cmd_list_element *show_thread_default_cmd_list = NULL; | |
2595 | ||
2596 | static void | |
2597 | set_thread_cmd (char *args, int from_tty) | |
2598 | { | |
9846de1b | 2599 | printf_unfiltered ("\"set thread\" must be followed by the name of a thread property, or \"default\".\n"); |
c906108c SS |
2600 | } |
2601 | ||
2602 | static void | |
2603 | show_thread_cmd (char *args, int from_tty) | |
2604 | { | |
2605 | printf_unfiltered ("\"show thread\" must be followed by the name of a thread property, or \"default\".\n"); | |
2606 | } | |
2607 | ||
2608 | static void | |
2609 | set_thread_default_cmd (char *args, int from_tty) | |
2610 | { | |
2611 | printf_unfiltered ("\"set thread default\" must be followed by the name of a thread property.\n"); | |
2612 | } | |
2613 | ||
2614 | static void | |
2615 | show_thread_default_cmd (char *args, int from_tty) | |
2616 | { | |
2617 | printf_unfiltered ("\"show thread default\" must be followed by the name of a thread property.\n"); | |
2618 | } | |
2619 | ||
2620 | static int | |
2621 | parse_int_arg (char *args, char *cmd_prefix) | |
2622 | { | |
2623 | if (args) | |
2624 | { | |
2625 | char *arg_end; | |
2626 | int val = strtoul (args, &arg_end, 10); | |
2627 | if (*args && *arg_end == '\0') | |
2628 | return val; | |
2629 | } | |
2630 | error ("Illegal argument for \"%s\" command, should be an integer.", cmd_prefix); | |
2631 | } | |
2632 | ||
2633 | static int | |
2634 | _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix) | |
2635 | { | |
2636 | if (!args || strcmp (args, t_val) == 0) | |
2637 | return 1; | |
2638 | else if (strcmp (args, f_val) == 0) | |
2639 | return 0; | |
2640 | else | |
2641 | error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".", | |
2642 | cmd_prefix, t_val, f_val); | |
2643 | } | |
2644 | ||
2645 | #define parse_bool_arg(args, cmd_prefix) \ | |
2646 | _parse_bool_arg (args, "on", "off", cmd_prefix) | |
2647 | ||
2648 | static void | |
2649 | check_empty (char *args, char *cmd_prefix) | |
2650 | { | |
2651 | if (args) | |
2652 | error ("Garbage after \"%s\" command: `%s'", cmd_prefix, args); | |
2653 | } | |
2654 | ||
2655 | /* Returns the alive thread named by INFERIOR_PID, or signals an error. */ | |
2656 | static struct proc * | |
fba45db2 | 2657 | cur_thread (void) |
c906108c SS |
2658 | { |
2659 | struct inf *inf = cur_inf (); | |
2660 | struct proc *thread = inf_tid_to_thread (inf, inferior_pid); | |
2661 | if (!thread) | |
2662 | error ("No current thread."); | |
2663 | return thread; | |
2664 | } | |
2665 | ||
2666 | /* Returns the current inferior, but signals an error if it has no task. */ | |
2667 | static struct inf * | |
fba45db2 | 2668 | active_inf (void) |
c906108c SS |
2669 | { |
2670 | struct inf *inf = cur_inf (); | |
c5aa993b | 2671 | if (!inf->task) |
c906108c SS |
2672 | error ("No current process."); |
2673 | return inf; | |
2674 | } | |
942fd805 | 2675 | |
c906108c SS |
2676 | \f |
2677 | static void | |
2678 | set_task_pause_cmd (char *args, int from_tty) | |
2679 | { | |
2680 | struct inf *inf = cur_inf (); | |
2681 | int old_sc = inf->pause_sc; | |
2682 | ||
2683 | inf->pause_sc = parse_bool_arg (args, "set task pause"); | |
2684 | ||
2685 | if (old_sc == 0 && inf->pause_sc != 0) | |
2686 | /* If the task is currently unsuspended, immediately suspend it, | |
2687 | otherwise wait until the next time it gets control. */ | |
2688 | inf_suspend (inf); | |
2689 | } | |
2690 | ||
2691 | static void | |
2692 | show_task_pause_cmd (char *args, int from_tty) | |
2693 | { | |
2694 | struct inf *inf = cur_inf (); | |
2695 | check_empty (args, "show task pause"); | |
2696 | printf_unfiltered ("The inferior task %s suspended while gdb has control.\n", | |
2697 | inf->task | |
2698 | ? (inf->pause_sc == 0 ? "isn't" : "is") | |
2699 | : (inf->pause_sc == 0 ? "won't be" : "will be")); | |
2700 | } | |
2701 | ||
2702 | static void | |
2703 | set_task_detach_sc_cmd (char *args, int from_tty) | |
2704 | { | |
2705 | cur_inf ()->detach_sc = parse_int_arg (args, "set task detach-suspend-count"); | |
2706 | } | |
2707 | ||
2708 | static void | |
2709 | show_task_detach_sc_cmd (char *args, int from_tty) | |
2710 | { | |
2711 | check_empty (args, "show task detach-suspend-count"); | |
2712 | printf_unfiltered ("The inferior task will be left with a suspend count of %d when detaching.\n", | |
2713 | cur_inf ()->detach_sc); | |
2714 | } | |
942fd805 | 2715 | |
c906108c SS |
2716 | \f |
2717 | static void | |
2718 | set_thread_default_pause_cmd (char *args, int from_tty) | |
2719 | { | |
2720 | struct inf *inf = cur_inf (); | |
2721 | inf->default_thread_pause_sc = | |
2722 | parse_bool_arg (args, "set thread default pause") ? 0 : 1; | |
2723 | } | |
2724 | ||
2725 | static void | |
2726 | show_thread_default_pause_cmd (char *args, int from_tty) | |
2727 | { | |
2728 | struct inf *inf = cur_inf (); | |
2729 | int sc = inf->default_thread_pause_sc; | |
2730 | check_empty (args, "show thread default pause"); | |
2731 | printf_unfiltered ("New threads %s suspended while gdb has control%s.\n", | |
2732 | sc ? "are" : "aren't", | |
2733 | !sc && inf->pause_sc ? " (but the task is)" : ""); | |
2734 | } | |
2735 | ||
2736 | static void | |
2737 | set_thread_default_run_cmd (char *args, int from_tty) | |
2738 | { | |
2739 | struct inf *inf = cur_inf (); | |
2740 | inf->default_thread_run_sc = | |
2741 | parse_bool_arg (args, "set thread default run") ? 0 : 1; | |
2742 | } | |
2743 | ||
2744 | static void | |
2745 | show_thread_default_run_cmd (char *args, int from_tty) | |
2746 | { | |
2747 | struct inf *inf = cur_inf (); | |
2748 | check_empty (args, "show thread default run"); | |
2749 | printf_unfiltered ("New threads %s allowed to run.\n", | |
2750 | inf->default_thread_run_sc == 0 ? "are" : "aren't"); | |
2751 | } | |
2752 | ||
2753 | static void | |
2754 | set_thread_default_detach_sc_cmd (char *args, int from_tty) | |
2755 | { | |
2756 | cur_inf ()->default_thread_detach_sc = | |
2757 | parse_int_arg (args, "set thread default detach-suspend-count"); | |
2758 | } | |
2759 | ||
2760 | static void | |
2761 | show_thread_default_detach_sc_cmd (char *args, int from_tty) | |
2762 | { | |
2763 | check_empty (args, "show thread default detach-suspend-count"); | |
2764 | printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n", | |
2765 | cur_inf ()->default_thread_detach_sc); | |
2766 | } | |
942fd805 | 2767 | |
c906108c SS |
2768 | \f |
2769 | /* Steal a send right called NAME in the inferior task, and make it PROC's | |
2770 | saved exception port. */ | |
2771 | static void | |
2772 | steal_exc_port (struct proc *proc, mach_port_t name) | |
2773 | { | |
2774 | error_t err; | |
2775 | mach_port_t port; | |
2776 | mach_msg_type_name_t port_type; | |
2777 | ||
2778 | if (!proc || !proc->inf->task) | |
2779 | error ("No inferior task."); | |
2780 | ||
2781 | err = mach_port_extract_right (proc->inf->task->port, | |
2782 | name, MACH_MSG_TYPE_COPY_SEND, | |
2783 | &port, &port_type); | |
2784 | if (err) | |
2785 | error ("Couldn't extract send right %d from inferior: %s", | |
2786 | name, strerror (err)); | |
2787 | ||
2788 | if (proc->saved_exc_port) | |
2789 | /* Get rid of our reference to the old one. */ | |
2790 | mach_port_deallocate (mach_task_self (), proc->saved_exc_port); | |
2791 | ||
2792 | proc->saved_exc_port = port; | |
2793 | ||
c5aa993b | 2794 | if (!proc->exc_port) |
c906108c SS |
2795 | /* If PROC is a thread, we may not have set its exception port before. |
2796 | We can't use proc_steal_exc_port because it also sets saved_exc_port. */ | |
2797 | { | |
c5aa993b JM |
2798 | proc->exc_port = proc->inf->event_port; |
2799 | err = proc_set_exception_port (proc, proc->exc_port); | |
2800 | error ("Can't set exception port for %s: %s", | |
2801 | proc_string (proc), strerror (err)); | |
c906108c SS |
2802 | } |
2803 | } | |
942fd805 | 2804 | |
c906108c SS |
2805 | static void |
2806 | set_task_exc_port_cmd (char *args, int from_tty) | |
2807 | { | |
2808 | struct inf *inf = cur_inf (); | |
2809 | if (!args) | |
2810 | error ("No argument to \"set task exception-port\" command."); | |
2811 | steal_exc_port (inf->task, parse_and_eval_address (args)); | |
2812 | } | |
2813 | ||
c5aa993b | 2814 | static void |
c906108c SS |
2815 | set_stopped_cmd (char *args, int from_tty) |
2816 | { | |
2817 | cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped"); | |
2818 | } | |
2819 | ||
2820 | static void | |
2821 | show_stopped_cmd (char *args, int from_tty) | |
2822 | { | |
2823 | struct inf *inf = active_inf (); | |
2824 | check_empty (args, "show stopped"); | |
2825 | printf_unfiltered ("The inferior process %s stopped.\n", | |
2826 | inf->stopped ? "is" : "isn't"); | |
2827 | } | |
2828 | ||
c5aa993b | 2829 | static void |
c906108c SS |
2830 | set_sig_thread_cmd (char *args, int from_tty) |
2831 | { | |
c906108c SS |
2832 | struct inf *inf = cur_inf (); |
2833 | ||
c5aa993b | 2834 | if (!args || (!isdigit (*args) && strcmp (args, "none") != 0)) |
c906108c SS |
2835 | error ("Illegal argument to \"set signal-thread\" command.\n" |
2836 | "Should be an integer thread ID, or `none'."); | |
2837 | ||
2838 | if (strcmp (args, "none") == 0) | |
2839 | inf->signal_thread = 0; | |
2840 | else | |
2841 | { | |
2842 | int tid = thread_id_to_pid (atoi (args)); | |
2843 | if (tid < 0) | |
2844 | error ("Thread ID %s not known. Use the \"info threads\" command to\n" | |
2845 | "see the IDs of currently known threads.", args); | |
2846 | inf->signal_thread = inf_tid_to_thread (inf, tid); | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | static void | |
2851 | show_sig_thread_cmd (char *args, int from_tty) | |
2852 | { | |
2853 | struct inf *inf = active_inf (); | |
2854 | check_empty (args, "show signal-thread"); | |
2855 | if (inf->signal_thread) | |
2856 | printf_unfiltered ("The signal thread is %s.\n", | |
2857 | proc_string (inf->signal_thread)); | |
2858 | else | |
2859 | printf_unfiltered ("There is no signal thread.\n"); | |
2860 | } | |
942fd805 | 2861 | |
c906108c | 2862 | \f |
c5aa993b | 2863 | static void |
c906108c SS |
2864 | set_signals_cmd (char *args, int from_tty) |
2865 | { | |
c906108c SS |
2866 | struct inf *inf = cur_inf (); |
2867 | ||
2868 | inf->want_signals = parse_bool_arg (args, "set signals"); | |
2869 | ||
2870 | if (inf->task && inf->want_signals != inf->traced) | |
2871 | /* Make this take effect immediately in a running process. */ | |
2872 | inf_set_traced (inf, inf->want_signals); | |
2873 | } | |
2874 | ||
2875 | static void | |
2876 | show_signals_cmd (char *args, int from_tty) | |
2877 | { | |
2878 | struct inf *inf = cur_inf (); | |
2879 | check_empty (args, "show signals"); | |
2880 | printf_unfiltered ("The inferior process's signals %s intercepted.\n", | |
2881 | inf->task | |
2882 | ? (inf->traced ? "are" : "aren't") | |
2883 | : (inf->want_signals ? "will be" : "won't be")); | |
2884 | } | |
2885 | ||
c5aa993b | 2886 | static void |
c906108c SS |
2887 | set_exceptions_cmd (char *args, int from_tty) |
2888 | { | |
2889 | struct inf *inf = cur_inf (); | |
2890 | int val = parse_bool_arg (args, "set exceptions"); | |
2891 | ||
2892 | if (inf->task && inf->want_exceptions != val) | |
2893 | /* Make this take effect immediately in a running process. */ | |
c5aa993b | 2894 | /* XXX */ ; |
c906108c SS |
2895 | |
2896 | inf->want_exceptions = val; | |
2897 | } | |
2898 | ||
2899 | static void | |
2900 | show_exceptions_cmd (char *args, int from_tty) | |
2901 | { | |
2902 | struct inf *inf = cur_inf (); | |
2903 | check_empty (args, "show exceptions"); | |
2904 | printf_unfiltered ("Exceptions in the inferior %s trapped.\n", | |
2905 | inf->task | |
2906 | ? (inf->want_exceptions ? "are" : "aren't") | |
2907 | : (inf->want_exceptions ? "will be" : "won't be")); | |
2908 | } | |
942fd805 | 2909 | |
c906108c SS |
2910 | \f |
2911 | static void | |
2912 | set_task_cmd (char *args, int from_tty) | |
2913 | { | |
942fd805 MK |
2914 | printf_unfiltered ("\"set task\" must be followed by the name" |
2915 | " of a task property.\n"); | |
c906108c SS |
2916 | } |
2917 | ||
2918 | static void | |
2919 | show_task_cmd (char *args, int from_tty) | |
2920 | { | |
2921 | struct inf *inf = cur_inf (); | |
2922 | ||
2923 | check_empty (args, "show task"); | |
2924 | ||
2925 | show_signals_cmd (0, from_tty); | |
2926 | show_exceptions_cmd (0, from_tty); | |
2927 | show_task_pause_cmd (0, from_tty); | |
2928 | ||
2929 | if (inf->pause_sc == 0) | |
2930 | show_thread_default_pause_cmd (0, from_tty); | |
2931 | show_thread_default_run_cmd (0, from_tty); | |
2932 | ||
2933 | if (inf->task) | |
2934 | { | |
2935 | show_stopped_cmd (0, from_tty); | |
2936 | show_sig_thread_cmd (0, from_tty); | |
2937 | } | |
2938 | ||
2939 | if (inf->detach_sc != 0) | |
2940 | show_task_detach_sc_cmd (0, from_tty); | |
2941 | if (inf->default_thread_detach_sc != 0) | |
2942 | show_thread_default_detach_sc_cmd (0, from_tty); | |
2943 | } | |
942fd805 | 2944 | |
c906108c SS |
2945 | \f |
2946 | static void | |
2947 | set_noninvasive_cmd (char *args, int from_tty) | |
2948 | { | |
2949 | /* Invert the sense of the arg for each component. */ | |
2950 | char *inv_args = parse_bool_arg (args, "set noninvasive") ? "off" : "on"; | |
2951 | ||
2952 | set_task_pause_cmd (inv_args, from_tty); | |
2953 | set_signals_cmd (inv_args, from_tty); | |
2954 | set_exceptions_cmd (inv_args, from_tty); | |
2955 | } | |
942fd805 | 2956 | |
c906108c SS |
2957 | \f |
2958 | static void | |
2959 | info_port_rights (char *args, mach_port_type_t only) | |
2960 | { | |
2961 | struct inf *inf = active_inf (); | |
2962 | value_ptr vmark = value_mark (); | |
2963 | ||
2964 | if (args) | |
2965 | /* Explicit list of port rights. */ | |
2966 | { | |
2967 | while (*args) | |
2968 | { | |
2969 | value_ptr val = parse_to_comma_and_eval (&args); | |
2970 | long right = value_as_long (val); | |
2971 | error_t err = | |
c5aa993b JM |
2972 | print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS, |
2973 | stdout); | |
c906108c SS |
2974 | if (err) |
2975 | error ("%ld: %s.", right, strerror (err)); | |
2976 | } | |
2977 | } | |
2978 | else | |
2979 | /* Print all of them. */ | |
2980 | { | |
2981 | error_t err = | |
c5aa993b JM |
2982 | print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS, |
2983 | stdout); | |
c906108c SS |
2984 | if (err) |
2985 | error ("%s.", strerror (err)); | |
2986 | } | |
2987 | ||
2988 | value_free_to_mark (vmark); | |
2989 | } | |
2990 | ||
2991 | static void | |
2992 | info_send_rights_cmd (char *args, int from_tty) | |
2993 | { | |
2994 | info_port_rights (args, MACH_PORT_TYPE_SEND); | |
2995 | } | |
942fd805 | 2996 | |
c906108c SS |
2997 | static void |
2998 | info_recv_rights_cmd (char *args, int from_tty) | |
2999 | { | |
3000 | info_port_rights (args, MACH_PORT_TYPE_RECEIVE); | |
3001 | } | |
942fd805 | 3002 | |
c906108c SS |
3003 | static void |
3004 | info_port_sets_cmd (char *args, int from_tty) | |
3005 | { | |
3006 | info_port_rights (args, MACH_PORT_TYPE_PORT_SET); | |
3007 | } | |
942fd805 | 3008 | |
c906108c SS |
3009 | static void |
3010 | info_dead_names_cmd (char *args, int from_tty) | |
3011 | { | |
3012 | info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME); | |
3013 | } | |
942fd805 | 3014 | |
c906108c SS |
3015 | static void |
3016 | info_port_rights_cmd (char *args, int from_tty) | |
3017 | { | |
3018 | info_port_rights (args, ~0); | |
3019 | } | |
942fd805 | 3020 | |
c906108c | 3021 | \f |
c5aa993b | 3022 | static void |
942fd805 | 3023 | add_task_commands (void) |
c906108c SS |
3024 | { |
3025 | add_cmd ("pause", class_run, set_thread_default_pause_cmd, | |
942fd805 MK |
3026 | "Set whether the new threads are suspended while gdb has control.\n\ |
3027 | This property normally has no effect because the whole task is\n\ | |
3028 | suspended, however, that may be disabled with \"set task pause off\".\n\ | |
3029 | The default value is \"off\".", | |
c906108c SS |
3030 | &set_thread_default_cmd_list); |
3031 | add_cmd ("pause", no_class, show_thread_default_pause_cmd, | |
3032 | "Show whether new threads are suspended while gdb has control.", | |
3033 | &show_thread_default_cmd_list); | |
942fd805 | 3034 | |
c906108c | 3035 | add_cmd ("run", class_run, set_thread_default_run_cmd, |
942fd805 MK |
3036 | "Set whether new threads are allowed to run \ |
3037 | (once gdb has noticed them).", | |
c906108c SS |
3038 | &set_thread_default_cmd_list); |
3039 | add_cmd ("run", no_class, show_thread_default_run_cmd, | |
942fd805 MK |
3040 | "Show whether new threads are allowed to run \ |
3041 | (once gdb has noticed them).", | |
c906108c | 3042 | &show_thread_default_cmd_list); |
942fd805 | 3043 | |
c906108c SS |
3044 | add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd, |
3045 | "Set the default detach-suspend-count value for new threads.", | |
3046 | &set_thread_default_cmd_list); | |
3047 | add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd, | |
3048 | "Show the default detach-suspend-count value for new threads.", | |
3049 | &show_thread_default_cmd_list); | |
3050 | ||
3051 | add_cmd ("signals", class_run, set_signals_cmd, | |
942fd805 MK |
3052 | "Set whether the inferior process's signals will be intercepted.\n\ |
3053 | Mach exceptions (such as breakpoint traps) are not affected.", | |
c906108c SS |
3054 | &setlist); |
3055 | add_alias_cmd ("sigs", "signals", class_run, 1, &setlist); | |
3056 | add_cmd ("signals", no_class, show_signals_cmd, | |
942fd805 | 3057 | "Show whether the inferior process's signals will be intercepted.", |
c906108c SS |
3058 | &showlist); |
3059 | add_alias_cmd ("sigs", "signals", no_class, 1, &showlist); | |
3060 | ||
3061 | add_cmd ("signal-thread", class_run, set_sig_thread_cmd, | |
942fd805 MK |
3062 | "Set the thread that gdb thinks is the libc signal thread.\n\ |
3063 | This thread is run when delivering a signal to a non-stopped process.", | |
c906108c SS |
3064 | &setlist); |
3065 | add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist); | |
3066 | add_cmd ("signal-thread", no_class, show_sig_thread_cmd, | |
3067 | "Set the thread that gdb thinks is the libc signal thread.", | |
3068 | &showlist); | |
3069 | add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist); | |
3070 | ||
3071 | add_cmd ("stopped", class_run, set_stopped_cmd, | |
942fd805 MK |
3072 | "Set whether gdb thinks the inferior process is stopped \ |
3073 | as with SIGSTOP.\n\ | |
3074 | Stopped process will be continued by sending them a signal.", | |
c906108c SS |
3075 | &setlist); |
3076 | add_cmd ("stopped", no_class, show_signals_cmd, | |
942fd805 MK |
3077 | "Show whether gdb thinks the inferior process is stopped \ |
3078 | as with SIGSTOP.", | |
c906108c SS |
3079 | &showlist); |
3080 | ||
3081 | add_cmd ("exceptions", class_run, set_exceptions_cmd, | |
942fd805 MK |
3082 | "Set whether exceptions in the inferior process will be trapped.\n\ |
3083 | When exceptions are turned off, neither breakpoints nor single-stepping\n\ | |
3084 | will work.", | |
c906108c SS |
3085 | &setlist); |
3086 | /* Allow `set exc' despite conflict with `set exception-port'. */ | |
3087 | add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist); | |
3088 | add_cmd ("exceptions", no_class, show_exceptions_cmd, | |
942fd805 | 3089 | "Show whether exceptions in the inferior process will be trapped.", |
c906108c SS |
3090 | &showlist); |
3091 | ||
3092 | add_prefix_cmd ("task", no_class, set_task_cmd, | |
3093 | "Command prefix for setting task attributes.", | |
3094 | &set_task_cmd_list, "set task ", 0, &setlist); | |
3095 | add_prefix_cmd ("task", no_class, show_task_cmd, | |
3096 | "Command prefix for showing task attributes.", | |
3097 | &show_task_cmd_list, "show task ", 0, &showlist); | |
3098 | ||
3099 | add_cmd ("pause", class_run, set_task_pause_cmd, | |
942fd805 MK |
3100 | "Set whether the task is suspended while gdb has control.\n\ |
3101 | A value of \"on\" takes effect immediately, otherwise nothing happens\n\ | |
3102 | until the next time the program is continued.\n\ | |
3103 | When setting this to \"off\", \"set thread default pause on\" can be\n\ | |
3104 | used to pause individual threads by default instead.", | |
c906108c SS |
3105 | &set_task_cmd_list); |
3106 | add_cmd ("pause", no_class, show_task_pause_cmd, | |
3107 | "Show whether the task is suspended while gdb has control.", | |
3108 | &show_task_cmd_list); | |
942fd805 | 3109 | |
c906108c SS |
3110 | add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd, |
3111 | "Set the suspend count will leave on the thread when detaching.", | |
3112 | &set_task_cmd_list); | |
3113 | add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd, | |
3114 | "Show the suspend count will leave on the thread when detaching.", | |
3115 | &show_task_cmd_list); | |
3116 | ||
3117 | add_cmd ("exception-port", no_class, set_task_exc_port_cmd, | |
942fd805 MK |
3118 | "Set the task exception port to which we forward exceptions.\n\ |
3119 | The argument should be the value of the send right in the task.", | |
c906108c SS |
3120 | &set_task_cmd_list); |
3121 | add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list); | |
942fd805 MK |
3122 | add_alias_cmd ("exc-port", "exception-port", no_class, 1, |
3123 | &set_task_cmd_list); | |
c906108c SS |
3124 | |
3125 | /* A convenient way of turning on all options require to noninvasively | |
3126 | debug running tasks. */ | |
3127 | add_cmd ("noninvasive", no_class, set_noninvasive_cmd, | |
942fd805 MK |
3128 | "Set task options so that we interfere as little as possible.\n\ |
3129 | This is the same as setting `task pause', `exceptions', and\n\ | |
3130 | `signals' to the opposite value.", | |
c906108c SS |
3131 | &setlist); |
3132 | ||
3133 | /* Commands to show information about the task's ports. */ | |
3134 | add_cmd ("send-rights", class_info, info_send_rights_cmd, | |
3135 | "Show information about the task's send rights", | |
3136 | &infolist); | |
3137 | add_cmd ("receive-rights", class_info, info_recv_rights_cmd, | |
3138 | "Show information about the task's receive rights", | |
3139 | &infolist); | |
942fd805 | 3140 | add_cmd ("port-rights", class_info, info_port_rights_cmd, |
c906108c SS |
3141 | "Show information about the task's port rights", |
3142 | &infolist); | |
3143 | add_cmd ("port-sets", class_info, info_port_sets_cmd, | |
3144 | "Show information about the task's port sets", | |
3145 | &infolist); | |
3146 | add_cmd ("dead-names", class_info, info_dead_names_cmd, | |
3147 | "Show information about the task's dead names", | |
3148 | &infolist); | |
3149 | add_info_alias ("ports", "port-rights", 1); | |
3150 | add_info_alias ("port", "port-rights", 1); | |
3151 | add_info_alias ("psets", "port-sets", 1); | |
3152 | } | |
c906108c | 3153 | |
942fd805 | 3154 | \f |
c906108c SS |
3155 | static void |
3156 | set_thread_pause_cmd (char *args, int from_tty) | |
3157 | { | |
3158 | struct proc *thread = cur_thread (); | |
3159 | int old_sc = thread->pause_sc; | |
3160 | thread->pause_sc = parse_bool_arg (args, "set thread pause"); | |
3161 | if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0) | |
3162 | /* If the task is currently unsuspended, immediately suspend it, | |
3163 | otherwise wait until the next time it gets control. */ | |
3164 | inf_suspend (thread->inf); | |
3165 | } | |
3166 | ||
3167 | static void | |
3168 | show_thread_pause_cmd (char *args, int from_tty) | |
3169 | { | |
3170 | struct proc *thread = cur_thread (); | |
3171 | int sc = thread->pause_sc; | |
3172 | check_empty (args, "show task pause"); | |
3173 | printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n", | |
3174 | proc_string (thread), | |
3175 | sc ? "is" : "isn't", | |
942fd805 | 3176 | !sc && thread->inf->pause_sc ? " (but the task is)" : ""); |
c906108c SS |
3177 | } |
3178 | ||
3179 | static void | |
3180 | set_thread_run_cmd (char *args, int from_tty) | |
3181 | { | |
3182 | struct proc *thread = cur_thread (); | |
3183 | thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1; | |
3184 | } | |
3185 | ||
3186 | static void | |
3187 | show_thread_run_cmd (char *args, int from_tty) | |
3188 | { | |
3189 | struct proc *thread = cur_thread (); | |
3190 | check_empty (args, "show thread run"); | |
3191 | printf_unfiltered ("Thread %s %s allowed to run.", | |
3192 | proc_string (thread), | |
3193 | thread->run_sc == 0 ? "is" : "isn't"); | |
3194 | } | |
3195 | ||
3196 | static void | |
3197 | set_thread_detach_sc_cmd (char *args, int from_tty) | |
3198 | { | |
942fd805 MK |
3199 | cur_thread ()->detach_sc = parse_int_arg (args, |
3200 | "set thread detach-suspend-count"); | |
c906108c SS |
3201 | } |
3202 | ||
3203 | static void | |
3204 | show_thread_detach_sc_cmd (char *args, int from_tty) | |
3205 | { | |
3206 | struct proc *thread = cur_thread (); | |
3207 | check_empty (args, "show thread detach-suspend-count"); | |
942fd805 MK |
3208 | printf_unfiltered ("Thread %s will be left with a suspend count" |
3209 | " of %d when detaching.\n", | |
c906108c SS |
3210 | proc_string (thread), |
3211 | thread->detach_sc); | |
3212 | } | |
3213 | ||
3214 | static void | |
3215 | set_thread_exc_port_cmd (char *args, int from_tty) | |
3216 | { | |
3217 | struct proc *thread = cur_thread (); | |
3218 | if (!args) | |
3219 | error ("No argument to \"set thread exception-port\" command."); | |
3220 | steal_exc_port (thread, parse_and_eval_address (args)); | |
3221 | } | |
3222 | ||
3223 | #if 0 | |
3224 | static void | |
3225 | show_thread_cmd (char *args, int from_tty) | |
3226 | { | |
3227 | struct proc *thread = cur_thread (); | |
3228 | check_empty (args, "show thread"); | |
3229 | show_thread_run_cmd (0, from_tty); | |
3230 | show_thread_pause_cmd (0, from_tty); | |
3231 | if (thread->detach_sc != 0) | |
3232 | show_thread_detach_sc_cmd (0, from_tty); | |
3233 | } | |
3234 | #endif | |
3235 | ||
3236 | static void | |
3237 | thread_takeover_sc_cmd (char *args, int from_tty) | |
3238 | { | |
3239 | struct proc *thread = cur_thread (); | |
3240 | thread_basic_info_data_t _info; | |
3241 | thread_basic_info_t info = &_info; | |
3242 | mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT; | |
3243 | error_t err = | |
c5aa993b | 3244 | thread_info (thread->port, THREAD_BASIC_INFO, (int *) &info, &info_len); |
c906108c SS |
3245 | if (err) |
3246 | error ("%s.", strerror (err)); | |
3247 | thread->sc = info->suspend_count; | |
3248 | if (from_tty) | |
3249 | printf_unfiltered ("Suspend count was %d.\n", thread->sc); | |
3250 | if (info != &_info) | |
942fd805 MK |
3251 | vm_deallocate (mach_task_self (), (vm_address_t) info, |
3252 | info_len * sizeof (int)); | |
c906108c SS |
3253 | } |
3254 | ||
942fd805 MK |
3255 | \f |
3256 | static void | |
3257 | add_thread_commands (void) | |
c906108c SS |
3258 | { |
3259 | add_prefix_cmd ("thread", no_class, set_thread_cmd, | |
3260 | "Command prefix for setting thread properties.", | |
3261 | &set_thread_cmd_list, "set thread ", 0, &setlist); | |
3262 | add_prefix_cmd ("default", no_class, show_thread_cmd, | |
3263 | "Command prefix for setting default thread properties.", | |
3264 | &set_thread_default_cmd_list, "set thread default ", 0, | |
3265 | &set_thread_cmd_list); | |
3266 | add_prefix_cmd ("thread", no_class, set_thread_default_cmd, | |
3267 | "Command prefix for showing thread properties.", | |
3268 | &show_thread_cmd_list, "show thread ", 0, &showlist); | |
3269 | add_prefix_cmd ("default", no_class, show_thread_default_cmd, | |
3270 | "Command prefix for showing default thread properties.", | |
3271 | &show_thread_default_cmd_list, "show thread default ", 0, | |
3272 | &show_thread_cmd_list); | |
3273 | ||
3274 | add_cmd ("pause", class_run, set_thread_pause_cmd, | |
942fd805 MK |
3275 | "Set whether the current thread is suspended \ |
3276 | while gdb has control.\n\ | |
3277 | A value of \"on\" takes effect immediately, otherwise nothing happens\n\ | |
3278 | until the next time the program is continued. This property normally\n\ | |
3279 | has no effect because the whole task is suspended, however, that may\n\ | |
3280 | be disabled with \"set task pause off\".\n\ | |
3281 | The default value is \"off\".", | |
c906108c SS |
3282 | &set_thread_cmd_list); |
3283 | add_cmd ("pause", no_class, show_thread_pause_cmd, | |
942fd805 MK |
3284 | "Show whether the current thread is suspended \ |
3285 | while gdb has control.", | |
c906108c SS |
3286 | &show_thread_cmd_list); |
3287 | ||
3288 | add_cmd ("run", class_run, set_thread_run_cmd, | |
3289 | "Set whether the current thread is allowed to run.", | |
3290 | &set_thread_cmd_list); | |
3291 | add_cmd ("run", no_class, show_thread_run_cmd, | |
3292 | "Show whether the current thread is allowed to run.", | |
3293 | &show_thread_cmd_list); | |
3294 | ||
3295 | add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd, | |
942fd805 MK |
3296 | "Set the suspend count will leave on the thread when detaching.\n\ |
3297 | Note that this is relative to suspend count when gdb noticed the thread;\n\ | |
3298 | use the `thread takeover-suspend-count' to force it to an absolute value.", | |
c906108c SS |
3299 | &set_thread_cmd_list); |
3300 | add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd, | |
942fd805 MK |
3301 | "Show the suspend count will leave on the thread when detaching.\n\ |
3302 | Note that this is relative to suspend count when gdb noticed the thread;\n\ | |
3303 | use the `thread takeover-suspend-count' to force it to an absolute value.", | |
c906108c SS |
3304 | &show_thread_cmd_list); |
3305 | ||
3306 | add_cmd ("exception-port", no_class, set_thread_exc_port_cmd, | |
942fd805 MK |
3307 | "Set the thread exception port to which we forward exceptions.\n\ |
3308 | This overrides the task exception port.\n\ | |
3309 | The argument should be the value of the send right in the task.", | |
c906108c SS |
3310 | &set_thread_cmd_list); |
3311 | add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list); | |
942fd805 MK |
3312 | add_alias_cmd ("exc-port", "exception-port", no_class, 1, |
3313 | &set_thread_cmd_list); | |
c906108c SS |
3314 | |
3315 | add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd, | |
942fd805 MK |
3316 | "Force the threads absolute suspend-count to be gdb's.\n\ |
3317 | Prior to giving this command, gdb's thread suspend-counts are relative\n\ | |
3318 | to the thread's initial suspend-count when gdb notices the threads.", | |
c906108c SS |
3319 | &thread_cmd_list); |
3320 | } | |
942fd805 | 3321 | |
c906108c SS |
3322 | \f |
3323 | void | |
942fd805 | 3324 | _initialize_gnu_nat (void) |
c906108c SS |
3325 | { |
3326 | proc_server = getproc (); | |
942fd805 | 3327 | |
c5aa993b | 3328 | init_gnu_ops (); |
c906108c | 3329 | add_target (&gnu_ops); |
942fd805 | 3330 | |
c906108c SS |
3331 | add_task_commands (); |
3332 | add_thread_commands (); | |
c906108c | 3333 | add_set_cmd ("gnu-debug", class_maintenance, |
c5aa993b | 3334 | var_boolean, (char *) &gnu_debug_flag, |
942fd805 | 3335 | "Set debugging output for the gnu backend.", &maintenancelist); |
c906108c SS |
3336 | } |
3337 | \f | |
3338 | #ifdef FLUSH_INFERIOR_CACHE | |
3339 | ||
3340 | /* When over-writing code on some machines the I-Cache must be flushed | |
3341 | explicitly, because it is not kept coherent by the lazy hardware. | |
3342 | This definitely includes breakpoints, for instance, or else we | |
3343 | end up looping in mysterious Bpt traps */ | |
3344 | ||
3345 | void | |
fba45db2 | 3346 | flush_inferior_icache (CORE_ADDR pc, int amount) |
c906108c SS |
3347 | { |
3348 | vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH; | |
c5aa993b JM |
3349 | error_t ret; |
3350 | ||
c906108c SS |
3351 | ret = vm_machine_attribute (current_inferior->task->port, |
3352 | pc, | |
3353 | amount, | |
3354 | MATTR_CACHE, | |
3355 | &flush); | |
3356 | if (ret != KERN_SUCCESS) | |
3357 | warning ("Error flushing inferior's cache : %s", strerror (ret)); | |
3358 | } | |
c5aa993b | 3359 | #endif /* FLUSH_INFERIOR_CACHE */ |