Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bcc
Commits
478636be
Commit
478636be
authored
Mar 27, 2016
by
Brenden Blanco
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #448 from vmg/vmg/stacks
Better stack walking APIs
parents
b61519cb
88899060
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
0 deletions
+88
-0
examples/tracing/mallocstacks.py
examples/tracing/mallocstacks.py
+60
-0
src/python/bcc/table.py
src/python/bcc/table.py
+28
-0
No files found.
examples/tracing/mallocstacks.py
0 → 100644
View file @
478636be
#!/usr/bin/python
#
# mallocstacks Trace malloc() calls in a process and print the full
# stack trace for all callsites.
# For Linux, uses BCC, eBPF. Embedded C.
#
# This script is a basic example of the new Linux 4.6+ BPF_STACK_TRACE
# table API.
#
# Copyright 2016 GitHub, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
from
__future__
import
print_function
from
bcc
import
BPF
,
ProcessSymbols
from
time
import
sleep
import
sys
if
len
(
sys
.
argv
)
<
2
:
print
(
"USAGE: mallocstacks PID"
)
exit
()
pid
=
int
(
sys
.
argv
[
1
])
# load BPF program
b
=
BPF
(
text
=
"""
#include <uapi/linux/ptrace.h>
BPF_HASH(calls, int);
BPF_STACK_TRACE(stack_traces, 1024)
int alloc_enter(struct pt_regs *ctx, size_t size) {
int key = stack_traces.get_stackid(ctx,
BPF_F_USER_STACK|BPF_F_REUSE_STACKID);
if (key < 0)
return 0;
u64 zero = 0, *val;
val = calls.lookup_or_init(&key, &zero);
(*val) += size;
return 0;
};
"""
)
b
.
attach_uprobe
(
name
=
"c"
,
sym
=
"malloc"
,
fn_name
=
"alloc_enter"
,
pid
=
pid
)
print
(
"Attaching to malloc in pid %d, Ctrl+C to quit."
%
pid
)
decoder
=
ProcessSymbols
(
pid
)
# sleep until Ctrl-C
try
:
sleep
(
99999999
)
except
KeyboardInterrupt
:
pass
calls
=
b
.
get_table
(
"calls"
)
stack_traces
=
b
.
get_table
(
"stack_traces"
)
for
k
,
v
in
reversed
(
sorted
(
calls
.
items
(),
key
=
lambda
c
:
c
[
1
].
value
)):
print
(
"%d bytes allocated at:"
%
v
.
value
)
for
addr
in
stack_traces
.
walk
(
k
.
value
):
print
(
"
\
t
%s (%x)"
%
(
decoder
.
decode_addr
(
addr
),
addr
))
src/python/bcc/table.py
View file @
478636be
...
...
@@ -412,9 +412,37 @@ class PerCpuArray(ArrayBase):
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment