Skip to content

Commit 8b23edf

Browse files
committed
experiment with using ctypes to wrap libusdt.so
depends on chrisa/libusdt#9
1 parent e436bf1 commit 8b23edf

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.so
2+
*.pyc
3+
core
24
build/

Makefile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
ctypes: dtrace-ctypes.py libusdt.so
2+
sudo dtrace -Zqn ':::func{printf("%s\n",copyinstr(arg0));}' -c ./dtrace-ctypes.py
3+
4+
libusdt.so: libusdt/libusdt.a
5+
gcc -g -shared -o $@ -Wl,--whole-archive $<
6+
17
test: dtrace.so test.py
2-
@echo
3-
@echo Run the following in another shell:
4-
@echo "sudo dtrace -qn ':::testname{printf(\"%s\\\\n\",copyinstr(arg0));}'"
5-
@echo
6-
@sleep 2
7-
python test.py
8+
sudo dtrace -Zqn ':::testname{printf("%s\n",copyinstr(arg0));}' -c ./test.py
89

910
dtrace.so: libusdt/libusdt.a dtrace.c setup.py
1011
rm -rf dtrace.so build/
1112
python setup.py build
12-
cp build/lib*/dtrace.so .
13+
mv build/lib*/*.so .
1314

1415
libusdt/libusdt.a:
1516
git submodule init

dtrace-ctypes.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
from ctypes import *
4+
import time
5+
6+
_libusdt = cdll.LoadLibrary("./libusdt.so")
7+
8+
usdt_create_provider = _libusdt.usdt_create_provider
9+
usdt_create_provider.argtypes = [c_char_p, c_char_p]
10+
11+
usdt_create_probe = _libusdt.usdt_create_probe
12+
usdt_create_probe.argtypes = [c_char_p, c_char_p, c_int, c_void_p]
13+
14+
usdt_provider_add_probe = _libusdt.usdt_provider_add_probe
15+
usdt_provider_enable = _libusdt.usdt_provider_enable
16+
usdt_fire_probedef = _libusdt.usdt_fire_probedef
17+
18+
class Probe():
19+
def __init__(self, name, func, arg_desc):
20+
self.length = len(arg_desc)
21+
args = (c_char_p * self.length)()
22+
for i in range(self.length):
23+
args[i] = arg_desc[i]
24+
self.probedef = usdt_create_probe(name, func, self.length, args)
25+
26+
def fire(self, args):
27+
if len(args) == self.length:
28+
c_args = (c_void_p * self.length)()
29+
for i in range(self.length):
30+
c_args[i] = cast(args[i], c_void_p)
31+
usdt_fire_probedef(self.probedef, self.length, c_args)
32+
33+
class Provider():
34+
provider = None
35+
probes = []
36+
def __init__(self, provider="python-dtrace", module="default_module"):
37+
self.provider = usdt_create_provider(provider, module)
38+
39+
def add_probe(self, probe):
40+
self.probes.append(probe)
41+
usdt_provider_add_probe(self.provider, probe.probedef)
42+
43+
def enable(self):
44+
usdt_provider_enable(self.provider)
45+
46+
def __del__(self):
47+
pass
48+
49+
def main():
50+
test_prov = Provider("provname", "provmod")
51+
test_probe = Probe("probename", "func", ["char *"])
52+
test_prov.add_probe(test_probe)
53+
test_prov.enable()
54+
55+
test_probe.fire(["Hello World"])
56+
57+
if __name__ == "__main__":
58+
main()

test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def timeout2():
2828
def timeout3():
2929
print timeout1() + " " + timeout2()
3030

31-
while True:
31+
for i in range(2):
3232
timeout3()
3333
dtrace.fire("Hello dtrace")
3434
time.sleep(1)

0 commit comments

Comments
 (0)