-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathps_core.c
More file actions
1629 lines (1437 loc) · 54.5 KB
/
ps_core.c
File metadata and controls
1629 lines (1437 loc) · 54.5 KB
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
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include <jni.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include "libproc_impl.h"
#include "cds.h"
#ifdef __APPLE__
#if defined(amd64)
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
#elif defined(aarch64)
#include "sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext.h"
#else
#error UNSUPPORTED_ARCH
#endif
#endif /* __APPLE__ */
// This file has the libproc implementation to read core files.
// For live processes, refer to ps_proc.c. Portions of this is adapted
// /modelled after Solaris libproc.so (in particular Pcore.c)
//----------------------------------------------------------------------
// ps_prochandle cleanup helper functions
// close all file descriptors
static void close_files(struct ps_prochandle* ph) {
lib_info* lib = NULL;
// close core file descriptor
if (ph->core->core_fd >= 0)
close(ph->core->core_fd);
// close exec file descriptor
if (ph->core->exec_fd >= 0)
close(ph->core->exec_fd);
// close interp file descriptor
if (ph->core->interp_fd >= 0)
close(ph->core->interp_fd);
// close class share archive file
if (ph->core->classes_jsa_fd >= 0)
close(ph->core->classes_jsa_fd);
// close all library file descriptors
lib = ph->libs;
while (lib) {
int fd = lib->fd;
if (fd >= 0 && fd != ph->core->exec_fd) {
close(fd);
}
lib = lib->next;
}
}
// clean all map_info stuff
static void destroy_map_info(struct ps_prochandle* ph) {
map_info* map = ph->core->maps;
while (map) {
map_info* next = map->next;
free(map);
map = next;
}
if (ph->core->map_array) {
free(ph->core->map_array);
}
// Part of the class sharing workaround
map = ph->core->class_share_maps;
while (map) {
map_info* next = map->next;
free(map);
map = next;
}
}
// ps_prochandle operations
static void core_release(struct ps_prochandle* ph) {
if (ph->core) {
close_files(ph);
destroy_map_info(ph);
free(ph->core);
}
}
static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
map_info* map;
if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
print_debug("can't allocate memory for map_info\n");
return NULL;
}
// initialize map
map->fd = fd;
map->offset = offset;
map->vaddr = vaddr;
map->memsz = memsz;
return map;
}
// add map info with given fd, offset, vaddr and memsz
static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset,
uintptr_t vaddr, size_t memsz) {
map_info* map;
if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) {
return NULL;
}
// add this to map list
map->next = ph->core->maps;
ph->core->maps = map;
ph->core->num_maps++;
return map;
}
// Part of the class sharing workaround
static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset,
uintptr_t vaddr, size_t memsz) {
map_info* map;
if ((map = allocate_init_map(ph->core->classes_jsa_fd,
offset, vaddr, memsz)) == NULL) {
return NULL;
}
map->next = ph->core->class_share_maps;
ph->core->class_share_maps = map;
return map;
}
// Return the map_info for the given virtual address. We keep a sorted
// array of pointers in ph->map_array, so we can binary search.
static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) {
int mid, lo = 0, hi = ph->core->num_maps - 1;
map_info *mp;
while (hi - lo > 1) {
mid = (lo + hi) / 2;
if (addr >= ph->core->map_array[mid]->vaddr) {
lo = mid;
} else {
hi = mid;
}
}
if (addr < ph->core->map_array[hi]->vaddr) {
mp = ph->core->map_array[lo];
} else {
mp = ph->core->map_array[hi];
}
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
return (mp);
}
// Part of the class sharing workaround
// Unfortunately, we have no way of detecting -Xshare state.
// Check out the share maps atlast, if we don't find anywhere.
// This is done this way so to avoid reading share pages
// ahead of other normal maps. For eg. with -Xshare:off we don't
// want to prefer class sharing data to data from core.
mp = ph->core->class_share_maps;
if (mp) {
print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr);
}
while (mp) {
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
print_debug("located map_info at 0x%lx from class share maps\n", addr);
return (mp);
}
mp = mp->next;
}
print_debug("can't locate map_info at 0x%lx\n", addr);
return (NULL);
}
//---------------------------------------------------------------
// Part of the class sharing workaround:
//
// With class sharing, pages are mapped from classes.jsa file.
// The read-only class sharing pages are mapped as MAP_SHARED,
// PROT_READ pages. These pages are not dumped into core dump.
// With this workaround, these pages are read from classes.jsa.
static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
jboolean i;
if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
*pvalue = i;
return true;
} else {
return false;
}
}
static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
uintptr_t uip;
if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
*pvalue = uip;
return true;
} else {
return false;
}
}
// used to read strings from debuggee
static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) {
size_t i = 0;
char c = ' ';
while (c != '\0') {
if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) {
return false;
}
if (i < size - 1) {
buf[i] = c;
} else {
// smaller buffer
return false;
}
i++; addr++;
}
buf[i] = '\0';
return true;
}
// mangled name of Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
#ifdef __APPLE__
#define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
#define LIBJVM_NAME "/libjvm.dylib"
#else
#define USE_SHARED_SPACES_SYM "UseSharedSpaces"
#define LIBJVM_NAME "/libjvm.so"
#endif // __APPLE_
static bool init_classsharing_workaround(struct ps_prochandle* ph) {
int m;
size_t n;
lib_info* lib = ph->libs;
while (lib != NULL) {
// we are iterating over shared objects from the core dump. look for
// libjvm.so.
const char *jvm_name = 0;
if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
char classes_jsa[PATH_MAX];
CDSFileMapHeaderBase header;
int fd = -1;
uintptr_t base = 0, useSharedSpacesAddr = 0;
uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
jboolean useSharedSpaces = 0;
memset(classes_jsa, 0, sizeof(classes_jsa));
jvm_name = lib->name;
useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
if (useSharedSpacesAddr == 0) {
print_debug("can't lookup 'UseSharedSpaces' flag\n");
return false;
}
// Hotspot vm types are not exported to build this library. So
// using equivalent type jboolean to read the value of
// UseSharedSpaces which is same as hotspot type "bool".
if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
print_debug("can't read the value of 'UseSharedSpaces' flag\n");
return false;
}
if ((int)useSharedSpaces == 0) {
print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
return true;
}
sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM);
if (sharedArchivePathAddrAddr == 0) {
print_debug("can't lookup shared archive path symbol\n");
return false;
}
if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
print_debug("can't read shared archive path pointer\n");
return false;
}
if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
print_debug("can't read shared archive path value\n");
return false;
}
print_debug("looking for %s\n", classes_jsa);
// open the class sharing archive file
fd = pathmap_open(classes_jsa);
if (fd < 0) {
print_debug("can't open %s!\n", classes_jsa);
ph->core->classes_jsa_fd = -1;
return false;
} else {
print_debug("opened %s\n", classes_jsa);
}
// read CDSFileMapHeaderBase from the file
memset(&header, 0, sizeof(CDSFileMapHeaderBase));
if ((n = read(fd, &header, sizeof(CDSFileMapHeaderBase)))
!= sizeof(CDSFileMapHeaderBase)) {
print_debug("can't read shared archive file map header from %s\n", classes_jsa);
close(fd);
return false;
}
// check file magic
if (header._magic != CDS_ARCHIVE_MAGIC) {
print_debug("%s has bad shared archive file magic number 0x%x, expecting 0x%x\n",
classes_jsa, header._magic, CDS_ARCHIVE_MAGIC);
close(fd);
return false;
}
// check version
if (header._version != CURRENT_CDS_ARCHIVE_VERSION) {
print_debug("%s has wrong shared archive file version %d, expecting %d\n",
classes_jsa, header._version, CURRENT_CDS_ARCHIVE_VERSION);
close(fd);
return false;
}
ph->core->classes_jsa_fd = fd;
// add read-only maps from classes.jsa to the list of maps
for (m = 0; m < NUM_CDS_REGIONS; m++) {
if (header._space[m]._read_only) {
base = (uintptr_t) header._space[m]._addr._base;
// no need to worry about the fractional pages at-the-end.
// possible fractional pages are handled by core_read_data.
add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
base, (size_t) header._space[m]._used);
print_debug("added a share archive map at 0x%lx\n", base);
}
}
return true;
}
lib = lib->next;
}
return true;
}
//---------------------------------------------------------------------------
// functions to handle map_info
// Order mappings based on virtual address. We use this function as the
// callback for sorting the array of map_info pointers.
static int core_cmp_mapping(const void *lhsp, const void *rhsp)
{
const map_info *lhs = *((const map_info **)lhsp);
const map_info *rhs = *((const map_info **)rhsp);
if (lhs->vaddr == rhs->vaddr) {
return (0);
}
return (lhs->vaddr < rhs->vaddr ? -1 : 1);
}
// we sort map_info by starting virtual address so that we can do
// binary search to read from an address.
static bool sort_map_array(struct ps_prochandle* ph) {
size_t num_maps = ph->core->num_maps;
map_info* map = ph->core->maps;
int i = 0;
// allocate map_array
map_info** array;
if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
print_debug("can't allocate memory for map array\n");
return false;
}
// add maps to array
while (map) {
array[i] = map;
i++;
map = map->next;
}
// sort is called twice. If this is second time, clear map array
if (ph->core->map_array) {
free(ph->core->map_array);
}
ph->core->map_array = array;
// sort the map_info array by base virtual address.
qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
core_cmp_mapping);
// print map
if (is_debug()) {
int j = 0;
print_debug("---- sorted virtual address map ----\n");
for (j = 0; j < ph->core->num_maps; j++) {
print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
ph->core->map_array[j]->memsz);
}
}
return true;
}
#ifndef MIN
#define MIN(x, y) (((x) < (y))? (x): (y))
#endif
static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
ssize_t resid = size;
int page_size=sysconf(_SC_PAGE_SIZE);
while (resid != 0) {
map_info *mp = core_lookup(ph, addr);
uintptr_t mapoff;
ssize_t len, rem;
off_t off;
int fd;
if (mp == NULL) {
break; /* No mapping for this address */
}
fd = mp->fd;
mapoff = addr - mp->vaddr;
len = MIN(resid, mp->memsz - mapoff);
off = mp->offset + mapoff;
if ((len = pread(fd, buf, len, off)) <= 0) {
break;
}
resid -= len;
addr += len;
buf = (char *)buf + len;
// mappings always start at page boundary. But, may end in fractional
// page. fill zeros for possible fractional page at the end of a mapping.
rem = mp->memsz % page_size;
if (rem > 0) {
rem = page_size - rem;
len = MIN(resid, rem);
resid -= len;
addr += len;
// we are not assuming 'buf' to be zero initialized.
memset(buf, 0, len);
buf += len;
}
}
if (resid) {
print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
size, addr, resid);
return false;
} else {
return true;
}
}
// null implementation for write
static bool core_write_data(struct ps_prochandle* ph,
uintptr_t addr, const char *buf , size_t size) {
return false;
}
static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
struct reg* regs) {
// for core we have cached the lwp regs after segment parsed
sa_thread_info* thr = ph->threads;
while (thr) {
if (thr->lwp_id == lwp_id) {
memcpy(regs, &thr->regs, sizeof(struct reg));
return true;
}
thr = thr->next;
}
return false;
}
static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) {
print_debug("core_get_lwp_info not implemented\n");
return false;
}
static ps_prochandle_ops core_ops = {
.release= core_release,
.p_pread= core_read_data,
.p_pwrite= core_write_data,
.get_lwp_regs= core_get_lwp_regs,
.get_lwp_info= core_get_lwp_info
};
// from this point, mainly two blocks divided by def __APPLE__
// one for Macosx, the other for regular Bsd
#ifdef __APPLE__
void print_thread(sa_thread_info *threadinfo) {
print_debug("thread added: %d\n", threadinfo->lwp_id);
print_debug("registers:\n");
#if defined(amd64)
print_debug(" r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
print_debug(" r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
print_debug(" r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
print_debug(" r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
print_debug(" r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
print_debug(" r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
print_debug(" r_r9: 0x%" PRIx64 "\n", threadinfo->regs.r_r9);
print_debug(" r_r8: 0x%" PRIx64 "\n", threadinfo->regs.r_r8);
print_debug(" r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi);
print_debug(" r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi);
print_debug(" r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp);
print_debug(" r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx);
print_debug(" r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx);
print_debug(" r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx);
print_debug(" r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax);
print_debug(" r_fs: 0x%" PRIx32 "\n", threadinfo->regs.r_fs);
print_debug(" r_gs: 0x%" PRIx32 "\n", threadinfo->regs.r_gs);
print_debug(" r_rip 0x%" PRIx64 "\n", threadinfo->regs.r_rip);
print_debug(" r_cs: 0x%" PRIx64 "\n", threadinfo->regs.r_cs);
print_debug(" r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp);
print_debug(" r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags);
#elif defined(aarch64)
print_debug(" r_r0: 0x%" PRIx64 "\n", threadinfo->regs.r_r0);
print_debug(" r_r1: 0x%" PRIx64 "\n", threadinfo->regs.r_r1);
print_debug(" r_r2: 0x%" PRIx64 "\n", threadinfo->regs.r_r2);
print_debug(" r_r3: 0x%" PRIx64 "\n", threadinfo->regs.r_r3);
print_debug(" r_r4: 0x%" PRIx64 "\n", threadinfo->regs.r_r4);
print_debug(" r_r5: 0x%" PRIx64 "\n", threadinfo->regs.r_r5);
print_debug(" r_r6: 0x%" PRIx64 "\n", threadinfo->regs.r_r6);
print_debug(" r_r7: 0x%" PRIx64 "\n", threadinfo->regs.r_r7);
print_debug(" r_r8: 0x%" PRIx64 "\n", threadinfo->regs.r_r8);
print_debug(" r_r9: 0x%" PRIx64 "\n", threadinfo->regs.r_r9);
print_debug(" r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
print_debug(" r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
print_debug(" r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
print_debug(" r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
print_debug(" r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
print_debug(" r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
print_debug(" r_r16: 0x%" PRIx64 "\n", threadinfo->regs.r_r16);
print_debug(" r_r17: 0x%" PRIx64 "\n", threadinfo->regs.r_r17);
print_debug(" r_r18: 0x%" PRIx64 "\n", threadinfo->regs.r_r18);
print_debug(" r_r19: 0x%" PRIx64 "\n", threadinfo->regs.r_r19);
print_debug(" r_r20: 0x%" PRIx64 "\n", threadinfo->regs.r_r20);
print_debug(" r_r21: 0x%" PRIx64 "\n", threadinfo->regs.r_r21);
print_debug(" r_r22: 0x%" PRIx64 "\n", threadinfo->regs.r_r22);
print_debug(" r_r23: 0x%" PRIx64 "\n", threadinfo->regs.r_r23);
print_debug(" r_r24: 0x%" PRIx64 "\n", threadinfo->regs.r_r24);
print_debug(" r_r25: 0x%" PRIx64 "\n", threadinfo->regs.r_r25);
print_debug(" r_r26: 0x%" PRIx64 "\n", threadinfo->regs.r_r26);
print_debug(" r_r27: 0x%" PRIx64 "\n", threadinfo->regs.r_r27);
print_debug(" r_r28: 0x%" PRIx64 "\n", threadinfo->regs.r_r28);
print_debug(" r_fp: 0x%" PRIx64 "\n", threadinfo->regs.r_fp);
print_debug(" r_lr: 0x%" PRIx64 "\n", threadinfo->regs.r_lr);
print_debug(" r_sp: 0x%" PRIx64 "\n", threadinfo->regs.r_sp);
print_debug(" r_pc: 0x%" PRIx64 "\n", threadinfo->regs.r_pc);
#else
#error UNSUPPORTED_ARCH
#endif
}
// read all segments64 commands from core file
// read all thread commands from core file
static bool read_core_segments(struct ps_prochandle* ph) {
int i = 0;
int num_threads = 0;
int fd = ph->core->core_fd;
off_t offset = 0;
mach_header_64 fhead;
load_command lcmd;
segment_command_64 segcmd;
// thread_command thrcmd;
lseek(fd, offset, SEEK_SET);
if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
goto err;
}
print_debug("total commands: %d\n", fhead.ncmds);
offset += sizeof(mach_header_64);
for (i = 0; i < fhead.ncmds; i++) {
lseek(fd, offset, SEEK_SET);
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
goto err;
}
offset += lcmd.cmdsize; // next command position
//print_debug("LC: 0x%x\n", lcmd.cmd);
if (lcmd.cmd == LC_SEGMENT_64) {
lseek(fd, -sizeof(load_command), SEEK_CUR);
if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) {
print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i);
goto err;
}
// The base of the library is offset by a random amount which ends up as a load command with a
// filesize of 0. This must be ignored otherwise the base address of the library is wrong.
if (segcmd.filesize != 0) {
if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) {
print_debug("Failed to add map_info at i = %d\n", i);
goto err;
}
}
print_debug("LC_SEGMENT_64 %s: nsects=%d fileoff=0x%llx vmaddr=0x%llx vmsize=0x%llx filesize=0x%llx %s\n",
segcmd.filesize == 0 ? "with filesize == 0 ignored" : "added",
segcmd.nsects, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize,
segcmd.filesize, &segcmd.segname[0]);
} else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) {
typedef struct thread_fc {
uint32_t flavor;
uint32_t count;
} thread_fc;
thread_fc fc;
uint32_t size = sizeof(load_command);
while (size < lcmd.cmdsize) {
if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(thread_fc);
#if defined(amd64)
if (fc.flavor == x86_THREAD_STATE) {
x86_thread_state_t thrstate;
if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(x86_thread_state_t);
// create thread info list, update lwp_id later
sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
if (newthr == NULL) {
printf("create thread_info failed\n");
goto err;
}
// note __DARWIN_UNIX03 depengs on other definitions
#if __DARWIN_UNIX03
#define get_register_v(regst, regname) \
regst.uts.ts64.__##regname
#else
#define get_register_v(regst, regname) \
regst.uts.ts64.##regname
#endif // __DARWIN_UNIX03
newthr->regs.r_rax = get_register_v(thrstate, rax);
newthr->regs.r_rbx = get_register_v(thrstate, rbx);
newthr->regs.r_rcx = get_register_v(thrstate, rcx);
newthr->regs.r_rdx = get_register_v(thrstate, rdx);
newthr->regs.r_rdi = get_register_v(thrstate, rdi);
newthr->regs.r_rsi = get_register_v(thrstate, rsi);
newthr->regs.r_rbp = get_register_v(thrstate, rbp);
newthr->regs.r_rsp = get_register_v(thrstate, rsp);
newthr->regs.r_r8 = get_register_v(thrstate, r8);
newthr->regs.r_r9 = get_register_v(thrstate, r9);
newthr->regs.r_r10 = get_register_v(thrstate, r10);
newthr->regs.r_r11 = get_register_v(thrstate, r11);
newthr->regs.r_r12 = get_register_v(thrstate, r12);
newthr->regs.r_r13 = get_register_v(thrstate, r13);
newthr->regs.r_r14 = get_register_v(thrstate, r14);
newthr->regs.r_r15 = get_register_v(thrstate, r15);
newthr->regs.r_rip = get_register_v(thrstate, rip);
newthr->regs.r_rflags = get_register_v(thrstate, rflags);
newthr->regs.r_cs = get_register_v(thrstate, cs);
newthr->regs.r_fs = get_register_v(thrstate, fs);
newthr->regs.r_gs = get_register_v(thrstate, gs);
print_thread(newthr);
} else if (fc.flavor == x86_FLOAT_STATE) {
x86_float_state_t flstate;
if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) {
print_debug("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(x86_float_state_t);
} else if (fc.flavor == x86_EXCEPTION_STATE) {
x86_exception_state_t excpstate;
if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(x86_exception_state_t);
}
#elif defined(aarch64)
if (fc.flavor == ARM_THREAD_STATE64) {
arm_thread_state64_t thrstate;
if (read(fd, (void *)&thrstate, sizeof(arm_thread_state64_t)) != sizeof(arm_thread_state64_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(arm_thread_state64_t);
// create thread info list, update lwp_id later
sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
if (newthr == NULL) {
printf("create thread_info failed\n");
goto err;
}
// note __DARWIN_UNIX03 depengs on other definitions
#if __DARWIN_UNIX03
#define get_register_v(regst, regname) \
regst.__##regname
#else
#define get_register_v(regst, regname) \
regst.##regname
#endif // __DARWIN_UNIX03
newthr->regs.r_r0 = get_register_v(thrstate, x[0]);
newthr->regs.r_r1 = get_register_v(thrstate, x[1]);
newthr->regs.r_r2 = get_register_v(thrstate, x[2]);
newthr->regs.r_r3 = get_register_v(thrstate, x[3]);
newthr->regs.r_r4 = get_register_v(thrstate, x[4]);
newthr->regs.r_r5 = get_register_v(thrstate, x[5]);
newthr->regs.r_r6 = get_register_v(thrstate, x[6]);
newthr->regs.r_r7 = get_register_v(thrstate, x[7]);
newthr->regs.r_r8 = get_register_v(thrstate, x[8]);
newthr->regs.r_r9 = get_register_v(thrstate, x[9]);
newthr->regs.r_r10 = get_register_v(thrstate, x[10]);
newthr->regs.r_r11 = get_register_v(thrstate, x[11]);
newthr->regs.r_r12 = get_register_v(thrstate, x[12]);
newthr->regs.r_r13 = get_register_v(thrstate, x[13]);
newthr->regs.r_r14 = get_register_v(thrstate, x[14]);
newthr->regs.r_r15 = get_register_v(thrstate, x[15]);
newthr->regs.r_r16 = get_register_v(thrstate, x[16]);
newthr->regs.r_r17 = get_register_v(thrstate, x[17]);
newthr->regs.r_r18 = get_register_v(thrstate, x[18]);
newthr->regs.r_r19 = get_register_v(thrstate, x[19]);
newthr->regs.r_r20 = get_register_v(thrstate, x[20]);
newthr->regs.r_r21 = get_register_v(thrstate, x[21]);
newthr->regs.r_r22 = get_register_v(thrstate, x[22]);
newthr->regs.r_r23 = get_register_v(thrstate, x[23]);
newthr->regs.r_r24 = get_register_v(thrstate, x[24]);
newthr->regs.r_r25 = get_register_v(thrstate, x[25]);
newthr->regs.r_r26 = get_register_v(thrstate, x[26]);
newthr->regs.r_r27 = get_register_v(thrstate, x[27]);
newthr->regs.r_r28 = get_register_v(thrstate, x[28]);
newthr->regs.r_fp = get_register_v(thrstate, fp);
newthr->regs.r_lr = get_register_v(thrstate, lr);
newthr->regs.r_sp = get_register_v(thrstate, sp);
newthr->regs.r_pc = get_register_v(thrstate, pc);
print_thread(newthr);
} else if (fc.flavor == ARM_NEON_STATE64) {
arm_neon_state64_t flstate;
if (read(fd, (void *)&flstate, sizeof(arm_neon_state64_t)) != sizeof(arm_neon_state64_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(arm_neon_state64_t);
} else if (fc.flavor == ARM_EXCEPTION_STATE64) {
arm_exception_state64_t excpstate;
if (read(fd, (void *)&excpstate, sizeof(arm_exception_state64_t)) != sizeof(arm_exception_state64_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(arm_exception_state64_t);
} else if (fc.flavor == ARM_DEBUG_STATE64) {
arm_debug_state64_t dbgstate;
if (read(fd, (void *)&dbgstate, sizeof(arm_debug_state64_t)) != sizeof(arm_debug_state64_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(arm_debug_state64_t);
}
#else
#error UNSUPPORTED_ARCH
#endif
}
}
}
return true;
err:
return false;
}
/**local function **/
bool exists(const char *fname) {
return access(fname, F_OK) == 0;
}
// we check: 1. lib
// 2. lib/server
// 3. jre/lib
// 4. jre/lib/server
// from: 1. exe path
// 2. JAVA_HOME
// 3. DYLD_LIBRARY_PATH
static bool get_real_path(struct ps_prochandle* ph, char *rpath) {
/** check if they exist in JAVA ***/
char* execname = ph->core->exec_path;
char filepath[4096];
char* filename = strrchr(rpath, '/'); // like /libjvm.dylib
if (filename == NULL) {
return false;
}
char* posbin = strstr(execname, "/bin/java");
if (posbin != NULL) {
memcpy(filepath, execname, posbin - execname); // not include trailing '/'
filepath[posbin - execname] = '\0';
} else {
char* java_home = getenv("JAVA_HOME");
if (java_home != NULL) {
strcpy(filepath, java_home);
} else {
char* dyldpath = getenv("DYLD_LIBRARY_PATH");
char* dypath = strtok(dyldpath, ":");
while (dypath != NULL) {
strcpy(filepath, dypath);
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
dypath = strtok(dyldpath, ":");
}
// not found
return false;
}
}
// for exec and java_home, jdkpath now is filepath
size_t filepath_base_size = strlen(filepath);
// first try /lib/ and /lib/server
strcat(filepath, "/lib");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
char* pos = strstr(filepath, filename); // like /libjvm.dylib
*pos = '\0';
strcat(filepath, "/server");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
// then try /jre/lib/ and /jre/lib/server
filepath[filepath_base_size] = '\0';
strcat(filepath, "/jre/lib");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
pos = strstr(filepath, filename);
*pos = '\0';
strcat(filepath, "/server");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
return false;
}
static bool read_shared_lib_info(struct ps_prochandle* ph) {
static int pagesize = 0;
int fd = ph->core->core_fd;
int i = 0, j;
uint32_t v;
mach_header_64 header; // used to check if a file header in segment
load_command lcmd;
dylib_command dylibcmd;
char name[BUF_SIZE]; // use to store name
if (pagesize == 0) {
pagesize = getpagesize();
print_debug("page size is %d\n", pagesize);
}
for (j = 0; j < ph->core->num_maps; j++) {
map_info *iter = ph->core->map_array[j]; // head
off_t fpos = iter->offset;
if (iter->fd != fd) {
// only search core file!
continue;
}
print_debug("map_info %d: vmaddr = 0x%016llx fileoff = 0x%llx vmsize = 0x%lx\n",
j, iter->vaddr, iter->offset, iter->memsz);
lseek(fd, fpos, SEEK_SET);
// we assume .dylib loaded at segment address --- which is true for JVM libraries
// multiple files may be loaded in one segment.
// if first word is not a magic word, means this segment does not contain lib file.
if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) {
if (v != MH_MAGIC_64) {
continue;
}
} else {
// may be encountered last map, which is not readable
continue;
}
while (ltell(fd) - iter->offset < iter->memsz) {
lseek(fd, fpos, SEEK_SET);
if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) {
break;
}
if (v != MH_MAGIC_64) {
fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize;
continue;
}
lseek(fd, -sizeof(uint32_t), SEEK_CUR);
// This is the begining of the mach-o file in the segment.
if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
goto err;
}
fpos = ltell(fd);
// found a mach-o file in this segment
for (i = 0; i < header.ncmds; i++) {
// read commands in this "file"
// LC_ID_DYLIB is the file itself for a .dylib
lseek(fd, fpos, SEEK_SET);
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
return false; // error
}
fpos += lcmd.cmdsize; // next command position
// make sure still within seg size.
if (fpos - lcmd.cmdsize - iter->offset > iter->memsz) {
print_debug("Warning: out of segement limit: %ld \n", fpos - lcmd.cmdsize - iter->offset);
break; // no need to iterate all commands
}
if (lcmd.cmd == LC_ID_DYLIB) {
lseek(fd, -sizeof(load_command), SEEK_CUR);
if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) {
return false;
}
/**** name stored at dylib_command.dylib.name.offset, is a C string */
lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR);
int j = 0;
while (j < BUF_SIZE) {
read(fd, (void *)(name + j), sizeof(char));
if (name[j] == '\0') break;
j++;
}
print_debug("%d %s\n", lcmd.cmd, name);
// changed name from @rpath/xxxx.dylib to real path
if (strrchr(name, '@')) {
get_real_path(ph, name);
print_debug("get_real_path returned: %s\n", name);
} else {
break; // Ignore non-relative paths, which are system libs. See JDK-8249779.
}
add_lib_info(ph, name, iter->vaddr);
break;
}
}
// done with the file, advanced to next page to search more files
#if 0
// This line is disabled due to JDK-8249779. Instead we break out of the loop
// and don't attempt to find any more mach-o files in this segment.
fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize;
#else
break;
#endif
}
}
return true;
err:
return false;
}
bool read_macho64_header(int fd, mach_header_64* core_header) {
bool is_macho = false;
if (fd < 0) return false;
off_t pos = ltell(fd);
lseek(fd, 0, SEEK_SET);
if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
is_macho = false;
} else {
is_macho = (core_header->magic == MH_MAGIC_64 || core_header->magic == MH_CIGAM_64);