forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnu-nat.c
3505 lines (2926 loc) · 98.5 KB
/
gnu-nat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Interface GDB to the GNU Hurd.
Copyright (C) 1992-2015 Free Software Foundation, Inc.
This file is part of GDB.
Written by Miles Bader <[email protected]>
Some code and ideas from m3-nat.c by Jukka Virtanen <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include <ctype.h>
#include <limits.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <mach.h>
#include <mach_error.h>
#include <mach/exception.h>
#include <mach/message.h>
#include <mach/notify.h>
#include <mach/vm_attributes.h>
#include <hurd.h>
#include <hurd/interrupt.h>
#include <hurd/msg.h>
#include <hurd/msg_request.h>
#include <hurd/process.h>
/* Defined in <hurd/process.h>, but we need forward declarations from
<hurd/process_request.h> as well. */
#undef _process_user_
#include <hurd/process_request.h>
#include <hurd/signal.h>
#include <hurd/sigpreempt.h>
#include <portinfo.h>
#include "inferior.h"
#include "symtab.h"
#include "value.h"
#include "language.h"
#include "target.h"
#include "gdb_wait.h"
#include "gdbcmd.h"
#include "gdbcore.h"
#include "gdbthread.h"
#include "gdb_obstack.h"
#include "gnu-nat.h"
#include "inf-child.h"
#include "exc_request_S.h"
#include "notify_S.h"
#include "process_reply_S.h"
#include "msg_reply_S.h"
#include "exc_request_U.h"
#include "msg_U.h"
static process_t proc_server = MACH_PORT_NULL;
/* If we've sent a proc_wait_request to the proc server, the pid of the
process we asked about. We can only ever have one outstanding. */
int proc_wait_pid = 0;
/* The number of wait requests we've sent, and expect replies from. */
int proc_waits_pending = 0;
int gnu_debug_flag = 0;
/* Forward decls */
static struct inf *make_inf ();
void inf_clear_wait (struct inf *inf);
void inf_cleanup (struct inf *inf);
void inf_startup (struct inf *inf, int pid);
int inf_update_suspends (struct inf *inf);
void inf_set_pid (struct inf *inf, pid_t pid);
void inf_validate_procs (struct inf *inf);
void inf_steal_exc_ports (struct inf *inf);
void inf_restore_exc_ports (struct inf *inf);
void inf_set_threads_resume_sc (struct inf *inf,
struct proc *run_thread,
int run_others);
int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
void inf_suspend (struct inf *inf);
void inf_resume (struct inf *inf);
void inf_set_step_thread (struct inf *inf, struct proc *proc);
void inf_detach (struct inf *inf);
void inf_attach (struct inf *inf, int pid);
void inf_signal (struct inf *inf, enum gdb_signal sig);
void inf_continue (struct inf *inf);
#define inf_debug(_inf, msg, args...) \
do { struct inf *__inf = (_inf); \
debug ("{inf %d %s}: " msg, __inf->pid, \
host_address_to_string (__inf) , ##args); } while (0)
void proc_abort (struct proc *proc, int force);
struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
struct proc *_proc_free (struct proc *proc);
int proc_update_sc (struct proc *proc);
error_t proc_get_exception_port (struct proc *proc, mach_port_t * port);
error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
static mach_port_t _proc_get_exc_port (struct proc *proc);
void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
void proc_restore_exc_port (struct proc *proc);
int proc_trace (struct proc *proc, int set);
/* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
to INF's msg port and task port respectively. If it has no msg port,
EIEIO is returned. INF must refer to a running process! */
#define INF_MSGPORT_RPC(inf, rpc_expr) \
HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
(refport = inf->task->port, 0), 0, \
msgport ? (rpc_expr) : EIEIO)
/* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
there's someone around to deal with the RPC (and resuspend things
afterwards). This effects INF's threads' resume_sc count. */
#define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
(inf_set_threads_resume_sc_for_signal_thread (inf) \
? ({ error_t __e; \
inf_resume (inf); \
__e = INF_MSGPORT_RPC (inf, rpc_expr); \
inf_suspend (inf); \
__e; }) \
: EIEIO)
/* The state passed by an exception message. */
struct exc_state
{
int exception; /* The exception code. */
int code, subcode;
mach_port_t handler; /* The real exception port to handle this. */
mach_port_t reply; /* The reply port from the exception call. */
};
/* The results of the last wait an inf did. */
struct inf_wait
{
struct target_waitstatus status; /* The status returned to gdb. */
struct exc_state exc; /* The exception that caused us to return. */
struct proc *thread; /* The thread in question. */
int suppress; /* Something trivial happened. */
};
/* The state of an inferior. */
struct inf
{
/* Fields describing the current inferior. */
struct proc *task; /* The mach task. */
struct proc *threads; /* A linked list of all threads in TASK. */
/* True if THREADS needn't be validated by querying the task. We
assume that we and the task in question are the only ones
frobbing the thread list, so as long as we don't let any code
run, we don't have to worry about THREADS changing. */
int threads_up_to_date;
pid_t pid; /* The real system PID. */
struct inf_wait wait; /* What to return from target_wait. */
/* One thread proc in INF may be in `single-stepping mode'. This
is it. */
struct proc *step_thread;
/* The thread we think is the signal thread. */
struct proc *signal_thread;
mach_port_t event_port; /* Where we receive various msgs. */
/* True if we think at least one thread in the inferior could currently be
running. */
unsigned int running:1;
/* True if the process has stopped (in the proc server sense). Note that
since a proc server `stop' leaves the signal thread running, the inf can
be RUNNING && STOPPED... */
unsigned int stopped:1;
/* True if the inferior has no message port. */
unsigned int nomsg:1;
/* True if the inferior is traced. */
unsigned int traced:1;
/* True if we shouldn't try waiting for the inferior, usually because we
can't for some reason. */
unsigned int no_wait:1;
/* When starting a new inferior, we don't try to validate threads until all
the proper execs have been done, which this flag states we still
expect to happen. */
unsigned int pending_execs:1;
/* Fields describing global state. */
/* The task suspend count used when gdb has control. This is normally 1 to
make things easier for us, but sometimes (like when attaching to vital
system servers) it may be desirable to let the task continue to run
(pausing individual threads as necessary). */
int pause_sc;
/* The task suspend count left when detaching from a task. */
int detach_sc;
/* The initial values used for the run_sc and pause_sc of newly discovered
threads -- see the definition of those fields in struct proc. */
int default_thread_run_sc;
int default_thread_pause_sc;
int default_thread_detach_sc;
/* True if the process should be traced when started/attached. Newly
started processes *must* be traced at first to exec them properly, but
if this is false, tracing is turned off as soon it has done so. */
int want_signals;
/* True if exceptions from the inferior process should be trapped. This
must be on to use breakpoints. */
int want_exceptions;
};
int
__proc_pid (struct proc *proc)
{
return proc->inf->pid;
}
/* Update PROC's real suspend count to match it's desired one. Returns true
if we think PROC is now in a runnable state. */
int
proc_update_sc (struct proc *proc)
{
int running;
int err = 0;
int delta = proc->sc - proc->cur_sc;
if (delta)
proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
if (proc->sc == 0 && proc->state_changed)
/* Since PROC may start running, we must write back any state changes. */
{
gdb_assert (proc_is_thread (proc));
proc_debug (proc, "storing back changed thread state");
err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
(thread_state_t) &proc->state, THREAD_STATE_SIZE);
if (!err)
proc->state_changed = 0;
}
if (delta > 0)
{
while (delta-- > 0 && !err)
{
if (proc_is_task (proc))
err = task_suspend (proc->port);
else
err = thread_suspend (proc->port);
}
}
else
{
while (delta++ < 0 && !err)
{
if (proc_is_task (proc))
err = task_resume (proc->port);
else
err = thread_resume (proc->port);
}
}
if (!err)
proc->cur_sc = proc->sc;
/* If we got an error, then the task/thread has disappeared. */
running = !err && proc->sc == 0;
proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
if (err)
proc_debug (proc, "err = %s", safe_strerror (err));
if (running)
{
proc->aborted = 0;
proc->state_valid = proc->state_changed = 0;
proc->fetched_regs = 0;
}
return running;
}
/* Thread_abort is called on PROC if needed. PROC must be a thread proc.
If PROC is deemed `precious', then nothing is done unless FORCE is true.
In particular, a thread is precious if it's running (in which case forcing
it includes suspending it first), or if it has an exception pending. */
void
proc_abort (struct proc *proc, int force)
{
gdb_assert (proc_is_thread (proc));
if (!proc->aborted)
{
struct inf *inf = proc->inf;
int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
if (running && force)
{
proc->sc = 1;
inf_update_suspends (proc->inf);
running = 0;
warning (_("Stopped %s."), proc_string (proc));
}
else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
/* An exception is pending on PROC, which don't mess with. */
running = 1;
if (!running)
/* We only abort the thread if it's not actually running. */
{
thread_abort (proc->port);
proc_debug (proc, "aborted");
proc->aborted = 1;
}
else
proc_debug (proc, "not aborting");
}
}
/* Make sure that the state field in PROC is up to date, and return a pointer
to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
that the thread is stopped and aborted first, and sets the state_changed
field in PROC to true. */
thread_state_t
proc_get_state (struct proc *proc, int will_modify)
{
int was_aborted = proc->aborted;
proc_debug (proc, "updating state info%s",
will_modify ? " (with intention to modify)" : "");
proc_abort (proc, will_modify);
if (!was_aborted && proc->aborted)
/* PROC's state may have changed since we last fetched it. */
proc->state_valid = 0;
if (!proc->state_valid)
{
mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
error_t err =
thread_get_state (proc->port, THREAD_STATE_FLAVOR,
(thread_state_t) &proc->state, &state_size);
proc_debug (proc, "getting thread state");
proc->state_valid = !err;
}
if (proc->state_valid)
{
if (will_modify)
proc->state_changed = 1;
return (thread_state_t) &proc->state;
}
else
return 0;
}
/* Set PORT to PROC's exception port. */
error_t
proc_get_exception_port (struct proc * proc, mach_port_t * port)
{
if (proc_is_task (proc))
return task_get_exception_port (proc->port, port);
else
return thread_get_exception_port (proc->port, port);
}
/* Set PROC's exception port to PORT. */
error_t
proc_set_exception_port (struct proc * proc, mach_port_t port)
{
proc_debug (proc, "setting exception port: %lu", port);
if (proc_is_task (proc))
return task_set_exception_port (proc->port, port);
else
return thread_set_exception_port (proc->port, port);
}
/* Get PROC's exception port, cleaning up a bit if proc has died. */
static mach_port_t
_proc_get_exc_port (struct proc *proc)
{
mach_port_t exc_port;
error_t err = proc_get_exception_port (proc, &exc_port);
if (err)
/* PROC must be dead. */
{
if (proc->exc_port)
mach_port_deallocate (mach_task_self (), proc->exc_port);
proc->exc_port = MACH_PORT_NULL;
if (proc->saved_exc_port)
mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
proc->saved_exc_port = MACH_PORT_NULL;
}
return exc_port;
}
/* Replace PROC's exception port with EXC_PORT, unless it's already
been done. Stash away any existing exception port so we can
restore it later. */
void
proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
{
mach_port_t cur_exc_port = _proc_get_exc_port (proc);
if (cur_exc_port)
{
error_t err = 0;
proc_debug (proc, "inserting exception port: %lu", exc_port);
if (cur_exc_port != exc_port)
/* Put in our exception port. */
err = proc_set_exception_port (proc, exc_port);
if (err || cur_exc_port == proc->exc_port)
/* We previously set the exception port, and it's still set. So we
just keep the old saved port which is what the proc set. */
{
if (cur_exc_port)
mach_port_deallocate (mach_task_self (), cur_exc_port);
}
else
/* Keep a copy of PROC's old exception port so it can be restored. */
{
if (proc->saved_exc_port)
mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
proc->saved_exc_port = cur_exc_port;
}
proc_debug (proc, "saved exception port: %lu", proc->saved_exc_port);
if (!err)
proc->exc_port = exc_port;
else
warning (_("Error setting exception port for %s: %s"),
proc_string (proc), safe_strerror (err));
}
}
/* If we previously replaced PROC's exception port, put back what we
found there at the time, unless *our* exception port has since been
overwritten, in which case who knows what's going on. */
void
proc_restore_exc_port (struct proc *proc)
{
mach_port_t cur_exc_port = _proc_get_exc_port (proc);
if (cur_exc_port)
{
error_t err = 0;
proc_debug (proc, "restoring real exception port");
if (proc->exc_port == cur_exc_port)
/* Our's is still there. */
err = proc_set_exception_port (proc, proc->saved_exc_port);
if (proc->saved_exc_port)
mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
proc->saved_exc_port = MACH_PORT_NULL;
if (!err)
proc->exc_port = MACH_PORT_NULL;
else
warning (_("Error setting exception port for %s: %s"),
proc_string (proc), safe_strerror (err));
}
}
/* Turns hardware tracing in PROC on or off when SET is true or false,
respectively. Returns true on success. */
int
proc_trace (struct proc *proc, int set)
{
thread_state_t state = proc_get_state (proc, 1);
if (!state)
return 0; /* The thread must be dead. */
proc_debug (proc, "tracing %s", set ? "on" : "off");
if (set)
{
/* XXX We don't get the exception unless the thread has its own
exception port???? */
if (proc->exc_port == MACH_PORT_NULL)
proc_steal_exc_port (proc, proc->inf->event_port);
THREAD_STATE_SET_TRACED (state);
}
else
THREAD_STATE_CLEAR_TRACED (state);
return 1;
}
/* A variable from which to assign new TIDs. */
static int next_thread_id = 1;
/* Returns a new proc structure with the given fields. Also adds a
notification for PORT becoming dead to be sent to INF's notify port. */
struct proc *
make_proc (struct inf *inf, mach_port_t port, int tid)
{
error_t err;
mach_port_t prev_port = MACH_PORT_NULL;
struct proc *proc = XNEW (struct proc);
proc->port = port;
proc->tid = tid;
proc->inf = inf;
proc->next = 0;
proc->saved_exc_port = MACH_PORT_NULL;
proc->exc_port = MACH_PORT_NULL;
proc->sc = 0;
proc->cur_sc = 0;
/* Note that these are all the values for threads; the task simply uses the
corresponding field in INF directly. */
proc->run_sc = inf->default_thread_run_sc;
proc->pause_sc = inf->default_thread_pause_sc;
proc->detach_sc = inf->default_thread_detach_sc;
proc->resume_sc = proc->run_sc;
proc->aborted = 0;
proc->dead = 0;
proc->state_valid = 0;
proc->state_changed = 0;
proc_debug (proc, "is new");
/* Get notified when things die. */
err =
mach_port_request_notification (mach_task_self (), port,
MACH_NOTIFY_DEAD_NAME, 1,
inf->event_port,
MACH_MSG_TYPE_MAKE_SEND_ONCE,
&prev_port);
if (err)
warning (_("Couldn't request notification for port %lu: %s"),
port, safe_strerror (err));
else
{
proc_debug (proc, "notifications to: %lu", inf->event_port);
if (prev_port != MACH_PORT_NULL)
mach_port_deallocate (mach_task_self (), prev_port);
}
if (inf->want_exceptions)
{
if (proc_is_task (proc))
/* Make the task exception port point to us. */
proc_steal_exc_port (proc, inf->event_port);
else
/* Just clear thread exception ports -- they default to the
task one. */
proc_steal_exc_port (proc, MACH_PORT_NULL);
}
return proc;
}
/* Frees PROC and any resources it uses, and returns the value of PROC's
next field. */
struct proc *
_proc_free (struct proc *proc)
{
struct inf *inf = proc->inf;
struct proc *next = proc->next;
proc_debug (proc, "freeing...");
if (proc == inf->step_thread)
/* Turn off single stepping. */
inf_set_step_thread (inf, 0);
if (proc == inf->wait.thread)
inf_clear_wait (inf);
if (proc == inf->signal_thread)
inf->signal_thread = 0;
if (proc->port != MACH_PORT_NULL)
{
if (proc->exc_port != MACH_PORT_NULL)
/* Restore the original exception port. */
proc_restore_exc_port (proc);
if (proc->cur_sc != 0)
/* Resume the thread/task. */
{
proc->sc = 0;
proc_update_sc (proc);
}
mach_port_deallocate (mach_task_self (), proc->port);
}
xfree (proc);
return next;
}
static struct inf *
make_inf (void)
{
struct inf *inf = XNEW (struct inf);
inf->task = 0;
inf->threads = 0;
inf->threads_up_to_date = 0;
inf->pid = 0;
inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
inf->wait.thread = 0;
inf->wait.exc.handler = MACH_PORT_NULL;
inf->wait.exc.reply = MACH_PORT_NULL;
inf->step_thread = 0;
inf->signal_thread = 0;
inf->event_port = MACH_PORT_NULL;
inf->running = 0;
inf->stopped = 0;
inf->nomsg = 1;
inf->traced = 0;
inf->no_wait = 0;
inf->pending_execs = 0;
inf->pause_sc = 1;
inf->detach_sc = 0;
inf->default_thread_run_sc = 0;
inf->default_thread_pause_sc = 0;
inf->default_thread_detach_sc = 0;
inf->want_signals = 1; /* By default */
inf->want_exceptions = 1; /* By default */
return inf;
}
/* Clear INF's target wait status. */
void
inf_clear_wait (struct inf *inf)
{
inf_debug (inf, "clearing wait");
inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
inf->wait.thread = 0;
inf->wait.suppress = 0;
if (inf->wait.exc.handler != MACH_PORT_NULL)
{
mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
inf->wait.exc.handler = MACH_PORT_NULL;
}
if (inf->wait.exc.reply != MACH_PORT_NULL)
{
mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
inf->wait.exc.reply = MACH_PORT_NULL;
}
}
void
inf_cleanup (struct inf *inf)
{
inf_debug (inf, "cleanup");
inf_clear_wait (inf);
inf_set_pid (inf, -1);
inf->pid = 0;
inf->running = 0;
inf->stopped = 0;
inf->nomsg = 1;
inf->traced = 0;
inf->no_wait = 0;
inf->pending_execs = 0;
if (inf->event_port)
{
mach_port_destroy (mach_task_self (), inf->event_port);
inf->event_port = MACH_PORT_NULL;
}
}
void
inf_startup (struct inf *inf, int pid)
{
error_t err;
inf_debug (inf, "startup: pid = %d", pid);
inf_cleanup (inf);
/* Make the port on which we receive all events. */
err = mach_port_allocate (mach_task_self (),
MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
if (err)
error (_("Error allocating event port: %s"), safe_strerror (err));
/* Make a send right for it, so we can easily copy it for other people. */
mach_port_insert_right (mach_task_self (), inf->event_port,
inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
inf_set_pid (inf, pid);
}
/* Close current process, if any, and attach INF to process PORT. */
void
inf_set_pid (struct inf *inf, pid_t pid)
{
task_t task_port;
struct proc *task = inf->task;
inf_debug (inf, "setting pid: %d", pid);
if (pid < 0)
task_port = MACH_PORT_NULL;
else
{
error_t err = proc_pid2task (proc_server, pid, &task_port);
if (err)
error (_("Error getting task for pid %d: %s"),
pid, safe_strerror (err));
}
inf_debug (inf, "setting task: %lu", task_port);
if (inf->pause_sc)
task_suspend (task_port);
if (task && task->port != task_port)
{
inf->task = 0;
inf_validate_procs (inf); /* Trash all the threads. */
_proc_free (task); /* And the task. */
}
if (task_port != MACH_PORT_NULL)
{
inf->task = make_proc (inf, task_port, PROC_TID_TASK);
inf->threads_up_to_date = 0;
}
if (inf->task)
{
inf->pid = pid;
if (inf->pause_sc)
/* Reflect task_suspend above. */
inf->task->sc = inf->task->cur_sc = 1;
}
else
inf->pid = -1;
}
/* Validates INF's stopped, nomsg and traced field from the actual
proc server state. Note that the traced field is only updated from
the proc server state if we do not have a message port. If we do
have a message port we'd better look at the tracemask itself. */
static void
inf_validate_procinfo (struct inf *inf)
{
char *noise;
mach_msg_type_number_t noise_len = 0;
struct procinfo *pi;
mach_msg_type_number_t pi_len = 0;
int info_flags = 0;
error_t err =
proc_getprocinfo (proc_server, inf->pid, &info_flags,
(procinfo_t *) &pi, &pi_len, &noise, &noise_len);
if (!err)
{
inf->stopped = !!(pi->state & PI_STOPPED);
inf->nomsg = !!(pi->state & PI_NOMSG);
if (inf->nomsg)
inf->traced = !!(pi->state & PI_TRACED);
vm_deallocate (mach_task_self (), (vm_address_t) pi,
pi_len * sizeof (*(procinfo_t) 0));
if (noise_len > 0)
vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
}
}
/* Validates INF's task suspend count. If it's higher than we expect,
verify with the user before `stealing' the extra count. */
static void
inf_validate_task_sc (struct inf *inf)
{
char *noise;
mach_msg_type_number_t noise_len = 0;
struct procinfo *pi;
mach_msg_type_number_t pi_len = 0;
int info_flags = PI_FETCH_TASKINFO;
int suspend_count = -1;
error_t err;
retry:
err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
(procinfo_t *) &pi, &pi_len, &noise, &noise_len);
if (err)
{
inf->task->dead = 1; /* oh well */
return;
}
if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
{
/* The proc server might have suspended the task while stopping
it. This happens when the task is handling a traced signal.
Refetch the suspend count. The proc server should be
finished stopping the task by now. */
suspend_count = pi->taskinfo.suspend_count;
goto retry;
}
suspend_count = pi->taskinfo.suspend_count;
vm_deallocate (mach_task_self (), (vm_address_t) pi,
pi_len * sizeof (*(procinfo_t) 0));
if (noise_len > 0)
vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
if (inf->task->cur_sc < suspend_count)
{
int abort;
target_terminal_ours (); /* Allow I/O. */
abort = !query (_("Pid %d has an additional task suspend count of %d;"
" clear it? "), inf->pid,
suspend_count - inf->task->cur_sc);
target_terminal_inferior (); /* Give it back to the child. */
if (abort)
error (_("Additional task suspend count left untouched."));
inf->task->cur_sc = suspend_count;
}
}
/* Turns tracing for INF on or off, depending on ON, unless it already
is. If INF is running, the resume_sc count of INF's threads will
be modified, and the signal thread will briefly be run to change
the trace state. */
static void
inf_set_traced (struct inf *inf, int on)
{
if (on == inf->traced)
return;
if (inf->task && !inf->task->dead)
/* Make it take effect immediately. */
{
sigset_t mask = on ? ~(sigset_t) 0 : 0;
error_t err =
INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
INIT_TRACEMASK, mask));
if (err == EIEIO)
{
if (on)
warning (_("Can't modify tracing state for pid %d: %s"),
inf->pid, "No signal thread");
inf->traced = on;
}
else if (err)
warning (_("Can't modify tracing state for pid %d: %s"),
inf->pid, safe_strerror (err));
else
inf->traced = on;
}
else
inf->traced = on;
}
/* Makes all the real suspend count deltas of all the procs in INF
match the desired values. Careful to always do thread/task suspend
counts in the safe order. Returns true if at least one thread is
thought to be running. */
int
inf_update_suspends (struct inf *inf)
{
struct proc *task = inf->task;
/* We don't have to update INF->threads even though we're iterating over it
because we'll change a thread only if it already has an existing proc
entry. */
inf_debug (inf, "updating suspend counts");
if (task)
{
struct proc *thread;
int task_running = (task->sc == 0), thread_running = 0;
if (task->sc > task->cur_sc)
/* The task is becoming _more_ suspended; do before any threads. */
task_running = proc_update_sc (task);
if (inf->pending_execs)
/* When we're waiting for an exec, things may be happening behind our
back, so be conservative. */
thread_running = 1;
/* Do all the thread suspend counts. */
for (thread = inf->threads; thread; thread = thread->next)
thread_running |= proc_update_sc (thread);
if (task->sc != task->cur_sc)
/* We didn't do the task first, because we wanted to wait for the
threads; do it now. */
task_running = proc_update_sc (task);
inf_debug (inf, "%srunning...",
(thread_running && task_running) ? "" : "not ");
inf->running = thread_running && task_running;
/* Once any thread has executed some code, we can't depend on the
threads list any more. */
if (inf->running)
inf->threads_up_to_date = 0;
return inf->running;
}
return 0;
}
/* Converts a GDB pid to a struct proc. */
struct proc *
inf_tid_to_thread (struct inf *inf, int tid)
{
struct proc *thread = inf->threads;
while (thread)
if (thread->tid == tid)
return thread;
else
thread = thread->next;
return 0;
}
/* Converts a thread port to a struct proc. */
static struct proc *
inf_port_to_thread (struct inf *inf, mach_port_t port)
{
struct proc *thread = inf->threads;
while (thread)
if (thread->port == port)
return thread;
else
thread = thread->next;
return 0;
}
/* See gnu-nat.h. */
void
inf_threads (struct inf *inf, inf_threads_ftype *f, void *arg)
{
struct proc *thread;
for (thread = inf->threads; thread; thread = thread->next)
f (thread, arg);
}
/* Make INF's list of threads be consistent with reality of TASK. */