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