1. 04 Apr, 2022 9 commits
    • Andrii Nakryiko's avatar
      Merge branch 'libbpf: name-based u[ret]probe attach' · 88d01a57
      Andrii Nakryiko authored
      Alan Maguire says:
      
      ====================
      
      This patch series focuses on supporting name-based attach - similar
      to that supported for kprobes - for uprobe BPF programs.
      
      Currently attach for such probes is done by determining the offset
      manually, so the aim is to try and mimic the simplicity of kprobe
      attach, making use of uprobe opts to specify a name string.
      Patch 1 supports expansion of the binary_path argument used for
      bpf_program__attach_uprobe_opts(), allowing it to determine paths
      for programs and shared objects automatically, allowing for
      specification of "libc.so.6" rather than the full path
      "/usr/lib64/libc.so.6".
      
      Patch 2 adds the "func_name" option to allow uprobe attach by
      name; the mechanics are described there.
      
      Having name-based support allows us to support auto-attach for
      uprobes; patch 3 adds auto-attach support while attempting
      to handle backwards-compatibility issues that arise.  The format
      supported is
      
      u[ret]probe/binary_path:[raw_offset|function[+offset]]
      
      For example, to attach to libc malloc:
      
      SEC("uprobe//usr/lib64/libc.so.6:malloc")
      
      ..or, making use of the path computation mechanisms introduced in patch 1
      
      SEC("uprobe/libc.so.6:malloc")
      
      Finally patch 4 add tests to the attach_probe selftests covering
      attach by name, with patch 5 covering skeleton auto-attach.
      
      Changes since v4 [1]:
      - replaced strtok_r() usage with copying segments from static char *; avoids
        unneeded string allocation (Andrii, patch 1)
      - switched to using access() instead of stat() when checking path-resolved
        binary (Andrii, patch 1)
      - removed computation of .plt offset for instrumenting shared library calls
        within binaries.  Firstly it proved too brittle, and secondly it was somewhat
        unintuitive in that this form of instrumentation did not support function+offset
        as the "local function in binary" and "shared library function in shared library"
        cases did.  We can still instrument library calls, just need to do it in the
        library .so (patch 2)
      - added binary path logging in cases where it was missing (Andrii, patch 2)
      - avoid strlen() calcuation in checking name match (Andrii, patch 2)
      - reword comments for func_name option (Andrii, patch 2)
      - tightened SEC() name validation to support "u[ret]probe" and fail on other
        permutations that do not support auto-attach (i.e. have u[ret]probe/binary_path:func
        format (Andrii, patch 3)
      - fixed selftests to fail independently rather than skip remainder on failure
        (Andrii, patches 4,5)
      Changes since v3 [2]:
      - reworked variable naming to fit better with libbpf conventions
        (Andrii, patch 2)
      - use quoted binary path in log messages (Andrii, patch 2)
      - added path determination mechanisms using LD_LIBRARY_PATH/PATH and
        standard locations (patch 1, Andrii)
      - changed section lookup to be type+name (if name is specified) to
        simplify use cases (patch 2, Andrii)
      - fixed .plt lookup scheme to match symbol table entries with .plt
        index via the .rela.plt table; also fix the incorrect assumption
        that the code in the .plt that does library linking is the same
        size as .plt entries (it just happens to be on x86_64)
      - aligned with pluggable section support such that uprobe SEC() names
        that do not conform to auto-attach format do not cause skeleton load
        failure (patch 3, Andrii)
      - no longer need to look up absolute path to libraries used by test_progs
        since we have mechanism to determine path automatically
      - replaced CHECK()s with ASSERT*()s for attach_probe test (Andrii, patch 4)
      - added auto-attach selftests also (Andrii, patch 5)
      Changes since RFC [3]:
      - used "long" for addresses instead of ssize_t (Andrii, patch 1).
      - used gelf_ interfaces to avoid assumptions about 64-bit
        binaries (Andrii, patch 1)
      - clarified string matching in symbol table lookups
        (Andrii, patch 1)
      - added support for specification of shared object functions
        in a non-shared object binary.  This approach instruments
        the Procedure Linking Table (PLT) - malloc@PLT.
      - changed logic in symbol search to check dynamic symbol table
        first, then fall back to symbol table (Andrii, patch 1).
      - modified auto-attach string to require "/" separator prior
        to path prefix i.e. uprobe//path/to/binary (Andrii, patch 2)
      - modified auto-attach string to use ':' separator (Andrii,
        patch 2)
      - modified auto-attach to support raw offset (Andrii, patch 2)
      - modified skeleton attach to interpret -ESRCH errors as
        a non-fatal "unable to auto-attach" (Andrii suggested
        -EOPNOTSUPP but my concern was it might collide with other
        instances where that value is returned and reflects a
        failure to attach a to-be-expected attachment rather than
        skip a program that does not present an auto-attachable
        section name. Admittedly -EOPNOTSUPP seems a more natural
        value here).
      - moved library path retrieval code to trace_helpers (Andrii,
        patch 3)
      
      [1] https://lore.kernel.org/bpf/1647000658-16149-1-git-send-email-alan.maguire@oracle.com/
      [2] https://lore.kernel.org/bpf/1643645554-28723-1-git-send-email-alan.maguire@oracle.com/
      [3] https://lore.kernel.org/bpf/1642678950-19584-1-git-send-email-alan.maguire@oracle.com/
      ====================
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      88d01a57
    • Alan Maguire's avatar
      selftests/bpf: Add tests for uprobe auto-attach via skeleton · 579c3196
      Alan Maguire authored
      tests that verify auto-attach works for function entry/return for
      local functions in program and library functions in a library.
      Signed-off-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1648654000-21758-6-git-send-email-alan.maguire@oracle.com
      579c3196
    • Alan Maguire's avatar
      selftests/bpf: Add tests for u[ret]probe attach by name · ba7499bc
      Alan Maguire authored
      add tests that verify attaching by name for
      
      1. local functions in a program
      2. library functions in a shared object
      
      ...succeed for uprobe and uretprobes using new "func_name"
      option for bpf_program__attach_uprobe_opts().  Also verify
      auto-attach works where uprobe, path to binary and function
      name are specified, but fails with -EOPNOTSUPP with a SEC
      name that does not specify binary path/function.
      Signed-off-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1648654000-21758-5-git-send-email-alan.maguire@oracle.com
      ba7499bc
    • Alan Maguire's avatar
      libbpf: Add auto-attach for uprobes based on section name · 39f8dc43
      Alan Maguire authored
      Now that u[ret]probes can use name-based specification, it makes
      sense to add support for auto-attach based on SEC() definition.
      The format proposed is
      
              SEC("u[ret]probe/binary:[raw_offset|[function_name[+offset]]")
      
      For example, to trace malloc() in libc:
      
              SEC("uprobe/libc.so.6:malloc")
      
      ...or to trace function foo2 in /usr/bin/foo:
      
              SEC("uprobe//usr/bin/foo:foo2")
      
      Auto-attach is done for all tasks (pid -1).  prog can be an absolute
      path or simply a program/library name; in the latter case, we use
      PATH/LD_LIBRARY_PATH to resolve the full path, falling back to
      standard locations (/usr/bin:/usr/sbin or /usr/lib64:/usr/lib) if
      the file is not found via environment-variable specified locations.
      Signed-off-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1648654000-21758-4-git-send-email-alan.maguire@oracle.com
      39f8dc43
    • Alan Maguire's avatar
      libbpf: Support function name-based attach uprobes · 433966e3
      Alan Maguire authored
      kprobe attach is name-based, using lookups of kallsyms to translate
      a function name to an address.  Currently uprobe attach is done
      via an offset value as described in [1].  Extend uprobe opts
      for attach to include a function name which can then be converted
      into a uprobe-friendly offset.  The calcualation is done in
      several steps:
      
      1. First, determine the symbol address using libelf; this gives us
         the offset as reported by objdump
      2. If the function is a shared library function - and the binary
         provided is a shared library - no further work is required;
         the address found is the required address
      3. Finally, if the function is local, subtract the base address
         associated with the object, retrieved from ELF program headers.
      
      The resultant value is then added to the func_offset value passed
      in to specify the uprobe attach address.  So specifying a func_offset
      of 0 along with a function name "printf" will attach to printf entry.
      
      The modes of operation supported are then
      
      1. to attach to a local function in a binary; function "foo1" in
         "/usr/bin/foo"
      2. to attach to a shared library function in a shared library -
         function "malloc" in libc.
      
      [1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.htmlSigned-off-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1648654000-21758-3-git-send-email-alan.maguire@oracle.com
      433966e3
    • Alan Maguire's avatar
      libbpf: auto-resolve programs/libraries when necessary for uprobes · 1ce3a60e
      Alan Maguire authored
      bpf_program__attach_uprobe_opts() requires a binary_path argument
      specifying binary to instrument.  Supporting simply specifying
      "libc.so.6" or "foo" should be possible too.
      
      Library search checks LD_LIBRARY_PATH, then /usr/lib64, /usr/lib.
      This allows users to run BPF programs prefixed with
      LD_LIBRARY_PATH=/path2/lib while still searching standard locations.
      Similarly for non .so files, we check PATH and /usr/bin, /usr/sbin.
      
      Path determination will be useful for auto-attach of BPF uprobe programs
      using SEC() definition.
      Signed-off-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1648654000-21758-2-git-send-email-alan.maguire@oracle.com
      1ce3a60e
    • Lorenzo Bianconi's avatar
      samples: bpf: Convert xdp_router_ipv4 to XDP samples helper · 85bf1f51
      Lorenzo Bianconi authored
      Rely on the libbpf skeleton facility and other utilities provided by XDP
      sample helpers in xdp_router_ipv4 sample.
      Signed-off-by: default avatarLorenzo Bianconi <lorenzo@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/7f4d98ee2c13c04d5eb924eebf79ced32fee8418.1647414711.git.lorenzo@kernel.org
      85bf1f51
    • Haiyue Wang's avatar
      bpf: Correct the comment for BTF kind bitfield · 66df0fdb
      Haiyue Wang authored
      The commit 8fd88691 ("bpf: Add BTF_KIND_FLOAT to uapi") has extended
      the BTF kind bitfield from 4 to 5 bits, correct the comment.
      Signed-off-by: default avatarHaiyue Wang <haiyue.wang@intel.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220403115327.205964-1-haiyue.wang@intel.com
      66df0fdb
    • Yuntao Wang's avatar
      selftests/bpf: Fix cd_flavor_subdir() of test_progs · 9bbad6da
      Yuntao Wang authored
      Currently, when we run test_progs with just executable file name, for
      example 'PATH=. test_progs-no_alu32', cd_flavor_subdir() will not check
      if test_progs is running as a flavored test runner and switch into
      corresponding sub-directory.
      
      This will cause test_progs-no_alu32 executed by the
      'PATH=. test_progs-no_alu32' command to run in the wrong directory and
      load the wrong BPF objects.
      Signed-off-by: default avatarYuntao Wang <ytcoode@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220403135245.1713283-1-ytcoode@gmail.com
      9bbad6da
  2. 03 Apr, 2022 3 commits
  3. 01 Apr, 2022 4 commits
  4. 31 Mar, 2022 24 commits