forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux-low.c
7732 lines (6415 loc) · 211 KB
/
linux-low.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
/* Low level interface to ptrace, for the remote server for GDB.
Copyright (C) 1995-2016 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 "server.h"
#include "linux-low.h"
#include "nat/linux-osdata.h"
#include "agent.h"
#include "tdesc.h"
#include "rsp-low.h"
#include "signals-state-save-restore.h"
#include "nat/linux-nat.h"
#include "nat/linux-waitpid.h"
#include "gdb_wait.h"
#include "nat/gdb_ptrace.h"
#include "nat/linux-ptrace.h"
#include "nat/linux-procfs.h"
#include "nat/linux-personality.h"
#include <signal.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sched.h>
#include <ctype.h>
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/uio.h>
#include "filestuff.h"
#include "tracepoint.h"
#include "hostio.h"
#include <inttypes.h>
#ifndef ELFMAG0
/* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
then ELFMAG0 will have been defined. If it didn't get included by
gdb_proc_service.h then including it will likely introduce a duplicate
definition of elf_fpregset_t. */
#include <elf.h>
#endif
#include "nat/linux-namespaces.h"
#ifndef SPUFS_MAGIC
#define SPUFS_MAGIC 0x23c9b64e
#endif
#ifdef HAVE_PERSONALITY
# include <sys/personality.h>
# if !HAVE_DECL_ADDR_NO_RANDOMIZE
# define ADDR_NO_RANDOMIZE 0x0040000
# endif
#endif
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
/* Some targets did not define these ptrace constants from the start,
so gdbserver defines them locally here. In the future, these may
be removed after they are added to asm/ptrace.h. */
#if !(defined(PT_TEXT_ADDR) \
|| defined(PT_DATA_ADDR) \
|| defined(PT_TEXT_END_ADDR))
#if defined(__mcoldfire__)
/* These are still undefined in 3.10 kernels. */
#define PT_TEXT_ADDR 49*4
#define PT_DATA_ADDR 50*4
#define PT_TEXT_END_ADDR 51*4
/* BFIN already defines these since at least 2.6.32 kernels. */
#elif defined(BFIN)
#define PT_TEXT_ADDR 220
#define PT_TEXT_END_ADDR 224
#define PT_DATA_ADDR 228
/* These are still undefined in 3.10 kernels. */
#elif defined(__TMS320C6X__)
#define PT_TEXT_ADDR (0x10000*4)
#define PT_DATA_ADDR (0x10004*4)
#define PT_TEXT_END_ADDR (0x10008*4)
#endif
#endif
#ifdef HAVE_LINUX_BTRACE
# include "nat/linux-btrace.h"
# include "btrace-common.h"
#endif
#ifndef HAVE_ELF32_AUXV_T
/* Copied from glibc's elf.h. */
typedef struct
{
uint32_t a_type; /* Entry type */
union
{
uint32_t a_val; /* Integer value */
/* We use to have pointer elements added here. We cannot do that,
though, since it does not work when using 32-bit definitions
on 64-bit platforms and vice versa. */
} a_un;
} Elf32_auxv_t;
#endif
#ifndef HAVE_ELF64_AUXV_T
/* Copied from glibc's elf.h. */
typedef struct
{
uint64_t a_type; /* Entry type */
union
{
uint64_t a_val; /* Integer value */
/* We use to have pointer elements added here. We cannot do that,
though, since it does not work when using 32-bit definitions
on 64-bit platforms and vice versa. */
} a_un;
} Elf64_auxv_t;
#endif
/* Does the current host support PTRACE_GETREGSET? */
int have_ptrace_getregset = -1;
/* LWP accessors. */
/* See nat/linux-nat.h. */
ptid_t
ptid_of_lwp (struct lwp_info *lwp)
{
return ptid_of (get_lwp_thread (lwp));
}
/* 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;
}
/* See nat/linux-nat.h. */
int
lwp_is_stepping (struct lwp_info *lwp)
{
return lwp->stepping;
}
/* A list of all unknown processes which receive stop signals. Some
other process will presumably claim each of these as forked
children momentarily. */
struct simple_pid_list
{
/* The process ID. */
int pid;
/* The status as reported by waitpid. */
int status;
/* Next in chain. */
struct simple_pid_list *next;
};
struct simple_pid_list *stopped_pids;
/* 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
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;
}
enum stopping_threads_kind
{
/* Not stopping threads presently. */
NOT_STOPPING_THREADS,
/* Stopping threads. */
STOPPING_THREADS,
/* Stopping and suspending threads. */
STOPPING_AND_SUSPENDING_THREADS
};
/* This is set while stop_all_lwps is in effect. */
enum stopping_threads_kind stopping_threads = NOT_STOPPING_THREADS;
/* FIXME make into a target method? */
int using_threads = 1;
/* True if we're presently stabilizing threads (moving them out of
jump pads). */
static int stabilizing_threads;
static void linux_resume_one_lwp (struct lwp_info *lwp,
int step, int signal, siginfo_t *info);
static void linux_resume (struct thread_resume *resume_info, size_t n);
static void stop_all_lwps (int suspend, struct lwp_info *except);
static void unstop_all_lwps (int unsuspend, struct lwp_info *except);
static void unsuspend_all_lwps (struct lwp_info *except);
static int linux_wait_for_event_filtered (ptid_t wait_ptid, ptid_t filter_ptid,
int *wstat, int options);
static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
static struct lwp_info *add_lwp (ptid_t ptid);
static void linux_mourn (struct process_info *process);
static int linux_stopped_by_watchpoint (void);
static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
static int lwp_is_marked_dead (struct lwp_info *lwp);
static void proceed_all_lwps (void);
static int finish_step_over (struct lwp_info *lwp);
static int kill_lwp (unsigned long lwpid, int signo);
static void enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info);
static void complete_ongoing_step_over (void);
static int linux_low_ptrace_options (int attached);
static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
static int proceed_one_lwp (struct inferior_list_entry *entry, void *except);
/* When the event-loop is doing a step-over, this points at the thread
being stepped. */
ptid_t step_over_bkpt;
/* True if the low target can hardware single-step. */
static int
can_hardware_single_step (void)
{
if (the_low_target.supports_hardware_single_step != NULL)
return the_low_target.supports_hardware_single_step ();
else
return 0;
}
/* True if the low target can software single-step. Such targets
implement the GET_NEXT_PCS callback. */
static int
can_software_single_step (void)
{
return (the_low_target.get_next_pcs != NULL);
}
/* True if the low target supports memory breakpoints. If so, we'll
have a GET_PC implementation. */
static int
supports_breakpoints (void)
{
return (the_low_target.get_pc != NULL);
}
/* Returns true if this target can support fast tracepoints. This
does not mean that the in-process agent has been loaded in the
inferior. */
static int
supports_fast_tracepoints (void)
{
return the_low_target.install_fast_tracepoint_jump_pad != NULL;
}
/* True if LWP is stopped in its stepping range. */
static int
lwp_in_step_range (struct lwp_info *lwp)
{
CORE_ADDR pc = lwp->stop_pc;
return (pc >= lwp->step_range_start && pc < lwp->step_range_end);
}
struct pending_signals
{
int signal;
siginfo_t info;
struct pending_signals *prev;
};
/* The read/write ends of the pipe registered as waitable file in the
event loop. */
static int linux_event_pipe[2] = { -1, -1 };
/* True if we're currently in async mode. */
#define target_is_async_p() (linux_event_pipe[0] != -1)
static void send_sigstop (struct lwp_info *lwp);
static void wait_for_sigstop (void);
/* Return non-zero if HEADER is a 64-bit ELF file. */
static int
elf_64_header_p (const Elf64_Ehdr *header, unsigned int *machine)
{
if (header->e_ident[EI_MAG0] == ELFMAG0
&& header->e_ident[EI_MAG1] == ELFMAG1
&& header->e_ident[EI_MAG2] == ELFMAG2
&& header->e_ident[EI_MAG3] == ELFMAG3)
{
*machine = header->e_machine;
return header->e_ident[EI_CLASS] == ELFCLASS64;
}
*machine = EM_NONE;
return -1;
}
/* Return non-zero if FILE is a 64-bit ELF file,
zero if the file is not a 64-bit ELF file,
and -1 if the file is not accessible or doesn't exist. */
static int
elf_64_file_p (const char *file, unsigned int *machine)
{
Elf64_Ehdr header;
int fd;
fd = open (file, O_RDONLY);
if (fd < 0)
return -1;
if (read (fd, &header, sizeof (header)) != sizeof (header))
{
close (fd);
return 0;
}
close (fd);
return elf_64_header_p (&header, machine);
}
/* Accepts an integer PID; Returns true if the executable PID is
running is a 64-bit ELF file.. */
int
linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine)
{
char file[PATH_MAX];
sprintf (file, "/proc/%d/exe", pid);
return elf_64_file_p (file, machine);
}
static void
delete_lwp (struct lwp_info *lwp)
{
struct thread_info *thr = get_lwp_thread (lwp);
if (debug_threads)
debug_printf ("deleting %ld\n", lwpid_of (thr));
remove_thread (thr);
free (lwp->arch_private);
free (lwp);
}
/* Add a process to the common process list, and set its private
data. */
static struct process_info *
linux_add_process (int pid, int attached)
{
struct process_info *proc;
proc = add_process (pid, attached);
proc->priv = XCNEW (struct process_info_private);
if (the_low_target.new_process != NULL)
proc->priv->arch_private = the_low_target.new_process ();
return proc;
}
static CORE_ADDR get_pc (struct lwp_info *lwp);
/* Call the target arch_setup function on the current thread. */
static void
linux_arch_setup (void)
{
the_low_target.arch_setup ();
}
/* Call the target arch_setup function on THREAD. */
static void
linux_arch_setup_thread (struct thread_info *thread)
{
struct thread_info *saved_thread;
saved_thread = current_thread;
current_thread = thread;
linux_arch_setup ();
current_thread = saved_thread;
}
/* Handle a GNU/Linux extended wait response. If we see a clone,
fork, or vfork event, we need to add the new LWP to our list
(and return 0 so as not to report the trap to higher layers).
If we see an exec event, we will modify ORIG_EVENT_LWP to point
to a new LWP representing the new program. */
static int
handle_extended_wait (struct lwp_info **orig_event_lwp, int wstat)
{
struct lwp_info *event_lwp = *orig_event_lwp;
int event = linux_ptrace_get_extended_event (wstat);
struct thread_info *event_thr = get_lwp_thread (event_lwp);
struct lwp_info *new_lwp;
gdb_assert (event_lwp->waitstatus.kind == TARGET_WAITKIND_IGNORE);
/* All extended events we currently use are mid-syscall. Only
PTRACE_EVENT_STOP is delivered more like a signal-stop, but
you have to be using PTRACE_SEIZE to get that. */
event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
if ((event == PTRACE_EVENT_FORK) || (event == PTRACE_EVENT_VFORK)
|| (event == PTRACE_EVENT_CLONE))
{
ptid_t ptid;
unsigned long new_pid;
int ret, status;
/* Get the pid of the new lwp. */
ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
&new_pid);
/* If we haven't already seen the new PID stop, wait for it now. */
if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
{
/* The new child has a pending SIGSTOP. We can't affect it until it
hits the SIGSTOP, but we're already attached. */
ret = my_waitpid (new_pid, &status, __WALL);
if (ret == -1)
perror_with_name ("waiting for new child");
else if (ret != new_pid)
warning ("wait returned unexpected PID %d", ret);
else if (!WIFSTOPPED (status))
warning ("wait returned unexpected status 0x%x", status);
}
if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
{
struct process_info *parent_proc;
struct process_info *child_proc;
struct lwp_info *child_lwp;
struct thread_info *child_thr;
struct target_desc *tdesc;
ptid = ptid_build (new_pid, new_pid, 0);
if (debug_threads)
{
debug_printf ("HEW: Got fork event from LWP %ld, "
"new child is %d\n",
ptid_get_lwp (ptid_of (event_thr)),
ptid_get_pid (ptid));
}
/* Add the new process to the tables and clone the breakpoint
lists of the parent. We need to do this even if the new process
will be detached, since we will need the process object and the
breakpoints to remove any breakpoints from memory when we
detach, and the client side will access registers. */
child_proc = linux_add_process (new_pid, 0);
gdb_assert (child_proc != NULL);
child_lwp = add_lwp (ptid);
gdb_assert (child_lwp != NULL);
child_lwp->stopped = 1;
child_lwp->must_set_ptrace_flags = 1;
child_lwp->status_pending_p = 0;
child_thr = get_lwp_thread (child_lwp);
child_thr->last_resume_kind = resume_stop;
child_thr->last_status.kind = TARGET_WAITKIND_STOPPED;
/* If we're suspending all threads, leave this one suspended
too. If the fork/clone parent is stepping over a breakpoint,
all other threads have been suspended already. Leave the
child suspended too. */
if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
|| event_lwp->bp_reinsert != 0)
{
if (debug_threads)
debug_printf ("HEW: leaving child suspended\n");
child_lwp->suspended = 1;
}
parent_proc = get_thread_process (event_thr);
child_proc->attached = parent_proc->attached;
if (event_lwp->bp_reinsert != 0
&& can_software_single_step ()
&& event == PTRACE_EVENT_VFORK)
{
/* If we leave single-step breakpoints there, child will
hit it, so uninsert single-step breakpoints from parent
(and child). Once vfork child is done, reinsert
them back to parent. */
uninsert_single_step_breakpoints (event_thr);
}
clone_all_breakpoints (child_thr, event_thr);
tdesc = XNEW (struct target_desc);
copy_target_description (tdesc, parent_proc->tdesc);
child_proc->tdesc = tdesc;
/* Clone arch-specific process data. */
if (the_low_target.new_fork != NULL)
the_low_target.new_fork (parent_proc, child_proc);
/* Save fork info in the parent thread. */
if (event == PTRACE_EVENT_FORK)
event_lwp->waitstatus.kind = TARGET_WAITKIND_FORKED;
else if (event == PTRACE_EVENT_VFORK)
event_lwp->waitstatus.kind = TARGET_WAITKIND_VFORKED;
event_lwp->waitstatus.value.related_pid = ptid;
/* The status_pending field contains bits denoting the
extended event, so when the pending event is handled,
the handler will look at lwp->waitstatus. */
event_lwp->status_pending_p = 1;
event_lwp->status_pending = wstat;
/* Link the threads until the parent event is passed on to
higher layers. */
event_lwp->fork_relative = child_lwp;
child_lwp->fork_relative = event_lwp;
/* If the parent thread is doing step-over with single-step
breakpoints, the list of single-step breakpoints are cloned
from the parent's. Remove them from the child process.
In case of vfork, we'll reinsert them back once vforked
child is done. */
if (event_lwp->bp_reinsert != 0
&& can_software_single_step ())
{
/* The child process is forked and stopped, so it is safe
to access its memory without stopping all other threads
from other processes. */
delete_single_step_breakpoints (child_thr);
gdb_assert (has_single_step_breakpoints (event_thr));
gdb_assert (!has_single_step_breakpoints (child_thr));
}
/* Report the event. */
return 0;
}
if (debug_threads)
debug_printf ("HEW: Got clone event "
"from LWP %ld, new child is LWP %ld\n",
lwpid_of (event_thr), new_pid);
ptid = ptid_build (pid_of (event_thr), new_pid, 0);
new_lwp = add_lwp (ptid);
/* Either we're going to immediately resume the new thread
or leave it stopped. linux_resume_one_lwp is a nop if it
thinks the thread is currently running, so set this first
before calling linux_resume_one_lwp. */
new_lwp->stopped = 1;
/* If we're suspending all threads, leave this one suspended
too. If the fork/clone parent is stepping over a breakpoint,
all other threads have been suspended already. Leave the
child suspended too. */
if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
|| event_lwp->bp_reinsert != 0)
new_lwp->suspended = 1;
/* Normally we will get the pending SIGSTOP. But in some cases
we might get another signal delivered to the group first.
If we do get another signal, be sure not to lose it. */
if (WSTOPSIG (status) != SIGSTOP)
{
new_lwp->stop_expected = 1;
new_lwp->status_pending_p = 1;
new_lwp->status_pending = status;
}
else if (report_thread_events)
{
new_lwp->waitstatus.kind = TARGET_WAITKIND_THREAD_CREATED;
new_lwp->status_pending_p = 1;
new_lwp->status_pending = status;
}
/* Don't report the event. */
return 1;
}
else if (event == PTRACE_EVENT_VFORK_DONE)
{
event_lwp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
if (event_lwp->bp_reinsert != 0 && can_software_single_step ())
{
reinsert_single_step_breakpoints (event_thr);
gdb_assert (has_single_step_breakpoints (event_thr));
}
/* Report the event. */
return 0;
}
else if (event == PTRACE_EVENT_EXEC && report_exec_events)
{
struct process_info *proc;
VEC (int) *syscalls_to_catch;
ptid_t event_ptid;
pid_t event_pid;
if (debug_threads)
{
debug_printf ("HEW: Got exec event from LWP %ld\n",
lwpid_of (event_thr));
}
/* Get the event ptid. */
event_ptid = ptid_of (event_thr);
event_pid = ptid_get_pid (event_ptid);
/* Save the syscall list from the execing process. */
proc = get_thread_process (event_thr);
syscalls_to_catch = proc->syscalls_to_catch;
proc->syscalls_to_catch = NULL;
/* Delete the execing process and all its threads. */
linux_mourn (proc);
current_thread = NULL;
/* Create a new process/lwp/thread. */
proc = linux_add_process (event_pid, 0);
event_lwp = add_lwp (event_ptid);
event_thr = get_lwp_thread (event_lwp);
gdb_assert (current_thread == event_thr);
linux_arch_setup_thread (event_thr);
/* Set the event status. */
event_lwp->waitstatus.kind = TARGET_WAITKIND_EXECD;
event_lwp->waitstatus.value.execd_pathname
= xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr)));
/* Mark the exec status as pending. */
event_lwp->stopped = 1;
event_lwp->status_pending_p = 1;
event_lwp->status_pending = wstat;
event_thr->last_resume_kind = resume_continue;
event_thr->last_status.kind = TARGET_WAITKIND_IGNORE;
/* Update syscall state in the new lwp, effectively mid-syscall too. */
event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
/* Restore the list to catch. Don't rely on the client, which is free
to avoid sending a new list when the architecture doesn't change.
Also, for ANY_SYSCALL, the architecture doesn't really matter. */
proc->syscalls_to_catch = syscalls_to_catch;
/* Report the event. */
*orig_event_lwp = event_lwp;
return 0;
}
internal_error (__FILE__, __LINE__, _("unknown ptrace event %d"), event);
}
/* Return the PC as read from the regcache of LWP, without any
adjustment. */
static CORE_ADDR
get_pc (struct lwp_info *lwp)
{
struct thread_info *saved_thread;
struct regcache *regcache;
CORE_ADDR pc;
if (the_low_target.get_pc == NULL)
return 0;
saved_thread = current_thread;
current_thread = get_lwp_thread (lwp);
regcache = get_thread_regcache (current_thread, 1);
pc = (*the_low_target.get_pc) (regcache);
if (debug_threads)
debug_printf ("pc is 0x%lx\n", (long) pc);
current_thread = saved_thread;
return pc;
}
/* This function should only be called if LWP got a SYSCALL_SIGTRAP.
Fill *SYSNO with the syscall nr trapped. */
static void
get_syscall_trapinfo (struct lwp_info *lwp, int *sysno)
{
struct thread_info *saved_thread;
struct regcache *regcache;
if (the_low_target.get_syscall_trapinfo == NULL)
{
/* If we cannot get the syscall trapinfo, report an unknown
system call number. */
*sysno = UNKNOWN_SYSCALL;
return;
}
saved_thread = current_thread;
current_thread = get_lwp_thread (lwp);
regcache = get_thread_regcache (current_thread, 1);
(*the_low_target.get_syscall_trapinfo) (regcache, sysno);
if (debug_threads)
debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno);
current_thread = saved_thread;
}
static int check_stopped_by_watchpoint (struct lwp_info *child);
/* Called when the LWP stopped for a signal/trap. If it stopped for a
trap check what caused it (breakpoint, watchpoint, trace, etc.),
and save the result in the LWP's stop_reason field. If it stopped
for a breakpoint, decrement the PC if necessary on the lwp's
architecture. Returns true if we now have the LWP's stop PC. */
static int
save_stop_reason (struct lwp_info *lwp)
{
CORE_ADDR pc;
CORE_ADDR sw_breakpoint_pc;
struct thread_info *saved_thread;
#if USE_SIGTRAP_SIGINFO
siginfo_t siginfo;
#endif
if (the_low_target.get_pc == NULL)
return 0;
pc = get_pc (lwp);
sw_breakpoint_pc = pc - the_low_target.decr_pc_after_break;
/* breakpoint_at reads from the current thread. */
saved_thread = current_thread;
current_thread = get_lwp_thread (lwp);
#if USE_SIGTRAP_SIGINFO
if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
(PTRACE_TYPE_ARG3) 0, &siginfo) == 0)
{
if (siginfo.si_signo == SIGTRAP)
{
if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
&& GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
{
/* The si_code is ambiguous on this arch -- check debug
registers. */
if (!check_stopped_by_watchpoint (lwp))
lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
}
else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
{
/* If we determine the LWP stopped for a SW breakpoint,
trust it. Particularly don't check watchpoint
registers, because at least on s390, we'd find
stopped-by-watchpoint as long as there's a watchpoint
set. */
lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
}
else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
{
/* This can indicate either a hardware breakpoint or
hardware watchpoint. Check debug registers. */
if (!check_stopped_by_watchpoint (lwp))
lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
}
else if (siginfo.si_code == TRAP_TRACE)
{
/* We may have single stepped an instruction that
triggered a watchpoint. In that case, on some
architectures (such as x86), instead of TRAP_HWBKPT,
si_code indicates TRAP_TRACE, and we need to check
the debug registers separately. */
if (!check_stopped_by_watchpoint (lwp))
lwp->stop_reason = TARGET_STOPPED_BY_SINGLE_STEP;
}
}
}
#else
/* We may have just stepped a breakpoint instruction. E.g., in
non-stop mode, GDB first tells the thread A to step a range, and
then the user inserts a breakpoint inside the range. In that
case we need to report the breakpoint PC. */
if ((!lwp->stepping || lwp->stop_pc == sw_breakpoint_pc)
&& (*the_low_target.breakpoint_at) (sw_breakpoint_pc))
lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
if (hardware_breakpoint_inserted_here (pc))
lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
if (lwp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
check_stopped_by_watchpoint (lwp);
#endif
if (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
{
if (debug_threads)
{
struct thread_info *thr = get_lwp_thread (lwp);
debug_printf ("CSBB: %s stopped by software breakpoint\n",
target_pid_to_str (ptid_of (thr)));
}
/* Back up the PC if necessary. */
if (pc != sw_breakpoint_pc)
{
struct regcache *regcache
= get_thread_regcache (current_thread, 1);
(*the_low_target.set_pc) (regcache, sw_breakpoint_pc);
}
/* Update this so we record the correct stop PC below. */
pc = sw_breakpoint_pc;
}
else if (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
{
if (debug_threads)
{
struct thread_info *thr = get_lwp_thread (lwp);
debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
target_pid_to_str (ptid_of (thr)));
}
}
else if (lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
{
if (debug_threads)
{
struct thread_info *thr = get_lwp_thread (lwp);
debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
target_pid_to_str (ptid_of (thr)));
}
}
else if (lwp->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
{
if (debug_threads)
{
struct thread_info *thr = get_lwp_thread (lwp);
debug_printf ("CSBB: %s stopped by trace\n",
target_pid_to_str (ptid_of (thr)));
}
}
lwp->stop_pc = pc;
current_thread = saved_thread;
return 1;
}
static struct lwp_info *
add_lwp (ptid_t ptid)
{
struct lwp_info *lwp;
lwp = XCNEW (struct lwp_info);
lwp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
if (the_low_target.new_thread != NULL)
the_low_target.new_thread (lwp);
lwp->thread = add_thread (ptid, lwp);
return lwp;
}
/* Start an inferior process and returns its pid.
ALLARGS is a vector of program-name and args. */
static int
linux_create_inferior (char *program, char **allargs)
{
struct lwp_info *new_lwp;
int pid;
ptid_t ptid;
struct cleanup *restore_personality
= maybe_disable_address_space_randomization (disable_randomization);
#if defined(__UCLIBC__) && defined(HAS_NOMMU)
pid = vfork ();
#else
pid = fork ();
#endif
if (pid < 0)
perror_with_name ("fork");
if (pid == 0)
{
close_most_fds ();
ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
setpgid (0, 0);
/* If gdbserver is connected to gdb via stdio, redirect the inferior's
stdout to stderr so that inferior i/o doesn't corrupt the connection.
Also, redirect stdin to /dev/null. */
if (remote_connection_is_stdio ())
{
close (0);
open ("/dev/null", O_RDONLY);
dup2 (2, 1);
if (write (2, "stdin/stdout redirected\n",
sizeof ("stdin/stdout redirected\n") - 1) < 0)
{
/* Errors ignored. */;
}
}
restore_original_signals_state ();
execv (program, allargs);
if (errno == ENOENT)
execvp (program, allargs);
fprintf (stderr, "Cannot exec %s: %s.\n", program,
strerror (errno));
fflush (stderr);
_exit (0177);