• 最快的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'

  • 相关阅读:
    Matlab optimtool优化阵列天线的幅相激励
    Web --- 端口协议
    Python:熬夜把内置库函数整理成合集了,不要错过 ❢
    浅谈企业信息化安全建设中的三大误区
    如何将系统盘MBR转GPT?无损教程分享!
    谷歌广告推广效果不佳的原因
    中国电子学会五级C++考试秘籍
    08 集群参数配置(下)
    LeetCode:2. 两数相加
    博主老程序员长期个人接单
  • 原文地址:https://blog.csdn.net/weixin_42758299/article/details/134558432