-
Notifications
You must be signed in to change notification settings - Fork 11
/
pargs.c
881 lines (836 loc) · 24.1 KB
/
pargs.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
// pargs - print process arguments and other vectors
//
// inspired by Solaris' `pargs` command
//
// 2017, Georg Sauthoff <[email protected]>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <ctype.h> // isdigit()
#include <errno.h>
#include <elf.h> // for AT_* defines
#include <fcntl.h> // open()
#include <inttypes.h>
#include <limits.h> // LONG_MAX
#include <sys/mman.h> // mmap(), munmap()
#include <sys/ptrace.h> // ptrace()
#include <sys/stat.h> // stat(), open()
#include <sys/types.h> // waitid(), open()
#include <sys/wait.h> // waitid()
#include <stdbool.h>
#include <stdio.h> // perror()
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // close()
#include <stdarg.h> // va_start(), va_end()
static bool debug_enabled;
static void debug(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
static void debug(const char *fmt, ...)
{
if (!debug_enabled)
return;
int r;
va_list ap;
va_start(ap, fmt);
r = vfprintf(stderr, fmt, ap);
(void)r;
fputc('\n', stderr);
va_end(ap);
}
struct Args {
bool print_argv;
bool print_auxv;
bool print_cmdline;
bool print_envp;
bool verbose;
bool stop_process;
};
typedef struct Args Args;
static bool is_all_num(const char *s)
{
for (; *s; ++s) {
if (!isdigit(*s))
return false;
}
return true;
}
static void print_help(FILE *o, const char *argv0)
{
fprintf(o, "Usage: %s [OPTION]... {PID|CORE}\n"
"Display arguments and other vectors of a process.\n\n"
" -a print process arguments (argv, argument vector)\n"
" -d enable debug output (when reading core files)\n"
" -e print environment variables (envp, environment vector)\n"
" -h,--help display this help text and exit\n"
" -l print command line\n"
" -s attach/detach process to stop process during -x reads\n"
" (for Linux < 3.2)\n"
" -x print auxillary vector\n"
" -v verbose mode\n"
"\n\n"
"2017, Georg Sauthoff <[email protected]>, GPLv3+\n"
, argv0);
}
// return_value != 0: caller should exit() with (return_value - 1)
static int parse_args(int argc, char **argv, Args *args)
{
*args = (Args){0};
bool more_opts = true;
for (int i = 1; i < argc; ++i) {
if (more_opts && *argv[i] == '-') {
const char *a = argv[i] + 1;
if (*a == '-') {
if (!a[1]) {
more_opts = false;
continue;
} else if (!strcmp(a + 1, "help")) {
print_help(stdout, *argv);
return 1;
} else {
fprintf(stderr, "Unknown argument: %s\n", argv[i]);
return 3;
}
}
for (; *a; ++a) {
switch (*a) {
case 'a': args->print_argv = true; break;
case 'd': debug_enabled = true; break;
case 'e': args->print_envp = true; break;
case 'h': print_help(stdout, *argv);
return 1;
case 'l': args->print_cmdline = true; break;
case 's': args->stop_process = true; break;
case 'x': args->print_auxv = true; break;
case 'v': args->verbose = true; break;
default: fprintf(stderr, "Unknown argument: -%c\n", *a);
return 3;
}
}
} else if (is_all_num(argv[i])) {
if (strlen(argv[i]) > 20) {
fputs("PID is too long.\n", stderr);
return 3;
}
continue; // PID
} else {
continue; // core file
}
}
if (args->print_cmdline && (args->print_auxv || args->print_envp)) {
fputs("-l is incompatible with -x and -e\n", stderr);
return 3;
}
if (!args->print_argv && !args->print_auxv && !args->print_cmdline
&& !args->print_envp)
args->print_argv = true;
return 0;
}
static int fput_proc_file(FILE *f,
const char *prefix, const char *delim, FILE *o)
{
char *line = 0;
size_t n = 0;
for (size_t i = 0;; ++i) {
errno = 0;
ssize_t r = getdelim(&line, &n, 0, f);
if (r == -1)
break;
if (i)
fputs(delim, o);
if (prefix)
fprintf(o, "%s[%zu]: ", prefix, i);
fputs(line, o);
}
if (errno) {
perror("read error");
free(line);
return -1;
}
free(line);
return 0;
}
static int fput_proc_vector(const char *pid, const char *name,
const char *prefix, const char *delim, FILE *o)
{
char filename[64];
sprintf(filename, "/proc/%s/%s", pid, name);
FILE *f = fopen(filename, "r");
if (!f) {
perror(0);
return -1;
}
int r = fput_proc_file(f, prefix, delim, o);
if (r) {
fclose(f);
return r;
}
r = fclose(f);
if (r) {
perror(0);
return -1;
}
return 0;
}
static int fput_header(const char *pid, FILE *o)
{
fprintf(o, "%s: ", pid);
return fput_proc_vector(pid, "cmdline", 0, " ", o);
}
struct Auxv_Type {
const char *key;
const char *desc;
};
typedef struct Auxv_Type Auxv_Type;
// AT info copied from glibc's /usr/include/elf.h
// cf. glibc-devel-2.25-12.fc26.x86_64
static Auxv_Type auxv_type_map[] = {
/* 0 */ { "AT_NULL" , "End of vector" },
/* 1 */ { "AT_IGNORE" , "Entry should be ignored" },
/* 2 */ { "AT_EXECFD" , "File descriptor of program" },
/* 3 */ { "AT_PHDR" , "Program headers for program" },
/* 4 */ { "AT_PHENT" , "Size of program header entry" },
/* 5 */ { "AT_PHNUM" , "Number of program headers" },
/* 6 */ { "AT_PAGESZ" , "System page size" },
/* 7 */ { "AT_BASE" , "Base address of interpreter" },
/* 8 */ { "AT_FLAGS" , "Flags" },
/* 9 */ { "AT_ENTRY" , "Entry point of program" },
/* 10 */ { "AT_NOTELF" , "Program is not ELF" },
/* 11 */ { "AT_UID" , "Real uid" },
/* 12 */ { "AT_EUID" , "Effective uid" },
/* 13 */ { "AT_GID" , "Real gid" },
/* 14 */ { "AT_EGID" , "Effective gid" },
/* 15 */ { "AT_PLATFORM" , "String identifying platform" },
/* 16 */ { "AT_HWCAP" , "CPU capabilities hints" },
/* 17 */ { "AT_CLKTCK" , "Frequency of times()" },
/* 18 */ { "AT_FPUCW" , "Used FPU control word" },
/* 19 */ { "AT_DCACHEBSIZE" , "Data cache block size" },
/* 20 */ { "AT_ICACHEBSIZE" , "Instruction cache block size" },
/* 21 */ { "AT_UCACHEBSIZE" , "Unified cache block size" },
/* 22 */ { "AT_IGNOREPPC" , "Entry should be ignored" },
/* 23 */ { "AT_SECURE" , "Boolean, was exec setuid-like?" },
/* 24 */ { "AT_BASE_PLATFORM" , "String identifying real platforms" },
/* 25 */ { "AT_RANDOM" , "Address of 16 random bytes" },
/* 26 */ { "AT_HWCAP2" , "More CPU capabilities hints" },
/* 27 */ { "unk_27" , "" },
/* 28 */ { "unk_28" , "" },
/* 29 */ { "unk_29" , "" },
/* 30 */ { "unk_30" , "" },
/* 31 */ { "AT_EXECFN" , "Filename of executable" },
/* 32 */ { "AT_SYSINFO" , "" },
/* 33 */ { "AT_SYSINFO_EHDR" , "" },
/* 34 */ { "AT_L1I_CACHESHAPE" , "" },
/* 35 */ { "AT_L1D_CACHESHAPE" , "" },
/* 36 */ { "AT_L2_CACHESHAPE" , "" },
/* 37 */ { "AT_L3_CACHESHAPE" , "" }
};
// copied from Linux's arch/x86/include/asm/cpufeatures.h
// i.e. Linux kernel 4.14 source
// cf. https://unix.stackexchange.com/questions/43539/what-do-the-flags-in-proc-cpuinfo-mean
// LWN: getauxval() and the auxiliary vector (2012)
// https://lwn.net/Articles/519085/
static Auxv_Type x86_hwcap_map[] = {
/* 0 */ { "fpu" , "Onboard FPU" },
/* 1 */ { "vme" , "Virtual Mode Extensions" },
/* 2 */ { "de" , "Debugging Extensions" },
/* 3 */ { "pse" , "Page Size Extensions" },
/* 4 */ { "tsc" , "Time Stamp Counter" },
/* 5 */ { "msr" , "Model-Specific Registers" },
/* 6 */ { "pae" , "Physical Address Extensions" },
/* 7 */ { "mce" , "Machine Check Exception" },
/* 8 */ { "cx8" , "CMPXCHG8 instruction" },
/* 9 */ { "apic" , "Onboard APIC" },
/* 10 */ { "unk_10" , "" },
/* 11 */ { "sep" , "SYSENTER/SYSEXIT" },
/* 12 */ { "mtrr" , "Memory Type Range Registers" },
/* 13 */ { "pge" , "Page Global Enable" },
/* 14 */ { "mca" , "Machine Check Architecture" },
/* 15 */ { "cmov" , "CMOV instructions" },
/* 16 */ { "pat" , "Page Attribute Table" },
/* 17 */ { "pse36" , "36-bit PSEs" },
/* 18 */ { "pn" , "Processor serial number" },
/* 19 */ { "clflush" , "CLFLUSH instruction" },
/* 20 */ { "unk_20" , "" },
/* 21 */ { "dts" , "'ds' Debug Store" },
/* 22 */ { "acpi" , "ACPI via MSR" },
/* 23 */ { "mmx" , "Multimedia Extensions" },
/* 24 */ { "fxsr" , "FXSAVE/FXRSTOR, CR4.OSFXSR" },
/* 25 */ { "sse" , "'xmm'" },
/* 26 */ { "sse2" , "'xmm2'" },
/* 27 */ { "ss" , "'selfsnoop' CPU self snoop" },
/* 28 */ { "ht" , "Hyper-Threading" },
/* 29 */ { "tm" , "'acc' Automatic clock control" },
/* 30 */ { "ia64" , "IA-64 processor" },
/* 31 */ { "pbe" , "Pending Break Enable" },
};
// It's not sufficient to look at the last 16 bytes of the /proc/$pid/auxv
// file, since they are all zero even for 32 bit processes.
// If we don't have read permissions for /proc/$pid/exe we likely
// don't have them for /proc/$pid/auxv, as well.
static int is_auxv_64_file(FILE *f, bool *result)
{
unsigned char v[5];
size_t r = fread(&v, 1, sizeof v, f);
if (r != sizeof v) {
if (ferror(f))
perror(0);
return -1;
}
static const unsigned char elf64_magic[] = { 0x7f, 0x45, 0x4c, 0x46, 0x02 };
int x = memcmp(v, elf64_magic, sizeof v);
*result = !x;
return 0;
}
static int is_auxv_64(const char *pid, bool *result)
{
char filename[64];
sprintf(filename, "/proc/%s/exe", pid);
FILE *f = fopen(filename, "rb");
if (!f) {
perror(0);
return -1;
}
int r = is_auxv_64_file(f, result);
if (r) {
fclose(f);
return r;
}
r = fclose(f);
if (r) {
perror(0);
return -1;
}
return 0;
}
static void pp_hwcap(uint64_t val, FILE *o)
{
#if defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__) || defined(__x86_64__)
for (size_t i = 0; i < sizeof x86_hwcap_map / sizeof x86_hwcap_map[0]; ++i) {
if (val & ( ((uint64_t)1) << i)) {
if (i)
fputs(" |", o);
fprintf(o, " %s", x86_hwcap_map[i].key);
}
}
#endif
}
// since long is signed and be as small as 32 bit directly using fseek
// is not sufficient, e.g.:
// - even when both pargs and the target process are ILP32 (e.g. x86), we can
// get into trouble when seeking beyond 2^31 - 1 (because long is signed).
static int u64_fseek(FILE *f, uint64_t off)
{
if (sizeof(long) >= 8) {
int r = fseek(f, off, SEEK_SET);
return r;
} else {
uint64_t m = LONG_MAX;
long ml = LONG_MAX;
if (off > m) {
int r = fseek(f, ml, SEEK_SET);
if (r)
return r;
uint64_t l = off - m;
for (;;) {
if (l > m) {
int r = fseek(f, ml, SEEK_CUR);
if (r)
return r;
l -= m;
} else {
int r = fseek(f, l, SEEK_CUR);
return r;
}
}
} else {
int r = fseek(f, off, SEEK_SET);
return r;
}
}
// unreachable
return 0;
}
static int read_mem_str(char **line, size_t *n, FILE *m, uint64_t off)
{
int p = u64_fseek(m, off);
if (p == -1) {
perror("fseek /proc/$pid/mem failed");
return -1;
}
ssize_t r = getdelim(line, n, 0, m);
if (r == -1) {
perror(0);
return -1;
}
return 0;
}
static int read_mem_rand(char **line, size_t *n, FILE *m, uint64_t off)
{
int p = u64_fseek(m, off);
if (p == -1) {
perror("fseek /proc/$pid/mem failed");
return -1;
}
unsigned char v[16];
if (*n < sizeof v * 3 + 1) {
fprintf(stderr, "internal read mem error\n");
return -1;
}
size_t r = fread(v, 1, sizeof v, m);
if (r != sizeof v) {
if (ferror(m))
perror(0);
return -1;
}
char *s = *line;
for (size_t i = 0; i < sizeof v; ++i, s += 3)
sprintf(s, "%.2x ", (unsigned) v[i]);
s[-1] = 0;
return 0;
}
static int pp_aux(uint64_t key, uint64_t val, FILE *o)
{
switch (key) {
case AT_HWCAP: pp_hwcap(val, o); break;
case AT_PAGESZ: fprintf(o, " %" PRIu64 " KiB", (uint64_t)(val/1024)); break;
case AT_CLKTCK: fprintf(o, " %" PRIu64 " Hz", val); break;
case AT_UID:
case AT_EUID:
case AT_GID:
case AT_EGID: fprintf(o, " %" PRIu64, val); break;
case AT_SECURE: fprintf(o, " %s", val ? "true" : "false"); break;
default: break;
}
return 0;
}
static int pp_aux_ref(uint64_t key, uint64_t val, char **line, size_t *n,
FILE *m, FILE *o)
{
switch (key) {
case AT_BASE_PLATFORM:
case AT_EXECFN:
case AT_PLATFORM:
if (read_mem_str(line, n, m, val))
return -1;
fprintf(o, " %s", *line);
break;
case AT_RANDOM:
if (read_mem_rand(line, n, m, val))
return -1;
fprintf(o, " %s", *line);
default: break;
}
return 0;
}
static int pp_aux_v(uint64_t key, FILE *o, const Args *args)
{
if (args->verbose && key < sizeof auxv_type_map / sizeof auxv_type_map[0]
&& *auxv_type_map[key].desc)
fprintf(o, " (%s)", auxv_type_map[key].desc);
return 0;
}
static int fput_proc_auxv_file(FILE *f, bool is_64, FILE *m, FILE *o,
const Args *args)
{
size_t n = 512;
char *line = malloc(n);
if (!line)
return -1;
*line = 0;
for (size_t i = 0;; ++i) {
uint64_t v[2] = {0};
if (is_64) {
size_t r = fread(&v, 1, sizeof v, f);
if (r != sizeof v) {
if (ferror(f))
perror(0);
free(line);
return -1;
}
} else {
uint32_t w[2] = {0};
size_t r = fread(&w, 1, sizeof w, f);
if (r != sizeof w) {
if (ferror(f))
perror(0);
free(line);
return -1;
}
v[0] = w[0];
v[1] = w[1];
}
if (!v[0]) {
free(line);
return 0;
}
if (i)
fputc('\n', o);
if (v[0] < sizeof auxv_type_map / sizeof auxv_type_map[0])
fprintf(o, "%-16s", auxv_type_map[v[0]].key);
else
fprintf(o, "unk_%" PRIu64, v[0]);
fprintf(o, " 0x%.16" PRIx64, v[1]);
int r = pp_aux(v[0], v[1], o);
if (r) {
free(line);
return r;
}
r = pp_aux_ref(v[0], v[1], &line, &n, m, o);
if (r) {
free(line);
return r;
}
r = pp_aux_v(v[0], o, args);
if (r) {
free(line);
return r;
}
}
free(line);
return -1;
}
// https://unix.stackexchange.com/questions/6301/how-do-i-read-from-proc-pid-mem-under-linux
static FILE *fopen_mem(const char *pid, bool stop_process)
{
// at least on Fedora 26, 4.14.8-200.fc26.x86_64
// reading the auxv referenced memory also works while
// the process is running ...
// process stop/start is just necessary for Linux < 3.2:
// https://unix.stackexchange.com/questions/6301/how-do-i-read-from-proc-pid-mem-under-linux#comment395763_6302
if (stop_process) {
pid_t p = atol(pid);
// XXX for root, we just need to stop the process?
long r = ptrace(PTRACE_ATTACH, p, 0, 0);
if (r == -1) {
perror("ptrace attach failed");
return 0;
}
siginfo_t info;
r = waitid(P_PID, p, &info, WSTOPPED);
if (r == -1) {
perror(0);
return 0;
}
}
char filename[64];
sprintf(filename, "/proc/%s/mem", pid);
FILE *f = fopen(filename, "rb");
if (!f) {
perror("open /proc/$pid/mem failed");
}
return f;
}
static int fclose_mem(const char *pid, FILE *f, bool stop_process)
{
int rc = 0;
if (f) {
int r = fclose(f);
if (r) {
perror(0);
rc = -1;
}
}
if (stop_process) {
pid_t p = atol(pid);
// XXX for root, we just need to start the process?
long r = ptrace(PTRACE_DETACH, p, 0, 0);
if (r == -1) {
perror(0);
return -1;
}
}
return rc;
}
static int fput_proc_auxv(const char *pid, FILE *o, const Args *args)
{
char filename[64];
sprintf(filename, "/proc/%s/auxv", pid);
FILE *f = fopen(filename, "rb");
if (!f) {
perror(0);
return -1;
}
bool is_64;
int r = is_auxv_64(pid, &is_64);
if (r)
return r;
FILE *m = fopen_mem(pid, args->stop_process);
if (!m) {
fclose(f);
return -1;
}
r = fput_proc_auxv_file(f, is_64, m, o, args);
if (r) {
fclose_mem(pid, m, args->stop_process); // RAII would simplify things ...
fclose(f);
return r;
}
r = fclose(f);
if (r) {
fclose_mem(pid, m, args->stop_process);
return r;
}
r = fclose_mem(pid, m, args->stop_process);
if (r)
return r;
return 0;
}
static int main_pid(const Args *args, const char *pid)
{
int r;
if (args->print_cmdline) {
r = fput_proc_vector(pid, "cmdline", 0, " ", stdout);
fputc('\n', stdout);
if (r)
return 1;
} else {
r = fput_header(pid, stdout);
if (r)
return 1;
fputc('\n', stdout);
if (args->print_argv) {
r = fput_proc_vector(pid, "cmdline", "argv", "\n", stdout);
if (r)
return 1;
fputc('\n', stdout);
}
if (args->print_envp) {
if (args->print_argv)
fputc('\n', stdout);
r = fput_proc_vector(pid, "environ", "envp", "\n", stdout);
if (r)
return 1;
fputc('\n', stdout);
}
if (args->print_auxv) {
if (args->print_argv || args->print_envp)
fputc('\n', stdout);
r = fput_proc_auxv(pid, stdout, args);
if (r)
return 1;
fputc('\n', stdout);
}
}
return 0;
}
struct Range {
const unsigned char *begin;
const unsigned char *end;
};
typedef struct Range Range;
static int mmap_core(const char *filename, Range *range)
{
int fd = open(filename, O_RDONLY);
if (fd == -1) {
perror(0);
return -1;
}
struct stat st;
int r = fstat(fd, &st);
unsigned char *p = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (p == (void*)-1) {
perror(0);
return -1;
}
range->begin = p;
range->end = p + st.st_size;
r = close(fd);
if (r == -1) {
perror(0);
return -1;
}
return 0;
}
struct Landmarks {
bool need_to_swap;
uint32_t pid;
uint64_t execfn_addr; // substract vector_base_addr to get offset into vector_section
uint8_t word_size;
Range auxv_note; // read pairs of word_size until {0,0}
Range vector_section;
Range envp;
Range argv;
int argc;
uint64_t vector_base_addr;
};
typedef struct Landmarks Landmarks;
static uint32_t align32_up_4(uint32_t i)
{
i += 3;
// binary literals are a GCC extensions in C11
// they are available in C++14 and we want help to create a precedent
i &= ~((uint32_t) 0b11);
return i;
}
static const unsigned char *aligned_naive_memmemr(
const unsigned char *haystack, size_t haystacklen,
const unsigned char *needle, size_t needlelen, unsigned dec)
{
if (needlelen > haystacklen)
return 0;
if (dec > needlelen)
return 0;
for (size_t i = haystacklen - needlelen + dec; i >= dec; i -= dec) {
const unsigned char *p = haystack + (i - dec);
if (!memcmp(p, needle, needlelen))
return p;
}
return 0;
}
#define PARGS_ELF_CLASS 32
#include "pargs_tmpl.c"
#undef PARGS_ELF_CLASS
#define PARGS_ELF_CLASS 64
#include "pargs_tmpl.c"
#undef PARGS_ELF_CLASS
// a good reference and introduction into the ELF format:
// /usr/include/elf.h (Linux)
// elf(5) Linux man page
static int parse_landmarks(const Range *range, const char *filename,
Landmarks *lm)
{
const unsigned char *b = range->begin;
const unsigned char *e = range->end;
if ((size_t)(e - b) < sizeof(Elf32_Ehdr)) {
fprintf(stderr, "File %s even too small for ELF32 header.\n", filename);
return -1;
}
static const unsigned char elf_magic[] = { 0x7f, 'E', 'L', 'F' };
if (memcmp(b, elf_magic, sizeof elf_magic)) {
fprintf(stderr, "Couldn't find ELF magic in %s.\n", filename);
return -1;
}
debug("Reading core file: %s", filename);
switch (b[EI_DATA]) {
case ELFDATA2LSB:
debug("Detected little endian byte order");
lm->need_to_swap = __BYTE_ORDER == __BIG_ENDIAN;
break;
case ELFDATA2MSB:
debug("Detected big endian byte order");
lm->need_to_swap = __BYTE_ORDER == __LITTLE_ENDIAN;
break;
default:
fprintf(stderr, "Unknown byte order in %s.\n", filename);
}
switch (b[EI_CLASS]) {
case ELFCLASS32:
debug("Reading 32 Bit ELF file");
lm->word_size = 32;
return parse_landmarks_32(range, filename, lm);
break;
case ELFCLASS64:
debug("Reading 64 Bit ELF file");
lm->word_size = 64;
return parse_landmarks_64(range, filename, lm);
break;
default:
fprintf(stderr, "Unknown ELF class in %s.\n", filename);
return -1;
}
return 0;
}
static int fput_core_auxv(const Landmarks *lm, FILE *o, const Args *args)
{
if (lm->word_size == 64)
return fput_core_auxv_64(lm, o, args);
else
return fput_core_auxv_32(lm, o, args);
}
static int fput_core_vector(const Landmarks *lm, const Range *vec,
const char *prefix, const char *delim, FILE *o)
{
if (lm->word_size == 64)
return fput_core_vector_64(lm, vec, prefix, delim, o);
else
return fput_core_vector_32(lm, vec, prefix, delim, o);
}
static int fput_core_header(const char *filename, const Landmarks *lm,
FILE *o)
{
fprintf(o, "core '%s' of %" PRIu32 ": ", filename, lm->pid);
// on Fedora 26, lm->args, i.e. prpsinfo_t::pr_psargs just
// contains argv[0] and a trailing space, thus, it's not sufficient
// for our puposes
return fput_core_vector(lm, &lm->argv, 0, " ", stdout);
}
static int main_core(const Args *args, const char *filename)
{
Range range;
int r = mmap_core(filename, &range);
if (r)
return r;
Landmarks lm = {0};
r = parse_landmarks(&range, filename, &lm);
if (r)
return r;
if (args->print_cmdline) {
r = fput_core_vector(&lm, &lm.argv, 0, " ", stdout);
fputc('\n', stdout);
if (r)
return 1;
} else {
r = fput_core_header(filename, &lm, stdout);
if (r)
return 1;
fputc('\n', stdout);
if (args->print_argv) {
r = fput_core_vector(&lm, &lm.argv, "argv", "\n", stdout);
if (r)
return 1;
fputc('\n', stdout);
}
if (args->print_envp) {
if (args->print_argv)
fputc('\n', stdout);
r = fput_core_vector(&lm, &lm.envp, "envp", "\n", stdout);
if (r)
return 1;
fputc('\n', stdout);
}
if (args->print_auxv) {
if (args->print_argv || args->print_envp)
fputc('\n', stdout);
r = fput_core_auxv(&lm, stdout, args);
if (r)
return 1;
fputc('\n', stdout);
}
}
r = munmap((void*)range.begin, range.end - range.begin);
if (r == -1) {
perror(0);
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
Args args;
int r = parse_args(argc, argv, &args);
if (r)
return r-1;
size_t cnt = 0;
bool more_opts = true;
for (int i = 1; i < argc; ++i) {
if (*argv[i] == '-' && argv[i][1] == '-') {
more_opts = false;
} else if (is_all_num(argv[i])) {
if (cnt)
fputc('\n', stdout);
int r = main_pid(&args, argv[i]);
if (r)
return 1;
++cnt;
} else if ((*argv[i] && *argv[i] != '-') || !more_opts) {
int r = main_core(&args, argv[i]);
if (r)
return 1;
++cnt;
}
}
if (!cnt) {
print_help(stderr, *argv);
return 2;
}
return 0;
}