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