Repository navigation
Expand file tree
/
Copy pathheap_tcmalloc.cpp
More file actions
1659 lines (1438 loc) · 39.9 KB
/
Copy pathheap_tcmalloc.cpp
File metadata and controls
1659 lines (1438 loc) · 39.9 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
/*
* heap_tcmalloc.c
*
* Created on: August 27, 2016
* Author: myan
*
* This Implementation uses gdb specific types. Hence not portable to non-Linux
* platforms
*/
#include "segment.h"
#include "heap_tcmalloc.h"
#define CA_DEBUG 0
#if CA_DEBUG
#define CA_PRINT_DBG CA_PRINT
#else
#define CA_PRINT_DBG(format,args...)
#endif
/*
* Internal data structures
*/
struct span_stats {
int sizeclass;
size_t span_count;
size_t inuse_count;
size_t free_count;
size_t inuse_bytes;
size_t free_bytes;
};
/*
* Globals
*/
//static int tc_version_major = 2;
static int tc_version_minor = 0;
//static int tc_version_patch = 0;
static bool g_initialized = false;
static struct ca_config g_config;
static struct ca_span *g_spans;
static unsigned long g_spans_capacity;
static unsigned long g_spans_count;
static unsigned long skip_npage;
static address_t *g_cached_blocks;
static unsigned long g_cached_blocks_capacity;
static unsigned long g_cached_blocks_count;
/*
* Forward declaration
*/
static bool gdb_symbol_prelude(void);
static int type_field_name2no(struct type *, const char *);
static struct value *get_field_value(struct value *, const char *);
static bool parse_config(void);
static bool parse_pagemap(void);
static bool parse_thread_cache(void);
static bool parse_central_cache(void);
static bool parse_leaf(struct value *, struct type *);
static bool parse_span(struct value *);
static bool parse_thread_cache_lists(struct value *);
static bool parse_central_freelist(struct value *);
static bool parse_central_freelist_tcentry(struct value *, bool *);
static bool cached_block_add(address_t);
static int cached_block_compare(const void *, const void *);
static int cached_block_search_compare(const void *, const void *);
static bool verify_sorted_cached_blocks(void);
static bool is_block_cached(address_t);
static struct ca_span *span_get(address_t);
static int span_search_compare(const void *, const void *);
static bool verify_sorted_spans(void);
static bool span_block_free(struct ca_span*, address_t);
static bool span_populate_free_bitmap(struct ca_span*);
static void span_get_stat(struct ca_span *, struct span_stats *);
static void span_walk(struct ca_span *);
static void add_one_big_block(struct heap_block *, unsigned int,
struct heap_block *);
/******************************************************************************
* Exposed functions
*****************************************************************************/
static const char *
heap_version(void)
{
return "TCmalloc";
}
static bool
init_heap(void)
{
unsigned long i;
/*
* Start with a clean slate
*/
g_initialized = false;
if (g_config.sizemap.class_to_pages != NULL)
free(g_config.sizemap.class_to_pages);
if (g_config.sizemap.class_to_size != NULL)
free(g_config.sizemap.class_to_size);
if (g_config.sizemap.num_objects_to_move != NULL)
free(g_config.sizemap.num_objects_to_move);
memset(&g_config, 0, sizeof(g_config));
for (i = 0; i < g_spans_count; i++) {
struct ca_span *span = &g_spans[i];
if (span->bitmap != NULL)
free(span->bitmap);
memset(span, 0, sizeof *span);
}
g_spans_count = 0;
skip_npage = 0;
g_cached_blocks_count = 0;
/* Trigger gdb symbol resolution */
gdb_symbol_prelude();
if (parse_config() == false ||
parse_pagemap() == false ||
parse_thread_cache() == false ||
parse_central_cache() == false) {
return false;
}
qsort(g_cached_blocks, g_cached_blocks_count, sizeof(*g_cached_blocks),
cached_block_compare);
if (verify_sorted_cached_blocks() == false ||
verify_sorted_spans() == false) {
return false;
}
/*
* Show result
*/
#if CA_DEBUG
CA_PRINT_DBG("%ld Spans are found\n", g_spans_count);
for (i = 0; i < g_spans_count; i++) {
struct ca_span *span = &g_spans[i];
CA_PRINT_DBG("[%ld] {\n"
"\tstart %ld\n"
"\tlength %ld\n"
"\tnext %#lx\n"
"\tprev %#lx\n"
"\tobjects %#lx\n"
"\trefcount %d\n"
"\tsizeclass %d\n"
"\tlocation %d\n"
"\tsample %d\n"
"}\n",
i, span->start, span->length, span->next, span->prev,
span->objects, span->refcount, span->sizeclass,
span->location, span->sample);
}
CA_PRINT_DBG("thread/central cached blocks %ld\n", g_cached_blocks_count);
CA_PRINT_DBG("tcmalloc heap is initialized successfully\n");
#endif
g_initialized = true;
return true;
}
static bool
get_heap_block_info(address_t addr, struct heap_block* blk)
{
struct ca_span *span;
address_t base;
if (g_initialized == false) {
CA_PRINT("tcmalloc heap was not initialized successfully\n");
return false;
}
/*
* No span means the address is not managed by tcmalloc
*/
span = span_get(addr);
if (span == NULL)
return false;
/*
* The whole span is free
*/
if (span->location != SPAN_IN_USE) {
blk->inuse = false;
blk->addr = span->start << g_config.kPageShift;
blk->size = span->length << g_config.kPageShift;
return true;
}
/*
* Block size by class
*/
if (span->sizeclass != 0)
blk->size = g_config.sizemap.class_to_size[span->sizeclass];
else
blk->size = span->length << g_config.kPageShift;
/*
* Block address is on fixed-size boundary
*/
base = span->start << g_config.kPageShift;
blk->addr = addr - ((addr - base) % blk->size);
/*
* Block status needs to consult span's object list and all cache lists
*/
span_populate_free_bitmap(span);
if (span_block_free(span, blk->addr) == true)
blk->inuse = false;
else
blk->inuse = true;
return true;
}
static bool
get_next_heap_block(address_t addr, struct heap_block* blk)
{
struct ca_span *span, *last_span, *next;
unsigned long pageid;
if (g_initialized == false) {
CA_PRINT("tcmalloc heap was not initialized successfully\n");
return false;
}
if (addr == 0) {
if (g_spans_count == 0) {
CA_PRINT("There is not heap block\n");
return false;
}
/*
* Return the first block with lowest address
*/
span = &g_spans[0];
} else {
span = span_get(addr);
if (span == NULL) {
CA_PRINT("The input address %#lx doesn't belong to "
"the heap\n", addr);
return false;
}
if (span->location == SPAN_IN_USE && span->sizeclass != 0) {
size_t blk_sz = g_config.sizemap.class_to_size[span->sizeclass];
address_t base = span->start << g_config.kPageShift;
unsigned int index = (addr - base) / blk_sz;
unsigned n, bit;
if (index < span->count -1 ) {
index++;
blk->addr = base + index * blk_sz;
blk->size = blk_sz;
n = index / UINT_BITS;
bit = index - n * UINT_BITS;
blk->inuse = !(span->bitmap[n] & (1 << bit));
return true;
}
}
/*
* The next block is in the next span
*/
last_span = &g_spans[g_spans_count - 1];
next = NULL;
for (pageid = span->start + span->length;
pageid <= last_span->start;
pageid++) {
next = span_get(pageid << g_config.kPageShift);
if (next != NULL)
break;
}
if (next == NULL)
return false;
span = next;
}
span_populate_free_bitmap(span);
blk->addr = span->start << g_config.kPageShift;
if (span->location != SPAN_IN_USE) {
blk->size = span->length << g_config.kPageShift;
blk->inuse = false;
} else if (span->sizeclass == 0) {
blk->size = span->length << g_config.kPageShift;
blk->inuse = true;
} else {
blk->size = g_config.sizemap.class_to_size[span->sizeclass];
blk->inuse = !(span->bitmap[0] & 1);
}
return true;
}
/* Return true if the block belongs to a heap */
static bool
is_heap_block(address_t addr)
{
if (g_initialized == false) {
CA_PRINT("tcmalloc heap was not initialized successfully\n");
return false;
}
return span_get(addr) != NULL;
}
/*
* Traverse all spans unless a non-zero address is given, in which case the
* specific span is walked
*/
static bool
heap_walk(address_t heapaddr, bool verbose)
{
unsigned int i;
struct span_stats *stats;
struct span_stats total;
size_t blk_sz;
struct ca_span *span;
if (g_initialized == false) {
CA_PRINT("tcmalloc heap was not initialized successfully\n");
return false;
}
/*
* Display all blocks belonging to the span of the given address
*/
if (heapaddr) {
span = span_get(heapaddr);
if (span == NULL) {
CA_PRINT("Address %#lx doesn't belong to tcmalloc "
"heap\n", heapaddr);
return false;
}
span_walk(span);
return true;
}
/*
* Full heap walk
*/
stats = (struct span_stats *)calloc(g_config.kNumClasses + 1, sizeof *stats);
if (stats == NULL) {
CA_PRINT("Out of memory\n");
return false;
}
for (i = 0; i < g_config.kNumClasses; i++) {
stats[i].sizeclass = i;
}
stats[g_config.kNumClasses].sizeclass = -1;
/*
* Collect statistics of all spans
*/
for (i = 0; i < g_spans_count; i++) {
span = &g_spans[i];
if (span->location == SPAN_IN_USE) {
span_get_stat(span, &stats[span->sizeclass]);
} else {
span_get_stat(span, &stats[g_config.kNumClasses]);
}
}
memset(&total, 0, sizeof(total));
/*
* Display statistics
*/
CA_PRINT(" size_class num_spans block_size inuse_blks inuse_bytes free_blks free_bytes\n");
for (i = 0; i < g_config.kNumClasses + 1; i++) {
if (i == 0) {
CA_PRINT(" (large)0%12zu n/a", stats[i].span_count);
}
else if (i == g_config.kNumClasses)
CA_PRINT(" (free)%12zu n/a", stats[i].span_count);
else {
blk_sz = g_config.sizemap.class_to_size[i];
CA_PRINT("%12d%12zu%12zu", i, stats[i].span_count, blk_sz);
}
if (stats[i].span_count != 0) {
CA_PRINT("%12zu%12zu%12zu%12zu\n",
stats[i].inuse_count, stats[i].inuse_bytes,
stats[i].free_count, stats[i].free_bytes);
}
else {
CA_PRINT("\n");
}
total.span_count += stats[i].span_count;
total.inuse_count += stats[i].inuse_count;
total.inuse_bytes += stats[i].inuse_bytes;
total.free_count += stats[i].free_count;
total.free_bytes += stats[i].free_bytes;
}
CA_PRINT("------------------------------------------------------------------------------------\n");
CA_PRINT(" Total");
CA_PRINT("%12zu %12zu%12zu%12zu%12zu\n",
total.span_count, total.inuse_count, total.inuse_bytes,
total.free_count, total.free_bytes);
free(stats);
return true;
}
static bool
get_biggest_blocks(struct heap_block* blks, unsigned int num)
{
unsigned long i;
struct ca_span *span;
struct heap_block* smallest = &blks[num - 1];
struct heap_block blk;
if (g_initialized == false) {
CA_PRINT("tcmalloc heap was not initialized successfully\n");
return false;
}
if (num == 0)
return true;
memset(blks, 0, num * sizeof *blks);
/*
* Traverse big blocks (size class 0) first and populate the returned
* array
*/
for (i = 0; i < g_spans_count; i++) {
span = &g_spans[i];
if (span->location != SPAN_IN_USE)
continue;
if (span->sizeclass == 0) {
blk.size = span->length << g_config.kPageShift;
if (blk.size > smallest->size)
{
blk.addr = span->start << g_config.kPageShift;
blk.inuse = true;
add_one_big_block(blks, num, &blk);
}
}
}
if (smallest->size > 0)
return true;
/*
* If the queried number of largest blocks exceeds number of big blocks,
* continue to traverse normal blocks (size class != 0)
*/
for (i = 0; i < g_spans_count; i++) {
span = &g_spans[i];
if (span->location != SPAN_IN_USE)
continue;
if (span->sizeclass != 0) {
address_t base = span->start << g_config.kPageShift;
unsigned int index;
blk.size = g_config.sizemap.class_to_size[span->sizeclass];
if (blk.size <= smallest->size)
continue;
span_populate_free_bitmap(span);
for (index = 0; index < span->count; index++) {
unsigned int n, bit;
n = index / UINT_BITS;
bit = index - n * UINT_BITS;
if (!(span->bitmap[n] & (1 << bit)) &&
blk.size > smallest->size) {
blk.addr = base + index * blk.size;
blk.inuse = true;
add_one_big_block(blks, num, &blk);
}
}
}
}
return true;
}
static bool
walk_inuse_blocks(struct inuse_block* opBlocks, unsigned long* opCount)
{
unsigned long i;
struct ca_span *span;
if (g_initialized == false) {
CA_PRINT("tcmalloc heap was not initialized successfully\n");
return false;
}
*opCount = 0;
for (i = 0; i < g_spans_count; i++) {
span = &g_spans[i];
span_populate_free_bitmap(span);
if (span->location != SPAN_IN_USE)
continue;
if (span->sizeclass == 0) {
(*opCount)++;
if (opBlocks != NULL) {
opBlocks->addr = span->start << g_config.kPageShift;
opBlocks->size = span->length << g_config.kPageShift;
opBlocks++;
}
} else {
address_t base = span->start << g_config.kPageShift;
unsigned int index;
size_t blk_sz = g_config.sizemap.class_to_size[span->sizeclass];
for (index = 0; index < span->count; index++) {
unsigned int n, bit;
n = index / UINT_BITS;
bit = index - n * UINT_BITS;
if (!(span->bitmap[n] & (1 << bit))) {
(*opCount)++;
if (opBlocks != NULL) {
opBlocks->addr = base + index * blk_sz;
opBlocks->size = blk_sz;
opBlocks++;
}
}
}
}
}
return true;
}
CoreAnalyzerHeapInterface sTcMallHeapManager = {
heap_version,
init_heap,
heap_walk,
is_heap_block,
get_heap_block_info,
get_next_heap_block,
get_biggest_blocks,
walk_inuse_blocks,
};
void register_tc_malloc() {
bool my_heap = gdb_symbol_prelude();
return register_heap_manager("tc", &sTcMallHeapManager, my_heap);
}
/******************************************************************************
* Helper Functions
*****************************************************************************/
static void
add_one_big_block(struct heap_block *blks, unsigned int num,
struct heap_block *blk)
{
unsigned int i;
for (i = 0; i < num; i++) {
if (blk->size > blks[i].size) {
int k;
// Insert blk->blks[i]
// Move blks[i]->blks[i+1], .., blks[num-2]->blks[num-1]
for (k = ((int)num) - 2; k >= (int)i; k--)
blks[k+1] = blks[k];
blks[i] = *blk;
break;
}
}
}
bool
gdb_symbol_prelude(void)
{
struct symbol *pagemap2;
struct symbol *pagemap3;
/*
* template <int BITS>
* class TCMalloc_PageMap2, TCMalloc_PageMap3
*/
pagemap2 = lookup_symbol("TCMalloc_PageMap2<35>", 0, VAR_DOMAIN, 0).symbol;
pagemap3 = lookup_symbol("TCMalloc_PageMap3<35>", 0, VAR_DOMAIN, 0).symbol;
if (pagemap2 == NULL && pagemap3 == NULL) {
CA_PRINT_DBG("Failed to lookup type \"TCMalloc_PageMap2<35>\" and \"TCMalloc_PageMap3<35>\""
"\n");
return false;
}
return true;
}
struct ca_span *
span_get(address_t addr)
{
struct ca_span *span;
unsigned long pageid;
pageid = addr >> g_config.kPageShift;
span = (struct ca_span *)bsearch(&pageid, (void *)g_spans, g_spans_count,
sizeof(struct ca_span), span_search_compare);
return span;
}
bool
parse_config(void)
{
struct symbol *pageshift_;
struct symbol *sizemap_;
struct value *sizemap;
struct value *class_to_size;
LONGEST low_bound, high_bound, index;
/*
* Global var
* static const size_t kPageShift;
*/
pageshift_ = lookup_symbol("kPageShift", 0, VAR_DOMAIN, 0).symbol;
if (pageshift_ == NULL) {
CA_PRINT("Failed to lookup gv \"kPageShift\"\n");
return false;
}
g_config.kPageShift = value_as_long(value_of_variable(pageshift_, 0));
/*
* Global var
* tcmalloc::Static::sizemap_
*/
sizemap_ = lookup_global_symbol("tcmalloc::Static::sizemap_", 0,
VAR_DOMAIN).symbol;
if (sizemap_ == NULL) {
CA_PRINT("Failed to lookup gv "
"\"tcmalloc::Static::sizemap_\"\n");
return false;
}
sizemap = value_of_variable(sizemap_, 0);
/*
* tcmalloc::Static::sizemap_.class_to_size_
*/
class_to_size = get_field_value(sizemap, "class_to_size_");
if (!class_to_size)
return false;
if (ca_code(value_type(class_to_size)) != TYPE_CODE_ARRAY) {
CA_PRINT("Unexpected \"class_to_size\" is not an array\n");
return false;
}
if (get_array_bounds (value_type(class_to_size), &low_bound,
&high_bound) == 0) {
CA_PRINT("Could not determine \"class_to_size\" bounds\n");
return false;
}
g_config.kNumClasses = high_bound - low_bound + 1;
g_config.sizemap.class_to_size = (size_t *)calloc(g_config.kNumClasses,
sizeof(size_t));
g_config.sizemap.class_to_pages = (size_t *)calloc(g_config.kNumClasses,
sizeof(size_t));
g_config.sizemap.num_objects_to_move = (int *)calloc(g_config.kNumClasses,
sizeof(int));
if (g_config.sizemap.class_to_size == NULL ||
g_config.sizemap.class_to_pages == NULL ||
g_config.sizemap.num_objects_to_move == NULL) {
CA_PRINT("Out of memory\n");
return false;
}
/*
* tcmalloc::Static::sizemap_.class_to_size_[index]
*/
for (index = low_bound; index <= high_bound; index++) {
struct value *v;
v = value_subscript(class_to_size, index);
g_config.sizemap.class_to_size[index] = value_as_long(v);
}
return true;
}
static bool
parse_pagemap_2_5(struct symbol *pageheap_, struct type *leaf_type,
struct type *span_type)
{
struct value *pageheap_p, *pageheap;
struct value *pagemap;
struct value *root_p, *root;
struct value *ptrs;
LONGEST low_bound, high_bound, index;
/* struct symbol to struct value */
pageheap_p = value_of_variable(pageheap_, 0);
/* deref pointer value */
pageheap = value_ind(pageheap_p);
/*
* tcmalloc::Static::pageheap_->pagemap_
*/
pagemap = get_field_value(pageheap, "pagemap_");
/*
* tcmalloc::Static::pageheap_->pagemap_.root_
*/
root_p = get_field_value(pagemap, "root_");
root = value_ind(root_p);
/*
* tcmalloc::Static::pageheap_->pagemap_.root_->ptrs
*/
ptrs = get_field_value(root, "ptrs");
if (ca_code(value_type(ptrs)) != TYPE_CODE_ARRAY) {
CA_PRINT("Unexpected \"ptrs\" is not an array\n");
return false;
}
if (get_array_bounds (value_type(ptrs), &low_bound, &high_bound) == 0) {
CA_PRINT("Could not determine \"ptrs\" bounds\n");
return false;
}
CA_PRINT_DBG("tcmalloc::Static::pageheap_->pagemap_.root_->ptrs[%ld-%ld] "
"array length %ld\n", low_bound, high_bound,
high_bound - low_bound + 1);
/*
* tcmalloc::Static::pageheap_->pagemap_.root_->ptrs[index]
*/
for (index = low_bound; index <= high_bound; index++) {
struct value *ptr, *node;
struct value *ptrs2;
LONGEST low_bound2, high_bound2, index2;
ptr = value_subscript(ptrs, index);
if (value_as_address(ptr) == 0)
continue;
node = value_ind(ptr);
/*
* tcmalloc::Static::pageheap_->pagemap_.root_->ptrs[index]->ptrs
*/
ptrs2 = get_field_value(node, "ptrs");
get_array_bounds (value_type(ptrs2), &low_bound2, &high_bound2);
CA_PRINT_DBG("tcmalloc::Static::pageheap_->pagemap_.root_->ptrs[%ld]->ptrs[%ld-%ld] "
"array length %ld\n", index, low_bound2, high_bound2,
high_bound2 - low_bound2 + 1);
/*
* tcmalloc::Static::pageheap_->pagemap_.root_->ptrs[index]->ptrs[index2]
*/
for (index2 = low_bound2; index2 <= high_bound2; index2++) {
struct value *node2;
struct value *leaf_p, *leaf;
node2 = value_subscript(ptrs2, index2);
if (value_as_address(node2) == 0)
continue;
leaf_p = value_cast(leaf_type, node2);
leaf = value_ind(leaf_p);
if (parse_leaf(leaf, span_type) == false)
return false;
}
}
return true;
}
static bool
parse_pagemap_2_7(struct symbol *pageheap_, struct type *leaf_type,
struct type *span_type)
{
struct value *pageheap, *storage;
struct value *pagemap;
struct type *pageheap_type;
struct value *root;
LONGEST low_bound, high_bound, index;
const char *type_name;
/* tcmalloc::PageHeap needs more than the default 64KB for gdb value buffer */
execute_command("set max-value-size 2097152", 0);
/* struct symbol to struct value */
storage = value_of_variable(pageheap_, 0);
/* cast reinterpret_cast<PageHeap *>(&pageheap_.memory); */
pageheap_type = lookup_transparent_type("tcmalloc::PageHeap");
if (pageheap_type == NULL) {
CA_PRINT("Failed to lookup type \"tcmalloc::PageHeap\"\n");
return false;
}
pageheap = value_cast(pageheap_type, storage);
/*
* tcmalloc::Static::pageheap_.memory.pagemap_
* type = class TCMalloc_PageMap2<35> {
* private:
* TCMalloc_PageMap2<35>::Leaf *root_[131072];
* void *(*allocator_)(Number);
* }
*/
pagemap = get_field_value(pageheap, "pagemap_");
type_name = ca_name(check_typedef(value_type(pagemap)));
if (strcmp(type_name, "TCMalloc_PageMap2<35>") != 0) {
CA_PRINT("Internal error: pageheap_.pagemap_ has unexpected type\n");
return false;
}
root = get_field_value(pagemap, "root_");
if (get_array_bounds (value_type(root), &low_bound, &high_bound) == 0) {
CA_PRINT("Could not determine \"root_\" bounds\n");
return false;
}
CA_PRINT_DBG("tcmalloc::Static::pageheap_.pagemap_.root_[%ld-%ld] "
"array length %ld\n", low_bound, high_bound,
high_bound - low_bound + 1);
/*
* tcmalloc::Static::pageheap_.memory.pagemap_.root_[index]
*/
for (index = low_bound; index <= high_bound; index++) {
struct value *leaf_p, *leaf;
leaf_p = value_subscript(root, index);
if (value_as_address(leaf_p) == 0)
continue;
leaf = value_ind(leaf_p);
if (parse_leaf(leaf, span_type) == false)
return false;
}
return true;
}
bool
parse_pagemap(void)
{
struct symbol *pageheap_;
struct type *ph_type;
struct type *leaf_type, *span_type;
const char *type_name;
bool span_has_objects = false;
/*
* We need to cast a void* to this type later
*/
type_name = "tcmalloc::Span";
span_type = lookup_transparent_type(type_name);
if (span_type == NULL) {
CA_PRINT("Failed to lookup type \"%s\"\n", type_name);
CA_PRINT("Do you forget to download debug symbols of libtcmalloc.so?\n");
return false;
}
if (type_field_name2no(span_type, "objects") >= 0)
span_has_objects = true;
span_type = lookup_pointer_type(span_type);
/*
* Version detection through global var:
* tcmalloc::PageHeap *tcmalloc::Static::pageheap_;
*/
pageheap_ = lookup_global_symbol("tcmalloc::Static::pageheap_", 0,
VAR_DOMAIN).symbol;
if (pageheap_ == NULL) {
CA_PRINT("Failed to lookup gv "
"\"tcmalloc::Static::pageheap_\"\n");
return false;
}
ph_type = ca_type(pageheap_);
if (ca_name(ph_type) &&
strcmp(ca_name(ph_type), "tcmalloc::Static::PageHeapStorage") == 0) {
if (span_has_objects)
tc_version_minor = 6;
else
tc_version_minor = 7;
} else {
tc_version_minor = 5;
}
if (tc_version_minor <= 5) {
/* Version 2.5 uses three-leveled page map */
type_name = "TCMalloc_PageMap3<35>::Leaf";
leaf_type = lookup_transparent_type(type_name);
if (leaf_type == NULL) {
CA_PRINT("Failed to lookup type \"%s\"\n", type_name);
return false;
}
leaf_type = lookup_pointer_type(leaf_type);
if (!parse_pagemap_2_5(pageheap_, leaf_type, span_type))
return false;
} else if (tc_version_minor <= 7) {
/* Version 2.6+ uses two-leveled page map */
type_name = "TCMalloc_PageMap2<35>::Leaf";
leaf_type = lookup_transparent_type(type_name);
if (leaf_type == NULL) {
CA_PRINT("Failed to lookup type \"%s\"\n", type_name);
return false;
}
leaf_type = lookup_pointer_type(leaf_type);
if (!parse_pagemap_2_7(pageheap_, leaf_type, span_type))
return false;
} else {
CA_PRINT("Unsupported tcmalloc version\n");
return false;
}
return true;
}
bool
parse_central_cache(void)
{
struct symbol *central_cache_;
struct value *central_cache;
LONGEST low_bound, high_bound, index;
/*
* Global var
* tcmalloc::CentralFreeListPadded tcmalloc::Static::central_cache_[88]
*/
central_cache_ = lookup_global_symbol("tcmalloc::Static::central_cache_",
0, VAR_DOMAIN).symbol;
if (central_cache_ == NULL) {
CA_PRINT("Failed to lookup gv "
"\"tcmalloc::Static::central_cache_\"\n");
return false;
}
central_cache = value_of_variable(central_cache_, 0);
if (ca_code(value_type(central_cache)) != TYPE_CODE_ARRAY) {
CA_PRINT("Unexpected \"central_cache_\" is not an array\n");
return false;
}
if (get_array_bounds (value_type(central_cache), &low_bound,
&high_bound) == 0) {
CA_PRINT("Could not determine \"central_cache_\" bounds\n");
return false;
}
if (g_config.kNumClasses == 0)
g_config.kNumClasses = high_bound - low_bound + 1;
else if (g_config.kNumClasses != high_bound - low_bound + 1) {
CA_PRINT("Inconsistent kNumClasses in central_cache\n");
return false;
}
/*
* tcmalloc::Static::central_cache_[index]
*/
for (index = low_bound; index <= high_bound; index++) {
struct value *v;
struct value *cfl; /* CentralFreeListPadded */
struct type *cfl_type;
v = value_subscript(central_cache, index);
/*
* We need to work with tcmalloc::CentralFreeList,
* which is base class of tcmalloc::CentralFreeListPaddedTo<16>,
* which is base class of tcmalloc::CentralFreeListPadded
*/
cfl_type = TYPE_BASECLASS(value_type(v), 0);
cfl_type = TYPE_BASECLASS(cfl_type, 0);
cfl = value_cast(cfl_type, v);
if (parse_central_freelist(cfl) == false)
return false;
}
return true;
}
bool
parse_central_freelist(struct value *cfl)
{
int used_slots, count;
struct value *tc_slots, *val;
LONGEST low_bound, high_bound, index;
/*
* tcmalloc::CentralFreeList::used_slots_
*/
val = get_field_value(cfl, "used_slots_");
used_slots = value_as_long(val);
/*
* tcmalloc::CentralFreeList::used_slots_
*/
tc_slots = get_field_value(cfl, "tc_slots_");
if (ca_code(value_type(tc_slots)) != TYPE_CODE_ARRAY) {
CA_PRINT("Unexpected \"tc_slots\" is not an array\n");
return false;
}
if (get_array_bounds (value_type(tc_slots), &low_bound,
&high_bound) == 0) {
CA_PRINT("Could not determine \"tc_slots\" bounds\n");
return false;
}
/*
* tcmalloc::CentralFreeList::used_slots_[index]
*/
count = 0;