sync the repo
[hiphop-php.git] / hphp / tools / gdb / sizeof.py
blob97489cfa366182d8befc15f219cdf7706ef2f041
1 #!/usr/bin/env python3
3 """
4 GDB command for printing the sizes of various containers.
5 """
7 from compatibility import *
9 import gdb
10 from gdbutils import *
13 # ------------------------------------------------------------------------------
14 # Size accessor.
17 def sizeof(container):
18 container = deref(container)
19 t = template_type(rawtype(container.type))
21 if t == "std::vector" or t == "HPHP::req::vector":
22 impl = container["_M_impl"]
23 return impl["_M_finish"] - impl["_M_start"]
24 elif t == "std::priority_queue":
25 return sizeof(container["c"])
26 elif t == "std::unordered_map" or t == "HPHP::hphp_hash_map":
27 return container["_M_h"]["_M_element_count"]
28 elif t == "HPHP::FixedStringMap":
29 return container["m_extra"]
30 elif t == "HPHP::IndexedStringMap":
31 return container["m_map"]["m_extra"]
34 # ------------------------------------------------------------------------------
35 # `sizeof' command.
38 class SizeOfCommand(gdb.Command):
39 """Print the semantic size of a container."""
41 def __init__(self):
42 super(SizeOfCommand, self).__init__("sizeof", gdb.COMMAND_DATA)
44 @errorwrap
45 def invoke(self, args, from_tty):
46 argv = parse_argv(args)
48 if len(argv) != 1:
49 print("Usage: sizeof <container>")
50 return
52 size = sizeof(argv[0])
54 if size is not None:
55 gdbprint(size)
56 else:
57 print("sizeof: Unrecognized container.")
60 SizeOfCommand()