-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphat.c
2025 lines (1821 loc) · 58.6 KB
/
phat.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
/*
parse java hprof results file
Copyright (C) Rich Coe 2011
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined(__sun)
#include <sys/byteorder.h>
#define bswap_16(x) BSWAP_16(x)
#define bswap_32(x) BSWAP_32(x)
#define bswap_64(x) BSWAP_64(x)
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#ifdef _LITTLE_ENDIAN
#define __BYTE_ORDER __LITTLE_ENDIAN
#else
#define __BYTE_ORDER __BIG_ENDIAN
#endif
#elif defined(__GNUC__)
#include <endian.h>
#include <byteswap.h>
#endif
#include <time.h>
#include <unistd.h>
#include <zlib.h>
#include "talloc.h"
#include "rbtree.h"
#if !defined(__sun)
#define _FILE_OFFSET_BITS 64
#endif
#define MAGIC_HEADER 0x4a415641 // JAVA
struct jdump { // java dump
FILE *fin;
int fVersion;
unsigned int identsz;
trbt_tree_t *sbTable, // string const table
*cTable, // class table, by ident
*hTable, // object table
*rsbTable, // revers string table, by name
*rcTable, // reverse class table, by name
*scTable, // class table, by serial
*roots; // root objects
struct _cinfo *javaLangClass, *javaLangString, *javaLangClassLoader;
char *fclass;
int plimit;
};
struct _arc {
struct _cinfo *parent; // source vertice of arc
struct _cinfo *child; // dest vertice of arc
struct _arc *next_parent, *next_child;
int has_placed;
long count;
long size; // size inherited along arc
long child_size; // child-size inherited along arc
};
typedef struct _arc Arc;
Arc ** arcs = NULL;
long arcs_num = 0, arcs_max = 0;
union hvalue {
long long ident;
unsigned char b;
unsigned short c;
unsigned int i;
unsigned long long j;
float f;
double d;
};
struct _hobject { // heap object
#define H_INSTANCE 0x01
#define H_VARRAY 0x02
#define H_OARRAY 0x03
#define H_CYCLE 0x04
int htype;
long long instId, classId;
long fpos;
long xclassId;
char resolved, visit;
unsigned int count;
int size;
unsigned long osize, csize;
union hvalue *hvalues;
};
typedef struct _hobject hobject;
struct _finfo { // Field Info
long long ident; // field identifier
char *name; // [decoded] field name
char ftype; // field type
char resolved; // passed through resolver
int valsz; // sizeof object
off_t offset; // for resolved objects
};
typedef struct _finfo finfo;
struct _cinfo { // Class Info
long long ident, nident;
char *name;
unsigned long count;
long long superId, loaderId, signerId, domainId;
unsigned short cstats, cfields;
int tfields;
char resolved;
hobject *statics;
finfo *fields; // this class's fields, self
finfo **values; // this class's value fields, self + super(s)
// map graph values
int index;
unsigned long size, child_size;
Arc *parents; // list of parent arcs
Arc *children; // list of child arcs
int cyc_num;
struct _cinfo *cyc_head, *cyc_next;
#define MG_DFN_NAN 0
#define MG_DFN_BUSY -1
int top_order;
char print_flag;
};
typedef struct _cinfo cinfo;
struct _rinfo {
long long ident;
long long ref;
int rtype;
#define ROOT_NATIVE_STATIC 0x1
#define ROOT_NATIVE_LOCAL 0x2
#define ROOT_JAVA_LOCAL 0x3
#define ROOT_NATIVE_STACK 0x4
#define ROOT_SYSTEM_CLASS 0x5
#define ROOT_THREAD_BLOCK 0x6
char *desc;
};
typedef struct _rinfo rinfo;
long fakeClass = 1000;
int readVersion(FILE *fin);
int readUint(FILE *fin, unsigned int *input);
int readUlong(FILE *fin, unsigned long long *input);
long long readIdent(struct jdump *);
struct jdump *readDump(char *findclass, int limit, char *dumpfile);
void readHeap(struct jdump *, unsigned int hsize);
char *hideSpecials(char *);
unsigned long resolveInstance(struct jdump *jf, hobject *ho);
unsigned long hashKey(char *key);
cinfo * findClass(struct jdump *jf, char *cname);
void printString(struct jdump *jf, hobject *ho);
Arc * arc_lookup(struct jdump *jf, cinfo *parent, cinfo *child);
void arc_add(struct jdump *jf, hobject *parent, hobject *child, long count);
void mg_assemble(struct jdump *);
int debug = 0;
extern int optind;
rinfo *
mkrinfo(trbt_tree_t *tab, long long rid, long long refid, int rtype, char *desc)
{
rinfo *ri = (rinfo *) talloc(tab, rinfo);
ri->ident = rid;
ri->ref = refid;
ri->rtype = rtype;
ri->desc = strdup(desc);
return ri;
}
cinfo *
mkcinfo(trbt_tree_t *tab, long long cid, long long nid, char *name)
{
cinfo *ci = (cinfo *) talloc(tab, cinfo);
ci->ident = cid;
ci->nident = nid;
ci->name = name;
ci->tfields = ci->resolved = ci->count = 0;
return ci;
}
int
main(int argc, char **argv)
{
int opt, limit = 0;
char *baseline = NULL;
char *findclass = NULL;
struct jdump *df, *bf;
while (-1 != (opt = getopt(argc, argv, "ab:C:dl:"))) {
switch (opt) {
case 'a': findclass = strdup("*"); break;
case 'b': baseline = strdup(optarg); break;
case 'd': debug++; break;
case 'C': findclass = strdup(optarg); break;
case 'l': limit = atoi(optarg); break;
default:
printf("opt %d\n");
break;
}
}
df = readDump(findclass, limit, argv[optind]);
if (baseline) {
bf = readDump(NULL, limit, baseline);
}
exit(0);
}
struct jdump *
readDump(char *fclass, int plimit, char *dumpfile)
{
struct jdump *df;
int rc;
unsigned int magic;
unsigned long long date;
time_t tdate;
struct tm *ltime;
char rtype;
df = (struct jdump *) malloc(sizeof(struct jdump));
df->fclass = fclass;
df->plimit = plimit;
if (NULL == (df->fin = fopen(dumpfile, "r"))) {
fprintf(stderr, "cannot open '%s' for reading, errno %d\n", dumpfile, errno);
exit(1);
}
// magic number
if (rc = readUint(df->fin, &magic)) {
fprintf(stderr, "cannot read header from '%s' for reading, errno %d\n", dumpfile, errno);
exit(2);
}
if (MAGIC_HEADER != magic) {
fprintf(stderr, "header 0x%x from '%s' does not match 0x%x\n", magic, dumpfile, MAGIC_HEADER);
exit(2);
}
// profile version
if (-1 == (df->fVersion = readVersion(df->fin))) {
fprintf(stderr, "unknown version\n");
exit(3);
}
// data
if (rc = readUint(df->fin, &df->identsz)) {
fprintf(stderr, "cannot read ident size from '%s' for reading, errno %d\n", dumpfile, errno);
exit(2);
}
if (4 != df->identsz && 8 != df->identsz) {
fprintf(stderr, "invalid identifer size %d\n", df->identsz);
exit(4);
}
if (rc = readUlong(df->fin, &date)) {
fprintf(stderr, "cannot read date from '%s' for reading, errno %d\n", dumpfile, errno);
exit(2);
}
tdate = date / 1000;
ltime = localtime(&tdate);
printf("Dump file created %s\n\n", ctime(&tdate));
df->sbTable = trbt_create(NULL, 0); // table of strings
// df->rsbTable = trbt_create(NULL, 0); // reverse lookup of sbTable
df->cTable = trbt_create(NULL, 0); // table of classes
df->rcTable = trbt_create(NULL, 0); // reverse lookup of cTable
df->scTable = trbt_create(NULL, 0); // table of classes by serial
df->hTable = trbt_create(NULL, 0); // table of heap objs
df->roots = trbt_create(NULL, 0); // table of root ids
while (EOF != (rtype = getc(df->fin))) {
unsigned int rlen, ts, pos;
pos = ftell(df->fin) - 1;
if (rc = readUint(df->fin, &ts)) {
puts("");
fprintf(stderr, "cannot read timestamp from '%s' for reading, errno %d\n", dumpfile, errno);
exit(2);
}
if (rc = readUint(df->fin, &rlen)) {
puts("");
fprintf(stderr, "cannot read record length type 0x%x from '%s' for reading, errno %d\n", rtype, dumpfile, errno);
exit(2);
}
if (debug > 2)
printf("0x%08x Read record 0x%02x : 0x%08x\t", pos, rtype, rlen);
switch ((unsigned char) rtype) {
case 0x01 : { // HPROF_UTF8
unsigned long skey = crc32(0, Z_NULL, 0);;
long long ident = readIdent(df);
int slen = rlen - df->identsz;
char *usb = talloc_zero_array(df->sbTable, char, 1 + rlen - df->identsz);
rc = fread(usb, 1, slen, df->fin);
usb = hideSpecials(usb);
if (debug)
printf("0x%8x %s\n", ident, usb);
trbt_insert32(df->sbTable, (long) ident, usb);
// trbt_insert32(df->rsbTable, crc32(skey, usb, slen), (void *) ident);
break;
}
case 0x02 : { // HPROF_LOAD_CLASS
unsigned long ckey = crc32(0, Z_NULL, 0);
long key, nkey;
cinfo *ci;
int serial, stackNum;
long long classId, classNameId;
readUint(df->fin, &serial); // serialNumber
classId = readIdent(df); // classId
readUint(df->fin, &stackNum); // stackTraceSerialNumber
classNameId = readIdent(df); // classNameID
// name = getNameFromID(classNameId);
// classNameFromObjectID
// classNameFromSerialNumber
key = (long) classId;
nkey = (long) classNameId;
ci = mkcinfo(df->cTable, classId, classNameId, (char *) trbt_lookup32(df->sbTable, nkey));
trbt_insert32(df->cTable, key, ci);
trbt_insert32(df->scTable, serial, ci);
ckey = crc32(ckey, ci->name, strlen(ci->name));
trbt_insert32(df->rcTable, ckey, ci);
if (debug) {
if (4 < df->identsz)
printf("0x%016lx classId 0x%08x 0x%016lx %s\n", classId, serial, classNameId, ci->name);
else
printf("0x%08lx classId 0x%08x 0x%08lx %s\n", classId, serial, classNameId, ci->name);
}
break;
}
case 0x04 : { // HPROF_FRAME
long long id, mName, mSig, srcFile;
char *SmNam, *SmSig, *SsrcFile;
cinfo *ci;
int serial, lineno;
id = readIdent(df);
mName = readIdent(df); // methodName
mSig = readIdent(df); // methodSig
srcFile = readIdent(df); // sourceFile
readUint(df->fin, &serial); // classSer
readUint(df->fin, &lineno); // lineNumber
SmNam = (char *) trbt_lookup32(df->sbTable, (long) mName);
SmSig = (char *) trbt_lookup32(df->sbTable, (long) mSig);
SsrcFile = (char *) trbt_lookup32(df->sbTable, (long) srcFile);
ci = (cinfo *) trbt_lookup32(df->scTable, (long) serial);
printf("[%2d] frame %s %s serial 0x%08x %s %s %d\n", id, SmNam, SmSig, serial, ci->name, SsrcFile, lineno);
}
break;
case 0x05 : { // HPROF_TRACE
int i, serial, threadSeq, frames;
readUint(df->fin, &serial); // serialNumber
readUint(df->fin, &threadSeq);
readUint(df->fin, &frames);
if (1 || debug)
printf("trace serial %d tseq: %d %d\n", serial, threadSeq, frames);
for (i = 0; i < frames; i++) {
long long ident = readIdent(df);
if (3 < debug)
printf(" [%2d] %ld\n", i, ident);
}
break;
}
case 0x0c : { // HPROF_HEAP_DUMP
// printf("heap dump\n");
if (debug) puts("");
readHeap(df, rlen);
break;
}
default:
if (debug) puts("");
fseek(df->fin, rlen, SEEK_CUR);
break;
}
}
puts("");
}
char *
hideSpecials(char *in)
{
char *out = in;
while (*in) {
if ('\n' == *in || '\r' == *in || 0x20 > *in)
*in = '.';
in++;
}
return out;
}
int
readUshort(FILE *fin, unsigned short *input)
{
unsigned char sb[2];
int rc;
unsigned short val;
if (2 != (rc = fread(sb, 1, 2, fin))) {
if (0 != rc)
fprintf(stderr, "readUint: short read %d of 2 bytes\n", rc);
return -1;
}
#if __BYTE_ORDER == __BIGENDIAN
val = sb[1] << 8 | sb[0];
#else
val = sb[0] << 8 | sb[1];
#endif
*input = val;
return 0;
}
int
readUint(FILE *fin, unsigned int *input)
{
unsigned char sb[4];
int rc;
unsigned int val;
if (4 != (rc = fread(sb, 1, 4, fin))) {
if (0 != rc)
fprintf(stderr, "readUint: short read %d of 4 bytes\n", rc);
return -1;
}
#if __BYTE_ORDER == __BIGENDIAN
val = ((sb[3] << 8 | sb[2]) << 8 | sb[1]) << 8 | sb[0];
#else
val = ((sb[0] << 8 | sb[1]) << 8 | sb[2]) << 8 | sb[3];
#endif
*input = val;
return 0;
}
int
readUlong(FILE *fin, unsigned long long *input)
{
unsigned char sb[8];
int rc;
unsigned long long val;
if (8 != (rc = fread(sb, 1, 8, fin))) {
if (0 != rc)
fprintf(stderr, "readUint: short read %d of 8 bytes\n", rc);
return -1;
}
#if __BYTE_ORDER == __BIGENDIAN
val = ((sb[3] << 8 | sb[2]) << 8 | sb[1]) << 8 | sb[0];
val = (val << 32) + ((sb[3] << 8 | sb[2]) << 8 | sb[1]) << 8 | sb[0];
#else
val = ((sb[0] << 8 | sb[1]) << 8 | sb[2]) << 8 | sb[3];
val = (val << 32) + ((sb[4] << 8 | sb[5]) << 8 | sb[6]) << 8 | sb[7];
#endif
*input = val;
return 0;
}
long long
readIdent(struct jdump *jf)
{
unsigned long long lval;
if (4 == jf->identsz) {
unsigned int val;
readUint(jf->fin, &val);
return (long long) val;
}
readUlong(jf->fin, &lval);
return (long long) lval;
}
hobject *
makeObj(trbt_tree_t *tab, int ht, long long iid, long long cid)
{
hobject *ho = (hobject *) talloc(tab, hobject);
ho->htype = ht;
ho->classId = cid;
ho->instId = iid;
ho->fpos = 0;
ho->resolved = ho->visit = 0;
ho->size = ho->csize = 0;
ho->xclassId = 0;
return ho;
}
int
readVersion(FILE *fin)
{
char sb[40];
int ch, len = 0;
while (EOF != (ch = getc(fin))) {
sb[len++] = ch;
if (0 == ch)
break;
if (sizeof(sb) < len + 2) {
sb[1+len] = 0;
break;
}
}
if (0 < len) {
if (0 == strcmp(sb, " PROFILE 1.0.2"))
return 2;
if (0 == strcmp(sb, " PROFILE 1.0.1"))
return 1;
if (0 == strcmp(sb, " PROFILE 1.0"))
return 0;
}
return -1;
}
unsigned char
sigFromType(unsigned char otype)
{
switch (otype) {
case 2 /* T_CLASS */ : return 'L';
case 4 /* T_BOOLEAN */ : return 'Z';
case 5 /* T_CHAR */ : return 'C';
case 6 /* T_FLOAT */ : return 'F';
case 7 /* T_DOUBLE */ : return 'D';
case 8 /* T_BYTE */ : return 'B';
case 9 /* T_SHORT */ : return 'S';
case 10 /* T_INT */ : return 'I';
case 11 /* T_LONG */ : return 'J';
}
return 'U';
}
int
readValue(struct jdump *jf, hobject *ho)
{
ho->htype = getc(jf->fin);
if (1 <= jf->fVersion)
ho->htype = sigFromType(ho->htype);
switch (ho->htype) {
case '[' :
case 'L' : {
ho->hvalues[0].ident = readIdent(jf);
return jf->identsz;
}
case 'Z' : {
ho->hvalues[0].b = getc(jf->fin);
if (0 != ho->hvalues[0].b && 1 != ho->hvalues[0].b) {
fprintf(stderr, "readValue: illegal bool read 0x%x\n", ho->hvalues[0].b);
return -1;
}
return 1;
}
case 'B' : {
ho->hvalues[0].b = getc(jf->fin);
return 1;
}
case 'S' : {
readUshort(jf->fin, &(ho->hvalues[0].c));
return 2;
}
case 'C' : {
readUshort(jf->fin, &(ho->hvalues[0].c));
return 2;
}
case 'I' : {
readUint(jf->fin, &(ho->hvalues[0].i));
return 4;
}
case 'J' : {
readUlong(jf->fin, &(ho->hvalues[0].j));
return 8;
}
case 'F' : { // float
ho->hvalues[0].f = 0.0;
fseek(jf->fin, 4, SEEK_CUR);
return 4;
}
case 'D' : { // double
ho->hvalues[0].d = 0.0;
fseek(jf->fin, 8, SEEK_CUR);
return 8;
}
default:
fprintf(stderr, "readValue: bad type sig 0x%x\n", ho->htype);
break;
}
return 0;
}
int
readClass(struct jdump *jf, unsigned int hsize)
{
long long ident, res1, res2;
unsigned int stackId, instsz;
unsigned short cpool;
cinfo *ci;
int i;
ident = readIdent(jf);
ci = (cinfo *) trbt_lookup32(jf->cTable, (long) ident);
readUint(jf->fin, &stackId);
ci->superId = readIdent(jf);
ci->loaderId = readIdent(jf);
ci->signerId = readIdent(jf);
ci->domainId = readIdent(jf);
res1 = readIdent(jf);
res2 = readIdent(jf);
readUint(jf->fin, &instsz);
hsize -= (7 * jf->identsz) + 8;
readUshort(jf->fin, &cpool); // const pool entries
hsize -= 2;
for (i = 0; i < cpool; i++) {
hobject sho;
union hvalue hv;
unsigned short entry;
sho.hvalues = &hv;
readUshort(jf->fin, &entry);
hsize -= 2;
hsize -= readValue(jf, &sho);
}
readUshort(jf->fin, &(ci->cstats)); // statics
hsize -= 2;
if (0 < ci->cstats) {
ci->statics = talloc_array(ci, hobject, ci->cstats);
for (i = 0; i < ci->cstats; i++) {
ci->statics[i].instId = readIdent(jf);
ci->statics[i].resolved = 0;
ci->statics[i].hvalues = talloc(ci, union hvalue);
hsize -= jf->identsz;
hsize -= 1 + readValue(jf, ci->statics + i);
}
}
readUshort(jf->fin, &(ci->cfields)); // fields
hsize -= 2;
if (0 < ci->cfields) {
ci->fields = talloc_array(ci, finfo, ci->cfields);
for (i = 0; i < ci->cfields; i++) {
ci->fields[i].ident = readIdent(jf);
ci->fields[i].ftype = getc(jf->fin);
ci->fields[i].resolved = 0;
if (1 <= jf->fVersion)
ci->fields[i].ftype = sigFromType(ci->fields[i].ftype);
hsize -= 1 + jf->identsz;
}
}
if (debug)
printf("0x%x class %4d %4d %4d %s\n", ident, cpool, ci->cstats, ci->cfields, ci->name);
// putchar('c');
return hsize;
}
int
readArray(struct jdump *jf, unsigned hsize, int prim)
{
long long ide, elemClassId;
unsigned int stackId, isz;
char primSig = 0x00;
int elsz;
hobject *ho;
ide = readIdent(jf);
readUint(jf->fin, &stackId);
readUint(jf->fin, &isz);
hsize -= 8 + jf->identsz;
if (prim) {
elemClassId = getc(jf->fin);
hsize -= 1;
if (debug)
printf("0x%x primary array type %2d %s\n", ide, elemClassId, "");
} else {
elemClassId = readIdent(jf);
hsize -= jf->identsz;
if (debug) {
cinfo *ci = (cinfo *) trbt_lookup32(jf->cTable, elemClassId);
printf("0x%x object array type %x %s\n", ide, elemClassId, ci->name);
}
}
if (prim /* || version < 1 */) {
switch (elemClassId) {
case 4 /* T_BOOLEAN */: primSig = 'Z'; elsz = 1; break;
case 5 /* T_CHAR */: primSig = 'C'; elsz = 2; break;
case 6 /* T_FLOAT */: primSig = 'F'; elsz = 4; break;
case 7 /* T_DOUBLE */: primSig = 'D'; elsz = 8; break;
case 8 /* T_BYTE */: primSig = 'B'; elsz = 1; break;
case 9 /* T_SHORT */: primSig = 'S'; elsz = 2; break;
case 10 /* T_INT */: primSig = 'I'; elsz = 4; break;
case 11 /* T_LONG */: primSig = 'J'; elsz = 8; break;
}
}
if (0x00 != primSig) {
cinfo *ci;
char *cname = talloc_zero_array(jf->sbTable, char, 3);
cname[0] = '[';
cname[1] = primSig;
if (NULL == (ci = findClass(jf, cname))) {
ci = mkcinfo(jf->cTable, fakeClass++, elemClassId, cname);
trbt_insert32(jf->sbTable, (long) elemClassId, cname);
trbt_insert32(jf->cTable, fakeClass, ci);
trbt_insert32(jf->rcTable, hashKey(cname), ci);
}
hsize -= elsz * isz;
ho = makeObj(jf->hTable, H_VARRAY, ide, ci->ident);
ho->fpos = ftello(jf->fin);
ho->size = elsz;
ho->count = isz;
fseek(jf->fin, elsz * isz, SEEK_CUR);
} else {
cinfo *ci;
char *cname;
ci = (cinfo *) trbt_lookup32(jf->cTable, elemClassId);
cname = talloc_zero_array(jf->sbTable, char, 2 + strlen(ci->name));
// *cname = '[';
strcat(cname, ci->name);
if (NULL == (ci = findClass(jf, cname))) {
ci = mkcinfo(jf->cTable, fakeClass++, elemClassId, cname);
trbt_insert32(jf->sbTable, (long) elemClassId, cname);
trbt_insert32(jf->cTable, fakeClass, ci);
trbt_insert32(jf->rcTable, hashKey(cname), ci);
}
hsize -= jf->identsz * isz;
ho = makeObj(jf->hTable, H_OARRAY, ide, ci->ident);
ho->fpos = ftello(jf->fin);
ho->size = jf->identsz;
ho->count = isz;
fseek(jf->fin, jf->identsz * isz, SEEK_CUR);
}
trbt_insert32(jf->hTable, ide, ho);
return hsize;
}
void
printClass_r(trbt_node_t *node)
{
cinfo *ci;
if (NULL == node)
return;
printClass_r(node->left);
ci = (cinfo *) node->data;
printf("0x%x class 0x%x %d %s\n", ci->ident, ci->nident, ci->count, ci->name);
printClass_r(node->right);
}
void
printClass(trbt_node_t *node)
{
trbt_node_t **stack = (trbt_node_t **) malloc(sizeof(trbt_node_t *) * 20);
int curr = 0, size = 20;
while (node) {
cinfo *ci = (cinfo *) node->data;
printf("0x%x instance 0x%x %d %s\n", ci->ident, ci->nident, ci->count, ci->name);
if (node->right) {
if (curr == size) {
stack = (trbt_node_t **) realloc(stack, sizeof(trbt_node_t *) * (size + 20));
size += 20;
}
stack[curr++] = node->right;
}
if (node->left) {
node = node->left;
continue;
}
if (0 < curr)
node = stack[--curr];
else
node = NULL;
}
}
unsigned long
hashKey(char *key)
{
unsigned long ckey = crc32(0, Z_NULL, 0);
return crc32(ckey, key, strlen(key));
}
cinfo *
findClass(struct jdump *jf, char *cname)
{
return (cinfo *) trbt_lookup32(jf->rcTable, hashKey(cname));
}
int
resolveSClassSize(struct jdump *jf, cinfo *ci)
{
cinfo *super;
if (0 == ci->superId)
return 0;
super = (cinfo *) trbt_lookup32(jf->cTable, ci->superId);
return ci->cfields + resolveSClassSize(jf, super);
}
void
resolveField(struct jdump *jf, hobject *fi)
{
if (fi->resolved)
return;
#ifdef notdef
fi->name = (char *) trbt_lookup32(jf->sbTable, (long) fi->ident);
switch (fi->ftype) {
case 'L':
case '[':
}
#endif
fi->resolved = 1;
}
cinfo *
getSuperClass(struct jdump *jf, cinfo *ci)
{
return (cinfo *) trbt_lookup32(jf->cTable, ci->superId);
}
finfo *
copyField(cinfo *parent, finfo *src)
{
finfo *dest = (finfo *) talloc(parent, finfo);
memcpy(dest, src, sizeof(finfo));
return dest;
}
void
resolveClassNode(struct jdump *jf, cinfo *ci)
{
cinfo *super;
int i;
if (ci->resolved)
return;
ci->tfields = resolveSClassSize(jf, ci);
// loader
// signers
// domain
for (i = 0; i < ci->cstats; i++)
resolveField(jf, ci->statics + i);
if (0 != (super = getSuperClass(jf, ci)))
resolveClassNode(jf, super);
if (0 < ci->tfields) {
int target, index, fieldNo;
cinfo *cc = ci;
ci->values = talloc_array(ci, finfo *, ci->tfields);
target = ci->tfields - cc->cfields;
fieldNo = index = 0;
for (i = 0; i < ci->tfields; i++, fieldNo++) {
finfo *field, **value;
while (fieldNo >= cc->cfields) {
cc = getSuperClass(jf, cc);
fieldNo = 0;
target -= cc->cfields;
}
value = ci->values + target + fieldNo;
if (cc == ci) {
field = *value = cc->fields + fieldNo;
field->name = (char *) trbt_lookup32(jf->sbTable, (long) field->ident);
} else
field = *value = copyField(ci, cc->fields + fieldNo);
field->offset = index;
switch (field->ftype) {
case 'L':
case '[': index += jf->identsz; break;
case 'Z':
case 'B': index += 1; break;
case 'S':
case 'C': index += 2; break;
case 'F':
case 'I': index += 4; break;
case 'D':
case 'J': index += 8; break;
}
}
ci->size = index;
}
ci->resolved = 1;
}
void
resolveClass(struct jdump *jf, trbt_node_t *cnode)
{
if (NULL == cnode)
return;
resolveClass(jf, cnode->left);
resolveClassNode(jf, (cinfo *) cnode->data);
resolveClass(jf, cnode->right);
}
void
resolveInstances(struct jdump *jf, trbt_node_t *cnode)
{
if (NULL == cnode)
return;
resolveInstances(jf, cnode->left);
resolveInstance(jf, (hobject *) cnode->data);
resolveInstances(jf, cnode->right);
}
void
resolveClasses(struct jdump *jf)
{
jf->javaLangClass = findClass(jf, "java/lang/Class");
jf->javaLangClassLoader = findClass(jf, "java/lang/ClassLoader");
resolveClass(jf, jf->cTable->root);
resolveInstances(jf, jf->hTable->root);
}
unsigned long
resolveInstance(struct jdump *jf, hobject *ho)
{
cinfo *ci;
unsigned long size = 0;
int i;
if (ho->resolved)
return ho->osize + ho->csize;
ho->resolved = 1;
ci = (cinfo *) trbt_lookup32(jf->cTable, ho->classId);
if (!ci->resolved)
resolveClassNode(jf, ci);
if (H_VARRAY == ho->htype) {
ho->hvalues = talloc_array(ho, union hvalue, ho->count);
if (0 == ho->count)
return 0;
fseeko(jf->fin, ho->fpos, SEEK_SET);
if (12 < ho->classId) {
switch (*(ci->name + 1)) {
case 'B': ho->xclassId = 8; break;
case 'Z': ho->xclassId = 8; break;
case 'C': ho->xclassId = 9; break;
case 'S': ho->xclassId = 9; break;
case 'F': ho->xclassId = 6; break;
case 'D': ho->xclassId = 7; break;
case 'I': ho->xclassId = 10; break;
case 'J': ho->xclassId = 11; break;
}
}
switch (ho->xclassId ? ho->xclassId : ho->classId) {
case 4: case 8: { // BOOLEAN, BYTE
char *b = (char *) ho->hvalues;
fread(b, sizeof(char), ho->count, jf->fin);
size = ho->count;
break;
}
case 5: case 9: { // CHAR, SHORT
unsigned short *c = (unsigned short *) ho->hvalues;
fread(c, sizeof(unsigned short), ho->count, jf->fin);
size = 2 * ho->count;
break;
}