Today's Question:  What weekend projects have you created?        GIVE A SHOUT

Technical Article => Programming =>  Python

Use pdb to help understand python program

  nanxiao      2017-08-22 22:42:37      347    0


As I have mentioned in Why do I need a debugger?:

(3) Debugger is a good tool to help you understand code.

So when I come across difficulty to understand code in bcc project, I know it is time to resort to pdb, python's debugger, to help me.

The thing which confuses me is here:

counts = b.get_table("counts")
for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
    print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value))

From previous code:

BPF_HASH(counts, struct key_t, u64, 256);

It seems the v's type is u64, and I can't figure out why use v.value to fetch its data here.

pdb's manual is very succinct and its command is so similar with gdb's, so it is no learning curve for me. Just launch a debugging session and set breakpoint at "counts = b.get_table("counts")" line:

# python -m pdb
> /root/Project/bcc/tools/<module>()
-> from __future__ import print_function
(Pdb) b

Start the program and press Ctrl-C after seconds; the breakpoint will be hit:

(Pdb) r
Tracing... Ctrl-C to end.
ADDR             FUNC                          COUNT
> /root/Project/bcc/tools/<module>()
-> counts = b.get_table("counts")

Step into get_table method, and single-step every line. Before leaving method, check the type of keytype and leaftype:

-> counts = b.get_table("counts")
(Pdb) s
> /usr/lib/python3.6/site-packages/bcc/
-> def get_table(self, name, keytype=None, leaftype=None, reducer=None):
(Pdb) n
> /usr/lib/python3.6/site-packages/bcc/
-> map_id = lib.bpf_table_id(self.module, name.encode("ascii"))
(Pdb) p leaf_desc
b'"unsigned long long"'
(Pdb) n
> /usr/lib/python3.6/site-packages/bcc/
-> leaftype = BPF._decode_table_type(json.loads(leaf_desc.decode()))
> /usr/lib/python3.6/site-packages/bcc/
-> return Table(self, map_id, map_fd, keytype, leaftype, reducer=reducer)
(Pdb) p leaftype
<class 'ctypes.c_ulong'>
(Pdb) p keytype
<class 'bcc.key_t'>

Yeah! The magic is here: leaftype's type is not pure u64, but ctypes.c_ulong. According to document:

>>> print(i.value)

We should use v.value to get its internal data.

Happy pdbing! Happy python debugging!


Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious


No related articles


No comment for this article.


How programmers see some customers

By sonic0002