forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux-nat.c
5240 lines (4309 loc) · 146 KB
/
linux-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
/* GNU/Linux native-dependent code common to multiple platforms.
Copyright (C) 2001-2015 Free Software Foundation, Inc.
This file is part of GDB.
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 "inferior.h"
#include "infrun.h"
#include "target.h"
#include "nat/linux-nat.h"
#include "nat/linux-waitpid.h"
#include "gdb_wait.h"
#ifdef HAVE_TKILL_SYSCALL
#include <unistd.h>
#include <sys/syscall.h>
#endif
#include "nat/gdb_ptrace.h"
#include "linux-nat.h"
#include "nat/linux-ptrace.h"
#include "nat/linux-procfs.h"
#include "nat/linux-personality.h"
#include "linux-fork.h"
#include "gdbthread.h"
#include "gdbcmd.h"
#include "regcache.h"
#include "regset.h"
#include "inf-child.h"
#include "inf-ptrace.h"
#include "auxv.h"
#include <sys/procfs.h> /* for elf_gregset etc. */
#include "elf-bfd.h" /* for elfcore_write_* */
#include "gregset.h" /* for gregset */
#include "gdbcore.h" /* for get_exec_file */
#include <ctype.h> /* for isdigit */
#include <sys/stat.h> /* for struct stat */
#include <fcntl.h> /* for O_RDONLY */
#include "inf-loop.h"
#include "event-loop.h"
#include "event-top.h"
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include "xml-support.h"
#include <sys/vfs.h>
#include "solib.h"
#include "nat/linux-osdata.h"
#include "linux-tdep.h"
#include "symfile.h"
#include "agent.h"
#include "tracepoint.h"
#include "buffer.h"
#include "target-descriptions.h"
#include "filestuff.h"
#include "objfiles.h"
#include "nat/linux-namespaces.h"
#include "fileio.h"
#ifndef SPUFS_MAGIC
#define SPUFS_MAGIC 0x23c9b64e
#endif
/* This comment documents high-level logic of this file.
Waiting for events in sync mode
===============================
When waiting for an event in a specific thread, we just use waitpid, passing
the specific pid, and not passing WNOHANG.
When waiting for an event in all threads, waitpid is not quite good. Prior to
version 2.4, Linux can either wait for event in main thread, or in secondary
threads. (2.4 has the __WALL flag). So, if we use blocking waitpid, we might
miss an event. The solution is to use non-blocking waitpid, together with
sigsuspend. First, we use non-blocking waitpid to get an event in the main
process, if any. Second, we use non-blocking waitpid with the __WCLONED
flag to check for events in cloned processes. If nothing is found, we use
sigsuspend to wait for SIGCHLD. When SIGCHLD arrives, it means something
happened to a child process -- and SIGCHLD will be delivered both for events
in main debugged process and in cloned processes. As soon as we know there's
an event, we get back to calling nonblocking waitpid with and without
__WCLONED.
Note that SIGCHLD should be blocked between waitpid and sigsuspend calls,
so that we don't miss a signal. If SIGCHLD arrives in between, when it's
blocked, the signal becomes pending and sigsuspend immediately
notices it and returns.
Waiting for events in async mode
================================
In async mode, GDB should always be ready to handle both user input
and target events, so neither blocking waitpid nor sigsuspend are
viable options. Instead, we should asynchronously notify the GDB main
event loop whenever there's an unprocessed event from the target. We
detect asynchronous target events by handling SIGCHLD signals. To
notify the event loop about target events, the self-pipe trick is used
--- a pipe is registered as waitable event source in the event loop,
the event loop select/poll's on the read end of this pipe (as well on
other event sources, e.g., stdin), and the SIGCHLD handler writes a
byte to this pipe. This is more portable than relying on
pselect/ppoll, since on kernels that lack those syscalls, libc
emulates them with select/poll+sigprocmask, and that is racy
(a.k.a. plain broken).
Obviously, if we fail to notify the event loop if there's a target
event, it's bad. OTOH, if we notify the event loop when there's no
event from the target, linux_nat_wait will detect that there's no real
event to report, and return event of type TARGET_WAITKIND_IGNORE.
This is mostly harmless, but it will waste time and is better avoided.
The main design point is that every time GDB is outside linux-nat.c,
we have a SIGCHLD handler installed that is called when something
happens to the target and notifies the GDB event loop. Whenever GDB
core decides to handle the event, and calls into linux-nat.c, we
process things as in sync mode, except that the we never block in
sigsuspend.
While processing an event, we may end up momentarily blocked in
waitpid calls. Those waitpid calls, while blocking, are guarantied to
return quickly. E.g., in all-stop mode, before reporting to the core
that an LWP hit a breakpoint, all LWPs are stopped by sending them
SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
Note that this is different from blocking indefinitely waiting for the
next event --- here, we're already handling an event.
Use of signals
==============
We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
signal is not entirely significant; we just need for a signal to be delivered,
so that we can intercept it. SIGSTOP's advantage is that it can not be
blocked. A disadvantage is that it is not a real-time signal, so it can only
be queued once; we do not keep track of other sources of SIGSTOP.
Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
use them, because they have special behavior when the signal is generated -
not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
kills the entire thread group.
A delivered SIGSTOP would stop the entire thread group, not just the thread we
tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
cancel it (by PTRACE_CONT without passing SIGSTOP).
We could use a real-time signal instead. This would solve those problems; we
could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
generates it, and there are races with trying to find a signal that is not
blocked. */
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
/* Does the current host support PTRACE_GETREGSET? */
enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN;
/* The single-threaded native GNU/Linux target_ops. We save a pointer for
the use of the multi-threaded target. */
static struct target_ops *linux_ops;
static struct target_ops linux_ops_saved;
/* The method to call, if any, when a new thread is attached. */
static void (*linux_nat_new_thread) (struct lwp_info *);
/* The method to call, if any, when a new fork is attached. */
static linux_nat_new_fork_ftype *linux_nat_new_fork;
/* The method to call, if any, when a process is no longer
attached. */
static linux_nat_forget_process_ftype *linux_nat_forget_process_hook;
/* Hook to call prior to resuming a thread. */
static void (*linux_nat_prepare_to_resume) (struct lwp_info *);
/* The method to call, if any, when the siginfo object needs to be
converted between the layout returned by ptrace, and the layout in
the architecture of the inferior. */
static int (*linux_nat_siginfo_fixup) (siginfo_t *,
gdb_byte *,
int);
/* The saved to_xfer_partial method, inherited from inf-ptrace.c.
Called by our to_xfer_partial. */
static target_xfer_partial_ftype *super_xfer_partial;
/* The saved to_close method, inherited from inf-ptrace.c.
Called by our to_close. */
static void (*super_close) (struct target_ops *);
static unsigned int debug_linux_nat;
static void
show_debug_linux_nat (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Debugging of GNU/Linux lwp module is %s.\n"),
value);
}
struct simple_pid_list
{
int pid;
int status;
struct simple_pid_list *next;
};
struct simple_pid_list *stopped_pids;
/* Async mode support. */
/* The read/write ends of the pipe registered as waitable file in the
event loop. */
static int linux_nat_event_pipe[2] = { -1, -1 };
/* True if we're currently in async mode. */
#define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
/* Flush the event pipe. */
static void
async_file_flush (void)
{
int ret;
char buf;
do
{
ret = read (linux_nat_event_pipe[0], &buf, 1);
}
while (ret >= 0 || (ret == -1 && errno == EINTR));
}
/* Put something (anything, doesn't matter what, or how much) in event
pipe, so that the select/poll in the event-loop realizes we have
something to process. */
static void
async_file_mark (void)
{
int ret;
/* It doesn't really matter what the pipe contains, as long we end
up with something in it. Might as well flush the previous
left-overs. */
async_file_flush ();
do
{
ret = write (linux_nat_event_pipe[1], "+", 1);
}
while (ret == -1 && errno == EINTR);
/* Ignore EAGAIN. If the pipe is full, the event loop will already
be awakened anyway. */
}
static int kill_lwp (int lwpid, int signo);
static int stop_callback (struct lwp_info *lp, void *data);
static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
static void block_child_signals (sigset_t *prev_mask);
static void restore_child_signals_mask (sigset_t *prev_mask);
struct lwp_info;
static struct lwp_info *add_lwp (ptid_t ptid);
static void purge_lwp_list (int pid);
static void delete_lwp (ptid_t ptid);
static struct lwp_info *find_lwp_pid (ptid_t ptid);
static int lwp_status_pending_p (struct lwp_info *lp);
static int check_stopped_by_breakpoint (struct lwp_info *lp);
static int sigtrap_is_event (int status);
static int (*linux_nat_status_is_event) (int status) = sigtrap_is_event;
/* LWP accessors. */
/* See nat/linux-nat.h. */
ptid_t
ptid_of_lwp (struct lwp_info *lwp)
{
return lwp->ptid;
}
/* See nat/linux-nat.h. */
void
lwp_set_arch_private_info (struct lwp_info *lwp,
struct arch_lwp_info *info)
{
lwp->arch_private = info;
}
/* See nat/linux-nat.h. */
struct arch_lwp_info *
lwp_arch_private_info (struct lwp_info *lwp)
{
return lwp->arch_private;
}
/* See nat/linux-nat.h. */
int
lwp_is_stopped (struct lwp_info *lwp)
{
return lwp->stopped;
}
/* See nat/linux-nat.h. */
enum target_stop_reason
lwp_stop_reason (struct lwp_info *lwp)
{
return lwp->stop_reason;
}
/* Trivial list manipulation functions to keep track of a list of
new stopped processes. */
static void
add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
{
struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
new_pid->pid = pid;
new_pid->status = status;
new_pid->next = *listp;
*listp = new_pid;
}
static int
in_pid_list_p (struct simple_pid_list *list, int pid)
{
struct simple_pid_list *p;
for (p = list; p != NULL; p = p->next)
if (p->pid == pid)
return 1;
return 0;
}
static int
pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
{
struct simple_pid_list **p;
for (p = listp; *p != NULL; p = &(*p)->next)
if ((*p)->pid == pid)
{
struct simple_pid_list *next = (*p)->next;
*statusp = (*p)->status;
xfree (*p);
*p = next;
return 1;
}
return 0;
}
/* Return the ptrace options that we want to try to enable. */
static int
linux_nat_ptrace_options (int attached)
{
int options = 0;
if (!attached)
options |= PTRACE_O_EXITKILL;
options |= (PTRACE_O_TRACESYSGOOD
| PTRACE_O_TRACEVFORKDONE
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEFORK
| PTRACE_O_TRACEEXEC);
return options;
}
/* Initialize ptrace warnings and check for supported ptrace
features given PID.
ATTACHED should be nonzero iff we attached to the inferior. */
static void
linux_init_ptrace (pid_t pid, int attached)
{
int options = linux_nat_ptrace_options (attached);
linux_enable_event_reporting (pid, options);
linux_ptrace_init_warnings ();
}
static void
linux_child_post_attach (struct target_ops *self, int pid)
{
linux_init_ptrace (pid, 1);
}
static void
linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
{
linux_init_ptrace (ptid_get_pid (ptid), 0);
}
/* Return the number of known LWPs in the tgid given by PID. */
static int
num_lwps (int pid)
{
int count = 0;
struct lwp_info *lp;
for (lp = lwp_list; lp; lp = lp->next)
if (ptid_get_pid (lp->ptid) == pid)
count++;
return count;
}
/* Call delete_lwp with prototype compatible for make_cleanup. */
static void
delete_lwp_cleanup (void *lp_voidp)
{
struct lwp_info *lp = lp_voidp;
delete_lwp (lp->ptid);
}
/* Target hook for follow_fork. On entry inferior_ptid must be the
ptid of the followed inferior. At return, inferior_ptid will be
unchanged. */
static int
linux_child_follow_fork (struct target_ops *ops, int follow_child,
int detach_fork)
{
if (!follow_child)
{
struct lwp_info *child_lp = NULL;
int status = W_STOPCODE (0);
struct cleanup *old_chain;
int has_vforked;
ptid_t parent_ptid, child_ptid;
int parent_pid, child_pid;
has_vforked = (inferior_thread ()->pending_follow.kind
== TARGET_WAITKIND_VFORKED);
parent_ptid = inferior_ptid;
child_ptid = inferior_thread ()->pending_follow.value.related_pid;
parent_pid = ptid_get_lwp (parent_ptid);
child_pid = ptid_get_lwp (child_ptid);
/* We're already attached to the parent, by default. */
old_chain = save_inferior_ptid ();
inferior_ptid = child_ptid;
child_lp = add_lwp (inferior_ptid);
child_lp->stopped = 1;
child_lp->last_resume_kind = resume_stop;
/* Detach new forked process? */
if (detach_fork)
{
make_cleanup (delete_lwp_cleanup, child_lp);
if (linux_nat_prepare_to_resume != NULL)
linux_nat_prepare_to_resume (child_lp);
/* When debugging an inferior in an architecture that supports
hardware single stepping on a kernel without commit
6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
set if the parent process had them set.
To work around this, single step the child process
once before detaching to clear the flags. */
if (!gdbarch_software_single_step_p (target_thread_architecture
(child_lp->ptid)))
{
linux_disable_event_reporting (child_pid);
if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
perror_with_name (_("Couldn't do single step"));
if (my_waitpid (child_pid, &status, 0) < 0)
perror_with_name (_("Couldn't wait vfork process"));
}
if (WIFSTOPPED (status))
{
int signo;
signo = WSTOPSIG (status);
if (signo != 0
&& !signal_pass_state (gdb_signal_from_host (signo)))
signo = 0;
ptrace (PTRACE_DETACH, child_pid, 0, signo);
}
/* Resets value of inferior_ptid to parent ptid. */
do_cleanups (old_chain);
}
else
{
/* Let the thread_db layer learn about this new process. */
check_for_thread_db ();
}
do_cleanups (old_chain);
if (has_vforked)
{
struct lwp_info *parent_lp;
parent_lp = find_lwp_pid (parent_ptid);
gdb_assert (linux_supports_tracefork () >= 0);
if (linux_supports_tracevforkdone ())
{
if (debug_linux_nat)
fprintf_unfiltered (gdb_stdlog,
"LCFF: waiting for VFORK_DONE on %d\n",
parent_pid);
parent_lp->stopped = 1;
/* We'll handle the VFORK_DONE event like any other
event, in target_wait. */
}
else
{
/* We can't insert breakpoints until the child has
finished with the shared memory region. We need to
wait until that happens. Ideal would be to just
call:
- ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
- waitpid (parent_pid, &status, __WALL);
However, most architectures can't handle a syscall
being traced on the way out if it wasn't traced on
the way in.
We might also think to loop, continuing the child
until it exits or gets a SIGTRAP. One problem is
that the child might call ptrace with PTRACE_TRACEME.
There's no simple and reliable way to figure out when
the vforked child will be done with its copy of the
shared memory. We could step it out of the syscall,
two instructions, let it go, and then single-step the
parent once. When we have hardware single-step, this
would work; with software single-step it could still
be made to work but we'd have to be able to insert
single-step breakpoints in the child, and we'd have
to insert -just- the single-step breakpoint in the
parent. Very awkward.
In the end, the best we can do is to make sure it
runs for a little while. Hopefully it will be out of
range of any breakpoints we reinsert. Usually this
is only the single-step breakpoint at vfork's return
point. */
if (debug_linux_nat)
fprintf_unfiltered (gdb_stdlog,
"LCFF: no VFORK_DONE "
"support, sleeping a bit\n");
usleep (10000);
/* Pretend we've seen a PTRACE_EVENT_VFORK_DONE event,
and leave it pending. The next linux_nat_resume call
will notice a pending event, and bypasses actually
resuming the inferior. */
parent_lp->status = 0;
parent_lp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
parent_lp->stopped = 1;
/* If we're in async mode, need to tell the event loop
there's something here to process. */
if (target_is_async_p ())
async_file_mark ();
}
}
}
else
{
struct lwp_info *child_lp;
child_lp = add_lwp (inferior_ptid);
child_lp->stopped = 1;
child_lp->last_resume_kind = resume_stop;
/* Let the thread_db layer learn about this new process. */
check_for_thread_db ();
}
return 0;
}
static int
linux_child_insert_fork_catchpoint (struct target_ops *self, int pid)
{
return !linux_supports_tracefork ();
}
static int
linux_child_remove_fork_catchpoint (struct target_ops *self, int pid)
{
return 0;
}
static int
linux_child_insert_vfork_catchpoint (struct target_ops *self, int pid)
{
return !linux_supports_tracefork ();
}
static int
linux_child_remove_vfork_catchpoint (struct target_ops *self, int pid)
{
return 0;
}
static int
linux_child_insert_exec_catchpoint (struct target_ops *self, int pid)
{
return !linux_supports_tracefork ();
}
static int
linux_child_remove_exec_catchpoint (struct target_ops *self, int pid)
{
return 0;
}
static int
linux_child_set_syscall_catchpoint (struct target_ops *self,
int pid, int needed, int any_count,
int table_size, int *table)
{
if (!linux_supports_tracesysgood ())
return 1;
/* On GNU/Linux, we ignore the arguments. It means that we only
enable the syscall catchpoints, but do not disable them.
Also, we do not use the `table' information because we do not
filter system calls here. We let GDB do the logic for us. */
return 0;
}
/* On GNU/Linux there are no real LWP's. The closest thing to LWP's
are processes sharing the same VM space. A multi-threaded process
is basically a group of such processes. However, such a grouping
is almost entirely a user-space issue; the kernel doesn't enforce
such a grouping at all (this might change in the future). In
general, we'll rely on the threads library (i.e. the GNU/Linux
Threads library) to provide such a grouping.
It is perfectly well possible to write a multi-threaded application
without the assistance of a threads library, by using the clone
system call directly. This module should be able to give some
rudimentary support for debugging such applications if developers
specify the CLONE_PTRACE flag in the clone system call, and are
using the Linux kernel 2.4 or above.
Note that there are some peculiarities in GNU/Linux that affect
this code:
- In general one should specify the __WCLONE flag to waitpid in
order to make it report events for any of the cloned processes
(and leave it out for the initial process). However, if a cloned
process has exited the exit status is only reported if the
__WCLONE flag is absent. Linux kernel 2.4 has a __WALL flag, but
we cannot use it since GDB must work on older systems too.
- When a traced, cloned process exits and is waited for by the
debugger, the kernel reassigns it to the original parent and
keeps it around as a "zombie". Somehow, the GNU/Linux Threads
library doesn't notice this, which leads to the "zombie problem":
When debugged a multi-threaded process that spawns a lot of
threads will run out of processes, even if the threads exit,
because the "zombies" stay around. */
/* List of known LWPs. */
struct lwp_info *lwp_list;
/* Original signal mask. */
static sigset_t normal_mask;
/* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
_initialize_linux_nat. */
static sigset_t suspend_mask;
/* Signals to block to make that sigsuspend work. */
static sigset_t blocked_mask;
/* SIGCHLD action. */
struct sigaction sigchld_action;
/* Block child signals (SIGCHLD and linux threads signals), and store
the previous mask in PREV_MASK. */
static void
block_child_signals (sigset_t *prev_mask)
{
/* Make sure SIGCHLD is blocked. */
if (!sigismember (&blocked_mask, SIGCHLD))
sigaddset (&blocked_mask, SIGCHLD);
sigprocmask (SIG_BLOCK, &blocked_mask, prev_mask);
}
/* Restore child signals mask, previously returned by
block_child_signals. */
static void
restore_child_signals_mask (sigset_t *prev_mask)
{
sigprocmask (SIG_SETMASK, prev_mask, NULL);
}
/* Mask of signals to pass directly to the inferior. */
static sigset_t pass_mask;
/* Update signals to pass to the inferior. */
static void
linux_nat_pass_signals (struct target_ops *self,
int numsigs, unsigned char *pass_signals)
{
int signo;
sigemptyset (&pass_mask);
for (signo = 1; signo < NSIG; signo++)
{
int target_signo = gdb_signal_from_host (signo);
if (target_signo < numsigs && pass_signals[target_signo])
sigaddset (&pass_mask, signo);
}
}
/* Prototypes for local functions. */
static int stop_wait_callback (struct lwp_info *lp, void *data);
static int linux_thread_alive (ptid_t ptid);
static char *linux_child_pid_to_exec_file (struct target_ops *self, int pid);
static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
/* Destroy and free LP. */
static void
lwp_free (struct lwp_info *lp)
{
xfree (lp->arch_private);
xfree (lp);
}
/* Remove all LWPs belong to PID from the lwp list. */
static void
purge_lwp_list (int pid)
{
struct lwp_info *lp, *lpprev, *lpnext;
lpprev = NULL;
for (lp = lwp_list; lp; lp = lpnext)
{
lpnext = lp->next;
if (ptid_get_pid (lp->ptid) == pid)
{
if (lp == lwp_list)
lwp_list = lp->next;
else
lpprev->next = lp->next;
lwp_free (lp);
}
else
lpprev = lp;
}
}
/* Add the LWP specified by PTID to the list. PTID is the first LWP
in the process. Return a pointer to the structure describing the
new LWP.
This differs from add_lwp in that we don't let the arch specific
bits know about this new thread. Current clients of this callback
take the opportunity to install watchpoints in the new thread, and
we shouldn't do that for the first thread. If we're spawning a
child ("run"), the thread executes the shell wrapper first, and we
shouldn't touch it until it execs the program we want to debug.
For "attach", it'd be okay to call the callback, but it's not
necessary, because watchpoints can't yet have been inserted into
the inferior. */
static struct lwp_info *
add_initial_lwp (ptid_t ptid)
{
struct lwp_info *lp;
gdb_assert (ptid_lwp_p (ptid));
lp = XNEW (struct lwp_info);
memset (lp, 0, sizeof (struct lwp_info));
lp->last_resume_kind = resume_continue;
lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
lp->ptid = ptid;
lp->core = -1;
lp->next = lwp_list;
lwp_list = lp;
return lp;
}
/* Add the LWP specified by PID to the list. Return a pointer to the
structure describing the new LWP. The LWP should already be
stopped. */
static struct lwp_info *
add_lwp (ptid_t ptid)
{
struct lwp_info *lp;
lp = add_initial_lwp (ptid);
/* Let the arch specific bits know about this new thread. Current
clients of this callback take the opportunity to install
watchpoints in the new thread. We don't do this for the first
thread though. See add_initial_lwp. */
if (linux_nat_new_thread != NULL)
linux_nat_new_thread (lp);
return lp;
}
/* Remove the LWP specified by PID from the list. */
static void
delete_lwp (ptid_t ptid)
{
struct lwp_info *lp, *lpprev;
lpprev = NULL;
for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
if (ptid_equal (lp->ptid, ptid))
break;
if (!lp)
return;
if (lpprev)
lpprev->next = lp->next;
else
lwp_list = lp->next;
lwp_free (lp);
}
/* Return a pointer to the structure describing the LWP corresponding
to PID. If no corresponding LWP could be found, return NULL. */
static struct lwp_info *
find_lwp_pid (ptid_t ptid)
{
struct lwp_info *lp;
int lwp;
if (ptid_lwp_p (ptid))
lwp = ptid_get_lwp (ptid);
else
lwp = ptid_get_pid (ptid);
for (lp = lwp_list; lp; lp = lp->next)
if (lwp == ptid_get_lwp (lp->ptid))
return lp;
return NULL;
}
/* See nat/linux-nat.h. */
struct lwp_info *
iterate_over_lwps (ptid_t filter,
iterate_over_lwps_ftype callback,
void *data)
{
struct lwp_info *lp, *lpnext;
for (lp = lwp_list; lp; lp = lpnext)
{
lpnext = lp->next;
if (ptid_match (lp->ptid, filter))
{
if ((*callback) (lp, data) != 0)
return lp;
}
}
return NULL;
}
/* Update our internal state when changing from one checkpoint to
another indicated by NEW_PTID. We can only switch single-threaded
applications, so we only create one new LWP, and the previous list
is discarded. */
void
linux_nat_switch_fork (ptid_t new_ptid)
{
struct lwp_info *lp;
purge_lwp_list (ptid_get_pid (inferior_ptid));
lp = add_lwp (new_ptid);
lp->stopped = 1;
/* This changes the thread's ptid while preserving the gdb thread
num. Also changes the inferior pid, while preserving the
inferior num. */
thread_change_ptid (inferior_ptid, new_ptid);
/* We've just told GDB core that the thread changed target id, but,
in fact, it really is a different thread, with different register
contents. */
registers_changed ();
}
/* Handle the exit of a single thread LP. */
static void
exit_lwp (struct lwp_info *lp)
{
struct thread_info *th = find_thread_ptid (lp->ptid);
if (th)
{
if (print_thread_events)
printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (lp->ptid));
delete_thread (lp->ptid);
}
delete_lwp (lp->ptid);
}
/* Wait for the LWP specified by LP, which we have just attached to.
Returns a wait status for that LWP, to cache. */
static int
linux_nat_post_attach_wait (ptid_t ptid, int first, int *cloned,
int *signalled)
{
pid_t new_pid, pid = ptid_get_lwp (ptid);
int status;
if (linux_proc_pid_is_stopped (pid))
{
if (debug_linux_nat)
fprintf_unfiltered (gdb_stdlog,
"LNPAW: Attaching to a stopped process\n");
/* The process is definitely stopped. It is in a job control
stop, unless the kernel predates the TASK_STOPPED /
TASK_TRACED distinction, in which case it might be in a
ptrace stop. Make sure it is in a ptrace stop; from there we
can kill it, signal it, et cetera.
First make sure there is a pending SIGSTOP. Since we are
already attached, the process can not transition from stopped
to running without a PTRACE_CONT; so we know this signal will
go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
probably already in the queue (unless this kernel is old