• 最快的ebpf开发环境搭建方式


    环境搭建

    启动容器

    1. sudo docker run --rm -it --privileged \
    2. -v /lib/modules:/lib/modules:ro \
    3. -v /sys:/sys:ro \
    4. -v /usr/src:/usr/src:ro \
    5. alpine:3.12

    安装依赖

    1. sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
    2. apk add bcc-tools bcc-doc

    测试

    hello.c 

    1. int hello_world(void *ctx)
    2. {
    3. bpf_trace_printk("Hello, World");
    4. return 0;
    5. }

    hello.py  

    1. from bcc import BPF
    2. b = BPF(src_file="hello.c")
    3. b.attach_kprobe(event="do_sys_openat2", fn_name="hello_world")
    4. b.trace_print()

    执行,可看到打印出了hello world

    1. / # python3 hello.py
    2. In file included from :2:
    3. In file included from /virtual/include/bcc/bpf.h:12:
    4. In file included from include/linux/types.h:6:
    5. In file included from include/uapi/linux/types.h:14:
    6. In file included from ./include/uapi/linux/posix_types.h:5:
    7. In file included from include/linux/stddef.h:5:
    8. In file included from include/uapi/linux/stddef.h:5:
    9. In file included from include/linux/compiler_types.h:90:
    10. include/linux/compiler-clang.h:41:9: warning: '__HAVE_BUILTIN_BSWAP32__' macro redefined [-Wmacro-redefined]
    11. #define __HAVE_BUILTIN_BSWAP32__
    12. ^
    13. <command line>:4:9: note: previous definition is here
    14. #define __HAVE_BUILTIN_BSWAP32__ 1
    15. ^
    16. In file included from :2:
    17. In file included from /virtual/include/bcc/bpf.h:12:
    18. In file included from include/linux/types.h:6:
    19. In file included from include/uapi/linux/types.h:14:
    20. In file included from ./include/uapi/linux/posix_types.h:5:
    21. In file included from include/linux/stddef.h:5:
    22. In file included from include/uapi/linux/stddef.h:5:
    23. In file included from include/linux/compiler_types.h:90:
    24. include/linux/compiler-clang.h:42:9: warning: '__HAVE_BUILTIN_BSWAP64__' macro redefined [-Wmacro-redefined]
    25. #define __HAVE_BUILTIN_BSWAP64__
    26. ^
    27. <command line>:5:9: note: previous definition is here
    28. #define __HAVE_BUILTIN_BSWAP64__ 1
    29. ^
    30. In file included from :2:
    31. In file included from /virtual/include/bcc/bpf.h:12:
    32. In file included from include/linux/types.h:6:
    33. In file included from include/uapi/linux/types.h:14:
    34. In file included from ./include/uapi/linux/posix_types.h:5:
    35. In file included from include/linux/stddef.h:5:
    36. In file included from include/uapi/linux/stddef.h:5:
    37. In file included from include/linux/compiler_types.h:90:
    38. include/linux/compiler-clang.h:43:9: warning: '__HAVE_BUILTIN_BSWAP16__' macro redefined [-Wmacro-redefined]
    39. #define __HAVE_BUILTIN_BSWAP16__
    40. ^
    41. <command line>:3:9: note: previous definition is here
    42. #define __HAVE_BUILTIN_BSWAP16__ 1
    43. ^
    44. 3 warnings generated.
    45. b' python3-1056231 [005] d..31 1056012.574165: bpf_trace_printk: Hello, World'
    46. b' python3-1056231 [005] d..31 1056012.574277: bpf_trace_printk: Hello, World'
    47. b' python3-1056231 [005] d..31 1056012.574734: bpf_trace_printk: Hello, World'
    48. b' <...>-1059946 [006] d..31 1056300.636287: bpf_trace_printk: Hello, World'
    49. b' <...>-6346 [001] d..31 1056300.673240: bpf_trace_printk: Hello, World'
    50. b' <...>-6346 [001] d..31 1056300.673277: bpf_trace_printk: Hello, World'
    51. b' <...>-6346 [001] d..31 1056300.673287: bpf_trace_printk: Hello, World'
    52. b' <...>-6346 [001] d..31 1056300.673648: bpf_trace_printk: Hello, World'
    53. b' <...>-6346 [001] d..31 1056300.673666: bpf_trace_printk: Hello, World'
    54. b' <...>-6346 [001] d..31 1056300.673676: bpf_trace_printk: Hello, World'
    55. b' <...>-6346 [001] d..31 1056300.673685: bpf_trace_printk: Hello, World'

  • 相关阅读:
    【微信小程序 | 实战开发】配置微信小程序APPID并快速接入
    PostgreSQL数据库结合内网穿透实现公网远程连接
    缘来交友网站的设计与实现(源码+数据库+论文+开题报告+说明文档)
    网络安全笔记-Web服务器
    SQLite数据库损坏及其修复探究
    这次是真【一条龙】了!
    JSP ssm 特殊人群防走失系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计
    计算机毕业设计Java派大星水产商城(源代码+数据库+系统+lw文档)
    抖音矩阵系统,这个排名很难啊。按?
    Python包:包的概念、2种建立包方式、包的使用(代码 + 图文)
  • 原文地址:https://blog.csdn.net/weixin_42758299/article/details/134558432