Skip to content

Commit

Permalink
Add cuckoohash_map pretty printer
Browse files Browse the repository at this point in the history
  • Loading branch information
Manu Goyal authored and Manu Goyal committed Dec 31, 2017
1 parent 11080cc commit 682118b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libcuckoo-gdb-printers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# GDB Printers

This directory contains a python module that allows pretty-printing a
cuckoohash map.

## Usage

In order to use the libcuckoo pretty printers, add the following to your
`~/.gdbinit`:

```
python
import sys
sys.path.insert(0, '/path/to/libcuckoo/libcuckoo-gdb-printers')
from libcuckoo.printers import register_libcuckoo_printers
register_libcuckoo_printers (None)
end
```
Empty file.
43 changes: 43 additions & 0 deletions libcuckoo-gdb-printers/libcuckoo/printers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import gdb
import gdb.printing

class CuckoohashMapPrinter:
"""Print a cuckoohash_map object"""

def __init__(self, val):
self.val = val
self.slot_per_bucket = int(self.val.type.template_argument(5))

def _iterator(self):
buckets_obj = self.val['buckets_']
hashpower = buckets_obj['hashpower_']['_M_i']
buckets_ptr = buckets_obj['buckets_']
if not buckets_ptr:
return
num_buckets = int(2**hashpower)
for i in range(num_buckets):
bucket = (buckets_ptr + i).dereference()
storage_value_type = gdb.lookup_type(str(bucket.type) + '::storage_value_type')
for j in range(self.slot_per_bucket):
if bucket['occupied_']['_M_elems'][j]:
value_blob = bucket['values_']['_M_elems'][j]
storage_value = value_blob.cast(storage_value_type)
yield ('key', storage_value['first'])
yield ('value', storage_value['second'])

def children(self):
return self._iterator()

def to_string(self):
return 'cuckoohash_map'

def display_hint(self):
return 'map'

def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter("libcuckoo")
pp.add_printer('cuckoohash_map', '^cuckoohash_map<.*>$', CuckoohashMapPrinter)
return pp

def register_libcuckoo_printers(objfile):
gdb.printing.register_pretty_printer(objfile, build_pretty_printer())

0 comments on commit 682118b

Please sign in to comment.