Skip to content

Commit

Permalink
table: Implement a StackWalker for StackTrace tables
Browse files Browse the repository at this point in the history
The StackWalker iterator lets us call `stack_trace.walk(id)` to iterate
through the addresses in any given stack. The constructor of this
iterator takes an optional `resolver` to convert the addresses in the
iterator into symbols (or to format them according to the users' needs).
  • Loading branch information
vmg committed Mar 25, 2016
1 parent b61519c commit 066eb30
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/python/bcc/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,37 @@ def __init__(self, *args, **kwargs):
raise Exception("Unsupported")

class StackTrace(TableBase):
MAX_DEPTH = 127

def __init__(self, *args, **kwargs):
super(StackTrace, self).__init__(*args, **kwargs)

class StackWalker(object):
def __init__(self, stack, resolve=None):
self.stack = stack
self.n = -1
self.resolve = resolve

def __iter__(self):
return self

def __next__(self):
return self.next()

def next(self):
self.n += 1
if self.n == StackTrace.MAX_DEPTH:
raise StopIteration()

addr = self.stack.ip[self.n]
if addr == 0 :
raise StopIteration()

return self.resolve(addr) if self.resolve else addr

def walk(self, stack_id, resolve=None):
return StackTrace.StackWalker(self[self.Key(stack_id)], resolve)

def __len__(self):
i = 0
for k in self: i += 1
Expand Down

0 comments on commit 066eb30

Please sign in to comment.