forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux-tdep.c
3201 lines (2658 loc) · 98 KB
/
linux-tdep.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
/* Target-dependent code for GNU/Linux, architecture independent.
Copyright (C) 2009-2024 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 "exceptions.h"
#include "gdbtypes.h"
#include "linux-tdep.h"
#include "auxv.h"
#include "target.h"
#include "gdbthread.h"
#include "gdbcore.h"
#include "regcache.h"
#include "regset.h"
#include "elf/common.h"
#include "elf-bfd.h"
#include "inferior.h"
#include "cli/cli-utils.h"
#include "arch-utils.h"
#include "gdbsupport/gdb_obstack.h"
#include "observable.h"
#include "objfiles.h"
#include "infcall.h"
#include "cli/cli-cmds.h"
#include "gdbsupport/gdb_regex.h"
#include "gdbsupport/enum-flags.h"
#include <optional>
#include "gcore.h"
#include "gcore-elf.h"
#include "solib-svr4.h"
#include "memtag.h"
#include "cli/cli-style.h"
#include "gdbsupport/unordered_map.h"
#include <ctype.h>
/* This enum represents the values that the user can choose when
informing the Linux kernel about which memory mappings will be
dumped in a corefile. They are described in the file
Documentation/filesystems/proc.txt, inside the Linux kernel
tree. */
enum filter_flag
{
COREFILTER_ANON_PRIVATE = 1 << 0,
COREFILTER_ANON_SHARED = 1 << 1,
COREFILTER_MAPPED_PRIVATE = 1 << 2,
COREFILTER_MAPPED_SHARED = 1 << 3,
COREFILTER_ELF_HEADERS = 1 << 4,
COREFILTER_HUGETLB_PRIVATE = 1 << 5,
COREFILTER_HUGETLB_SHARED = 1 << 6,
};
DEF_ENUM_FLAGS_TYPE (enum filter_flag, filter_flags);
/* This struct is used to map flags found in the "VmFlags:" field (in
the /proc/<PID>/smaps file). */
struct smaps_vmflags
{
/* Zero if this structure has not been initialized yet. It
probably means that the Linux kernel being used does not emit
the "VmFlags:" field on "/proc/PID/smaps". */
unsigned int initialized_p : 1;
/* Memory mapped I/O area (VM_IO, "io"). */
unsigned int io_page : 1;
/* Area uses huge TLB pages (VM_HUGETLB, "ht"). */
unsigned int uses_huge_tlb : 1;
/* Do not include this memory region on the coredump (VM_DONTDUMP, "dd"). */
unsigned int exclude_coredump : 1;
/* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
unsigned int shared_mapping : 1;
/* Memory map has memory tagging enabled. */
unsigned int memory_tagging : 1;
};
/* Data structure that holds the information contained in the
/proc/<pid>/smaps file. */
struct smaps_data
{
ULONGEST start_address;
ULONGEST end_address;
std::string filename;
struct smaps_vmflags vmflags;
bool read;
bool write;
bool exec;
bool priv;
bool has_anonymous;
bool mapping_anon_p;
bool mapping_file_p;
ULONGEST inode;
ULONGEST offset;
};
/* Whether to take the /proc/PID/coredump_filter into account when
generating a corefile. */
static bool use_coredump_filter = true;
/* Whether the value of smaps_vmflags->exclude_coredump should be
ignored, including mappings marked with the VM_DONTDUMP flag in
the dump. */
static bool dump_excluded_mappings = false;
/* This enum represents the signals' numbers on a generic architecture
running the Linux kernel. The definition of "generic" comes from
the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
tree, which is the "de facto" implementation of signal numbers to
be used by new architecture ports.
For those architectures which have differences between the generic
standard (e.g., Alpha), we define the different signals (and *only*
those) in the specific target-dependent file (e.g.,
alpha-linux-tdep.c, for Alpha). Please refer to the architecture's
tdep file for more information.
ARM deserves a special mention here. On the file
<arch/arm/include/uapi/asm/signal.h>, it defines only one different
(and ARM-only) signal, which is SIGSWI, with the same number as
SIGRTMIN. This signal is used only for a very specific target,
called ArthurOS (from RISCOS). Therefore, we do not handle it on
the ARM-tdep file, and we can safely use the generic signal handler
here for ARM targets.
As stated above, this enum is derived from
<include/uapi/asm-generic/signal.h>, from the Linux kernel
tree. */
enum
{
LINUX_SIGHUP = 1,
LINUX_SIGINT = 2,
LINUX_SIGQUIT = 3,
LINUX_SIGILL = 4,
LINUX_SIGTRAP = 5,
LINUX_SIGABRT = 6,
LINUX_SIGIOT = 6,
LINUX_SIGBUS = 7,
LINUX_SIGFPE = 8,
LINUX_SIGKILL = 9,
LINUX_SIGUSR1 = 10,
LINUX_SIGSEGV = 11,
LINUX_SIGUSR2 = 12,
LINUX_SIGPIPE = 13,
LINUX_SIGALRM = 14,
LINUX_SIGTERM = 15,
LINUX_SIGSTKFLT = 16,
LINUX_SIGCHLD = 17,
LINUX_SIGCONT = 18,
LINUX_SIGSTOP = 19,
LINUX_SIGTSTP = 20,
LINUX_SIGTTIN = 21,
LINUX_SIGTTOU = 22,
LINUX_SIGURG = 23,
LINUX_SIGXCPU = 24,
LINUX_SIGXFSZ = 25,
LINUX_SIGVTALRM = 26,
LINUX_SIGPROF = 27,
LINUX_SIGWINCH = 28,
LINUX_SIGIO = 29,
LINUX_SIGPOLL = LINUX_SIGIO,
LINUX_SIGPWR = 30,
LINUX_SIGSYS = 31,
LINUX_SIGUNUSED = 31,
LINUX_SIGRTMIN = 32,
LINUX_SIGRTMAX = 64,
};
struct linux_gdbarch_data
{
struct type *siginfo_type = nullptr;
int num_disp_step_buffers = 0;
};
static const registry<gdbarch>::key<linux_gdbarch_data>
linux_gdbarch_data_handle;
static struct linux_gdbarch_data *
get_linux_gdbarch_data (struct gdbarch *gdbarch)
{
struct linux_gdbarch_data *result = linux_gdbarch_data_handle.get (gdbarch);
if (result == nullptr)
result = linux_gdbarch_data_handle.emplace (gdbarch);
return result;
}
/* Linux-specific cached data. This is used by GDB for caching
purposes for each inferior. This helps reduce the overhead of
transferring data from a remote target to the local host. */
struct linux_info
{
/* Cache of the inferior's vsyscall/vDSO mapping range. Only valid
if VSYSCALL_RANGE_P is positive. This is cached because getting
at this info requires an auxv lookup (which is itself cached),
and looking through the inferior's mappings (which change
throughout execution and therefore cannot be cached). */
struct mem_range vsyscall_range {};
/* Zero if we haven't tried looking up the vsyscall's range before
yet. Positive if we tried looking it up, and found it. Negative
if we tried looking it up but failed. */
int vsyscall_range_p = 0;
/* Inferior's displaced step buffers. */
std::optional<displaced_step_buffers> disp_step_bufs;
};
/* Per-inferior data key. */
static const registry<inferior>::key<linux_info> linux_inferior_data;
/* Frees whatever allocated space there is to be freed and sets INF's
linux cache data pointer to NULL. */
static void
invalidate_linux_cache_inf (struct inferior *inf)
{
linux_inferior_data.clear (inf);
}
/* inferior_execd observer. */
static void
linux_inferior_execd (inferior *exec_inf, inferior *follow_inf)
{
invalidate_linux_cache_inf (follow_inf);
}
/* Fetch the linux cache info for INF. This function always returns a
valid INFO pointer. */
static struct linux_info *
get_linux_inferior_data (inferior *inf)
{
linux_info *info = linux_inferior_data.get (inf);
if (info == nullptr)
info = linux_inferior_data.emplace (inf);
return info;
}
/* See linux-tdep.h. */
struct type *
linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
linux_siginfo_extra_fields extra_fields)
{
struct linux_gdbarch_data *linux_gdbarch_data;
struct type *int_type, *uint_type, *long_type, *void_ptr_type, *short_type;
struct type *uid_type, *pid_type;
struct type *sigval_type, *clock_type;
struct type *siginfo_type, *sifields_type;
struct type *type;
linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
if (linux_gdbarch_data->siginfo_type != NULL)
return linux_gdbarch_data->siginfo_type;
type_allocator alloc (gdbarch);
int_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
0, "int");
uint_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
1, "unsigned int");
long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
0, "long");
short_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
0, "short");
void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
/* sival_t */
sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
sigval_type->set_name (xstrdup ("sigval_t"));
append_composite_type_field (sigval_type, "sival_int", int_type);
append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
/* __pid_t */
pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
int_type->length () * TARGET_CHAR_BIT,
"__pid_t");
pid_type->set_target_type (int_type);
pid_type->set_target_is_stub (true);
/* __uid_t */
uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
uint_type->length () * TARGET_CHAR_BIT,
"__uid_t");
uid_type->set_target_type (uint_type);
uid_type->set_target_is_stub (true);
/* __clock_t */
clock_type = alloc.new_type (TYPE_CODE_TYPEDEF,
long_type->length () * TARGET_CHAR_BIT,
"__clock_t");
clock_type->set_target_type (long_type);
clock_type->set_target_is_stub (true);
/* _sifields */
sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
{
const int si_max_size = 128;
int si_pad_size;
int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
/* _pad */
if (gdbarch_ptr_bit (gdbarch) == 64)
si_pad_size = (si_max_size / size_of_int) - 4;
else
si_pad_size = (si_max_size / size_of_int) - 3;
append_composite_type_field (sifields_type, "_pad",
init_vector_type (int_type, si_pad_size));
}
/* _kill */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "si_pid", pid_type);
append_composite_type_field (type, "si_uid", uid_type);
append_composite_type_field (sifields_type, "_kill", type);
/* _timer */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "si_tid", int_type);
append_composite_type_field (type, "si_overrun", int_type);
append_composite_type_field (type, "si_sigval", sigval_type);
append_composite_type_field (sifields_type, "_timer", type);
/* _rt */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "si_pid", pid_type);
append_composite_type_field (type, "si_uid", uid_type);
append_composite_type_field (type, "si_sigval", sigval_type);
append_composite_type_field (sifields_type, "_rt", type);
/* _sigchld */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "si_pid", pid_type);
append_composite_type_field (type, "si_uid", uid_type);
append_composite_type_field (type, "si_status", int_type);
append_composite_type_field (type, "si_utime", clock_type);
append_composite_type_field (type, "si_stime", clock_type);
append_composite_type_field (sifields_type, "_sigchld", type);
/* _sigfault */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "si_addr", void_ptr_type);
/* Additional bound fields for _sigfault in case they were requested. */
if ((extra_fields & LINUX_SIGINFO_FIELD_ADDR_BND) != 0)
{
struct type *sigfault_bnd_fields;
append_composite_type_field (type, "_addr_lsb", short_type);
sigfault_bnd_fields = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (sigfault_bnd_fields, "_lower", void_ptr_type);
append_composite_type_field (sigfault_bnd_fields, "_upper", void_ptr_type);
append_composite_type_field (type, "_addr_bnd", sigfault_bnd_fields);
}
append_composite_type_field (sifields_type, "_sigfault", type);
/* _sigpoll */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "si_band", long_type);
append_composite_type_field (type, "si_fd", int_type);
append_composite_type_field (sifields_type, "_sigpoll", type);
/* _sigsys */
type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
append_composite_type_field (type, "_call_addr", void_ptr_type);
append_composite_type_field (type, "_syscall", int_type);
append_composite_type_field (type, "_arch", uint_type);
append_composite_type_field (sifields_type, "_sigsys", type);
/* struct siginfo */
siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
siginfo_type->set_name (xstrdup ("siginfo"));
append_composite_type_field (siginfo_type, "si_signo", int_type);
append_composite_type_field (siginfo_type, "si_errno", int_type);
append_composite_type_field (siginfo_type, "si_code", int_type);
append_composite_type_field_aligned (siginfo_type,
"_sifields", sifields_type,
long_type->length ());
linux_gdbarch_data->siginfo_type = siginfo_type;
return siginfo_type;
}
/* This function is suitable for architectures that don't
extend/override the standard siginfo structure. */
static struct type *
linux_get_siginfo_type (struct gdbarch *gdbarch)
{
return linux_get_siginfo_type_with_fields (gdbarch, 0);
}
/* Return true if the target is running on uClinux instead of normal
Linux kernel. */
int
linux_is_uclinux (void)
{
CORE_ADDR dummy;
return (target_auxv_search (AT_NULL, &dummy) > 0
&& target_auxv_search (AT_PAGESZ, &dummy) == 0);
}
static int
linux_has_shared_address_space (struct gdbarch *gdbarch)
{
return linux_is_uclinux ();
}
/* This is how we want PTIDs from core files to be printed. */
static std::string
linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
{
if (ptid.lwp () != 0)
return string_printf ("LWP %ld", ptid.lwp ());
return normal_pid_to_str (ptid);
}
/* Data from one mapping from /proc/PID/maps. */
struct mapping
{
ULONGEST addr;
ULONGEST endaddr;
std::string permissions;
ULONGEST offset;
std::string_view device;
ULONGEST inode;
/* This field is guaranteed to be NULL-terminated, hence it is not a
std::string_view. */
const char *filename;
};
/* Service function for corefiles and info proc. */
static mapping
read_mapping (const char *line)
{
struct mapping mapping;
const char *p = line;
mapping.addr = strtoulst (p, &p, 16);
if (*p == '-')
p++;
mapping.endaddr = strtoulst (p, &p, 16);
p = skip_spaces (p);
const char *permissions_start = p;
while (*p && !isspace (*p))
p++;
mapping.permissions = std::string (permissions_start,
(size_t) (p - permissions_start));
mapping.offset = strtoulst (p, &p, 16);
p = skip_spaces (p);
const char *device_start = p;
while (*p && !isspace (*p))
p++;
mapping.device = {device_start, (size_t) (p - device_start)};
mapping.inode = strtoulst (p, &p, 10);
p = skip_spaces (p);
mapping.filename = p;
return mapping;
}
/* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
This function was based on the documentation found on
<Documentation/filesystems/proc.txt>, on the Linux kernel.
Linux kernels before commit
834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
field on smaps. */
static void
decode_vmflags (char *p, struct smaps_vmflags *v)
{
char *saveptr = NULL;
const char *s;
v->initialized_p = 1;
p = skip_to_space (p);
p = skip_spaces (p);
for (s = strtok_r (p, " ", &saveptr);
s != NULL;
s = strtok_r (NULL, " ", &saveptr))
{
if (strcmp (s, "io") == 0)
v->io_page = 1;
else if (strcmp (s, "ht") == 0)
v->uses_huge_tlb = 1;
else if (strcmp (s, "dd") == 0)
v->exclude_coredump = 1;
else if (strcmp (s, "sh") == 0)
v->shared_mapping = 1;
else if (strcmp (s, "mt") == 0)
v->memory_tagging = 1;
}
}
/* Regexes used by mapping_is_anonymous_p. Put in a structure because
they're initialized lazily. */
struct mapping_regexes
{
/* Matches "/dev/zero" filenames (with or without the "(deleted)"
string in the end). We know for sure, based on the Linux kernel
code, that memory mappings whose associated filename is
"/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
compiled_regex dev_zero
{"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB,
_("Could not compile regex to match /dev/zero filename")};
/* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
string in the end). These filenames refer to shared memory
(shmem), and memory mappings associated with them are
MAP_ANONYMOUS as well. */
compiled_regex shmem_file
{"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB,
_("Could not compile regex to match shmem filenames")};
/* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
0' code, which is responsible to decide if it is dealing with a
'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
FILE_DELETED matches, it does not necessarily mean that we are
dealing with an anonymous shared mapping. However, there is no
easy way to detect this currently, so this is the best
approximation we have.
As a result, GDB will dump readonly pages of deleted executables
when using the default value of coredump_filter (0x33), while the
Linux kernel will not dump those pages. But we can live with
that. */
compiled_regex file_deleted
{" (deleted)$", REG_NOSUB,
_("Could not compile regex to match '<file> (deleted)'")};
};
/* Return 1 if the memory mapping is anonymous, 0 otherwise.
FILENAME is the name of the file present in the first line of the
memory mapping, in the "/proc/PID/smaps" output. For example, if
the first line is:
7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
Then FILENAME will be "/path/to/file". */
static int
mapping_is_anonymous_p (const char *filename)
{
static std::optional<mapping_regexes> regexes;
static int init_regex_p = 0;
if (!init_regex_p)
{
/* Let's be pessimistic and assume there will be an error while
compiling the regex'es. */
init_regex_p = -1;
regexes.emplace ();
/* If we reached this point, then everything succeeded. */
init_regex_p = 1;
}
if (init_regex_p == -1)
{
const char deleted[] = " (deleted)";
size_t del_len = sizeof (deleted) - 1;
size_t filename_len = strlen (filename);
/* There was an error while compiling the regex'es above. In
order to try to give some reliable information to the caller,
we just try to find the string " (deleted)" in the filename.
If we managed to find it, then we assume the mapping is
anonymous. */
return (filename_len >= del_len
&& strcmp (filename + filename_len - del_len, deleted) == 0);
}
if (*filename == '\0'
|| regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
|| regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
|| regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
return 1;
return 0;
}
/* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
be dumped, or greater than 0 if it should.
In a nutshell, this is the logic that we follow in order to decide
if a mapping should be dumped or not.
- If the mapping is associated to a file whose name ends with
" (deleted)", or if the file is "/dev/zero", or if it is
"/SYSV%08x" (shared memory), or if there is no file associated
with it, or if the AnonHugePages: or the Anonymous: fields in the
/proc/PID/smaps have contents, then GDB considers this mapping to
be anonymous. Otherwise, GDB considers this mapping to be a
file-backed mapping (because there will be a file associated with
it).
It is worth mentioning that, from all those checks described
above, the most fragile is the one to see if the file name ends
with " (deleted)". This does not necessarily mean that the
mapping is anonymous, because the deleted file associated with
the mapping may have been a hard link to another file, for
example. The Linux kernel checks to see if "i_nlink == 0", but
GDB cannot easily (and normally) do this check (iff running as
root, it could find the mapping in /proc/PID/map_files/ and
determine whether there still are other hard links to the
inode/file). Therefore, we made a compromise here, and we assume
that if the file name ends with " (deleted)", then the mapping is
indeed anonymous. FWIW, this is something the Linux kernel could
do better: expose this information in a more direct way.
- If we see the flag "sh" in the "VmFlags:" field (in
/proc/PID/smaps), then certainly the memory mapping is shared
(VM_SHARED). If we have access to the VmFlags, and we don't see
the "sh" there, then certainly the mapping is private. However,
Linux kernels before commit
834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
"VmFlags:" field; in that case, we use another heuristic: if we
see 'p' in the permission flags, then we assume that the mapping
is private, even though the presence of the 's' flag there would
mean VM_MAYSHARE, which means the mapping could still be private.
This should work OK enough, however.
- Even if, at the end, we decided that we should not dump the
mapping, we still have to check if it is something like an ELF
header (of a DSO or an executable, for example). If it is, and
if the user is interested in dump it, then we should dump it. */
static int
dump_mapping_p (filter_flags filterflags, const struct smaps_vmflags *v,
int maybe_private_p, int mapping_anon_p, int mapping_file_p,
const char *filename, ULONGEST addr, ULONGEST offset)
{
/* Initially, we trust in what we received from our caller. This
value may not be very precise (i.e., it was probably gathered
from the permission line in the /proc/PID/smaps list, which
actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
what we have until we take a look at the "VmFlags:" field
(assuming that the version of the Linux kernel being used
supports it, of course). */
int private_p = maybe_private_p;
int dump_p;
/* We always dump vDSO and vsyscall mappings, because it's likely that
there'll be no file to read the contents from at core load time.
The kernel does the same. */
if (strcmp ("[vdso]", filename) == 0
|| strcmp ("[vsyscall]", filename) == 0)
return 1;
if (v->initialized_p)
{
/* We never dump I/O mappings. */
if (v->io_page)
return 0;
/* Check if we should exclude this mapping. */
if (!dump_excluded_mappings && v->exclude_coredump)
return 0;
/* Update our notion of whether this mapping is shared or
private based on a trustworthy value. */
private_p = !v->shared_mapping;
/* HugeTLB checking. */
if (v->uses_huge_tlb)
{
if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
|| (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
return 1;
return 0;
}
}
if (private_p)
{
if (mapping_anon_p && mapping_file_p)
{
/* This is a special situation. It can happen when we see a
mapping that is file-backed, but that contains anonymous
pages. */
dump_p = ((filterflags & COREFILTER_ANON_PRIVATE) != 0
|| (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
}
else if (mapping_anon_p)
dump_p = (filterflags & COREFILTER_ANON_PRIVATE) != 0;
else
dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
}
else
{
if (mapping_anon_p && mapping_file_p)
{
/* This is a special situation. It can happen when we see a
mapping that is file-backed, but that contains anonymous
pages. */
dump_p = ((filterflags & COREFILTER_ANON_SHARED) != 0
|| (filterflags & COREFILTER_MAPPED_SHARED) != 0);
}
else if (mapping_anon_p)
dump_p = (filterflags & COREFILTER_ANON_SHARED) != 0;
else
dump_p = (filterflags & COREFILTER_MAPPED_SHARED) != 0;
}
/* Even if we decided that we shouldn't dump this mapping, we still
have to check whether (a) the user wants us to dump mappings
containing an ELF header, and (b) the mapping in question
contains an ELF header. If (a) and (b) are true, then we should
dump this mapping.
A mapping contains an ELF header if it is a private mapping, its
offset is zero, and its first word is ELFMAG. */
if (!dump_p && private_p && offset == 0
&& (filterflags & COREFILTER_ELF_HEADERS) != 0)
{
/* Useful define specifying the size of the ELF magical
header. */
#ifndef SELFMAG
#define SELFMAG 4
#endif
/* Let's check if we have an ELF header. */
gdb_byte h[SELFMAG];
if (target_read_memory (addr, h, SELFMAG) == 0)
{
/* The EI_MAG* and ELFMAG* constants come from
<elf/common.h>. */
if (h[EI_MAG0] == ELFMAG0 && h[EI_MAG1] == ELFMAG1
&& h[EI_MAG2] == ELFMAG2 && h[EI_MAG3] == ELFMAG3)
{
/* This mapping contains an ELF header, so we
should dump it. */
dump_p = 1;
}
}
}
return dump_p;
}
/* As above, but return true only when we should dump the NT_FILE
entry. */
static int
dump_note_entry_p (filter_flags filterflags, const struct smaps_vmflags *v,
int maybe_private_p, int mapping_anon_p, int mapping_file_p,
const char *filename, ULONGEST addr, ULONGEST offset)
{
/* vDSO and vsyscall mappings will end up in the core file. Don't
put them in the NT_FILE note. */
if (strcmp ("[vdso]", filename) == 0
|| strcmp ("[vsyscall]", filename) == 0)
return 0;
/* Otherwise, any other file-based mapping should be placed in the
note. */
return 1;
}
/* Implement the "info proc" command. */
static void
linux_info_proc (struct gdbarch *gdbarch, const char *args,
enum info_proc_what what)
{
/* A long is used for pid instead of an int to avoid a loss of precision
compiler warning from the output of strtoul. */
long pid;
int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
int status_f = (what == IP_STATUS || what == IP_ALL);
int stat_f = (what == IP_STAT || what == IP_ALL);
char filename[100];
fileio_error target_errno;
if (args && isdigit (args[0]))
{
char *tem;
pid = strtoul (args, &tem, 10);
args = tem;
}
else
{
if (!target_has_execution ())
error (_("No current process: you must name one."));
if (current_inferior ()->fake_pid_p)
error (_("Can't determine the current process's PID: you must name one."));
pid = current_inferior ()->pid;
}
args = skip_spaces (args);
if (args && args[0])
error (_("Too many parameters: %s"), args);
gdb_printf (_("process %ld\n"), pid);
if (cmdline_f)
{
xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
gdb_byte *buffer;
LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer);
if (len > 0)
{
gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
ssize_t pos;
for (pos = 0; pos < len - 1; pos++)
{
if (buffer[pos] == '\0')
buffer[pos] = ' ';
}
buffer[len - 1] = '\0';
gdb_printf ("cmdline = '%s'\n", buffer);
}
else
warning (_("unable to open /proc file '%s'"), filename);
}
if (cwd_f)
{
xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
std::optional<std::string> contents
= target_fileio_readlink (NULL, filename, &target_errno);
if (contents.has_value ())
gdb_printf ("cwd = '%s'\n", contents->c_str ());
else
warning (_("unable to read link '%s'"), filename);
}
if (exe_f)
{
xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
std::optional<std::string> contents
= target_fileio_readlink (NULL, filename, &target_errno);
if (contents.has_value ())
gdb_printf ("exe = '%s'\n", contents->c_str ());
else
warning (_("unable to read link '%s'"), filename);
}
if (mappings_f)
{
xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
gdb::unique_xmalloc_ptr<char> map
= target_fileio_read_stralloc (NULL, filename);
if (map != NULL)
{
gdb_printf (_("Mapped address spaces:\n\n"));
ui_out_emit_table emitter (current_uiout, 6, -1, "ProcMappings");
int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
current_uiout->table_header (width, ui_left, "start", "Start Addr");
current_uiout->table_header (width, ui_left, "end", "End Addr");
current_uiout->table_header (width, ui_left, "size", "Size");
current_uiout->table_header (width, ui_left, "offset", "Offset");
current_uiout->table_header (5, ui_left, "perms", "Perms");
current_uiout->table_header (0, ui_left, "objfile", "File");
current_uiout->table_body ();
char *saveptr;
for (const char *line = strtok_r (map.get (), "\n", &saveptr);
line != nullptr;
line = strtok_r (nullptr, "\n", &saveptr))
{
struct mapping m = read_mapping (line);
ui_out_emit_tuple tuple_emitter (current_uiout, nullptr);
current_uiout->field_core_addr ("start", gdbarch, m.addr);
current_uiout->field_core_addr ("end", gdbarch, m.endaddr);
/* These next two aren't really addresses and so
shouldn't be styled as such. */
current_uiout->field_string ("size",
paddress (gdbarch,
m.endaddr - m.addr));
current_uiout->field_string ("offset",
paddress (gdbarch, m.offset));
current_uiout->field_string ("perms", m.permissions);
current_uiout->field_string ("objfile", m.filename,
file_name_style.style ());
current_uiout->text ("\n");
}
}
else
warning (_("unable to open /proc file '%s'"), filename);
}
if (status_f)
{
xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
gdb::unique_xmalloc_ptr<char> status
= target_fileio_read_stralloc (NULL, filename);
if (status)
gdb_puts (status.get ());
else
warning (_("unable to open /proc file '%s'"), filename);
}
if (stat_f)
{
xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
gdb::unique_xmalloc_ptr<char> statstr
= target_fileio_read_stralloc (NULL, filename);
if (statstr)
{
const char *p = statstr.get ();
gdb_printf (_("Process: %s\n"),
pulongest (strtoulst (p, &p, 10)));
p = skip_spaces (p);
if (*p == '(')
{
/* ps command also relies on no trailing fields
ever contain ')'. */
const char *ep = strrchr (p, ')');
if (ep != NULL)
{
gdb_printf ("Exec file: %.*s\n",
(int) (ep - p - 1), p + 1);
p = ep + 1;
}
}
p = skip_spaces (p);
if (*p)
gdb_printf (_("State: %c\n"), *p++);
if (*p)
gdb_printf (_("Parent process: %s\n"),
pulongest (strtoulst (p, &p, 10)));
if (*p)
gdb_printf (_("Process group: %s\n"),
pulongest (strtoulst (p, &p, 10)));
if (*p)
gdb_printf (_("Session id: %s\n"),
pulongest (strtoulst (p, &p, 10)));
if (*p)
gdb_printf (_("TTY: %s\n"),
pulongest (strtoulst (p, &p, 10)));
if (*p)
gdb_printf (_("TTY owner process group: %s\n"),
pulongest (strtoulst (p, &p, 10)));
if (*p)
gdb_printf (_("Flags: %s\n"),
hex_string (strtoulst (p, &p, 10)));
if (*p)
gdb_printf (_("Minor faults (no memory page): %s\n"),