From patchwork Wed Jul 7 19:46:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363993 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E88B3C07E95 for ; Wed, 7 Jul 2021 19:47:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D190361CB0 for ; Wed, 7 Jul 2021 19:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232766AbhGGTtw (ORCPT ); Wed, 7 Jul 2021 15:49:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:58442 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233086AbhGGTtu (ORCPT ); Wed, 7 Jul 2021 15:49:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N6Lkp9H+PeXEJohL6vruWVGim8bb8jO4Rintdh3j+00=; b=Z2PpmaxhScqQGLUL6IQM8yOFVU3NPEuV995gMNMgerMFQ5RxYOj6c8BzPSppHwGWZoXL8Y KCs6rS2PJRAoYenBJwSM1W0H5ufXBtRW4ApCpUIHaJkwYCf+hUcWvI7laRm6n5CuPFZzVM D7x9N9UN+jkwdkUzQ+q7fzJbKQSkLts= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-475-kYbJR8WSMeq_rgPzqooUpg-1; Wed, 07 Jul 2021 15:47:08 -0400 X-MC-Unique: kYbJR8WSMeq_rgPzqooUpg-1 Received: by mail-wm1-f71.google.com with SMTP id l3-20020a05600c1d03b029021076e2b2f6so2547590wms.4 for ; Wed, 07 Jul 2021 12:47:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N6Lkp9H+PeXEJohL6vruWVGim8bb8jO4Rintdh3j+00=; b=LzEjqrw36Ri/a25Pk2weuv279ov7YsAIuyak+jbm035lK5REMEetzIigb0xRmMxLgo 9tYGXLHGME7PT+vxRn8QaiNHRNcpm+J3yOYIw7xhHwQbR5eQno3nEE+DrxZXhNdf3wAn qGAeuVtZErgx3OnPIisaaqsg7LWyhzCxCD3kpvz3HtiOAE+KvgHJbKOZkpzvJHM+C8TK Rup1Dd2X5m4zpkTOEfasNb9fUhHjv7rHIECKcypB0/f9F3Tg4fn0Ogy1Buk3PFHa3xRj vn0wEJ1Xa8xJa+lfg2udcgpprDw/1ZJZFf5xmALfO5A4fGU3niCCJEm5D0+mu08Ml6Hv 8wNg== X-Gm-Message-State: AOAM533e5R9VAQC6etT7fDuo85IWHmyeIWOt/Ja6BJjfxYrN8sAg9B1w JmhSk0xLNmDeXnMbfQrt8DA5rvXeh4oTkOhn3GTDdt7Dka3yg7hwlL/k0hfsuFeeLX4Tp380dAR oMQgb2nrinuEZ X-Received: by 2002:a1c:4302:: with SMTP id q2mr792617wma.37.1625687227560; Wed, 07 Jul 2021 12:47:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwx6Hij6OUVN0HPvFYTpi+Y4VHcPBqilpZnkPQP9PAJQEeBMChoVHxNTGuD6GH5ZZ0UJXu2rA== X-Received: by 2002:a1c:4302:: with SMTP id q2mr792598wma.37.1625687227390; Wed, 07 Jul 2021 12:47:07 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id l20sm20208357wmq.3.2021.07.07.12.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:47:07 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 6/7] libbpf: allow specification of "kprobe/function+offset" Date: Wed, 7 Jul 2021 21:46:18 +0200 Message-Id: <20210707194619.151676-7-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alan Maguire kprobes can be placed on most instructions in a function, not just entry, and ftrace and bpftrace support the function+offset notification for probe placement. Adding parsing of func_name into func+offset to bpf_program__attach_kprobe() allows the user to specify SEC("kprobe/bpf_fentry_test5+0x6") ...for example, and the offset can be passed to perf_event_open_probe() to support kprobe attachment. Signed-off-by: Alan Maguire Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1e04ce724240..60c9e3e77684 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10309,11 +10309,25 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, const char *func_name) { char errmsg[STRERR_BUFSIZE]; + char func[BPF_OBJ_NAME_LEN]; + unsigned long offset = 0; struct bpf_link *link; - int pfd, err; + int pfd, err, n; + + n = sscanf(func_name, "%[a-zA-Z0-9_.]+%lx", func, &offset); + if (n < 1) { + err = -EINVAL; + pr_warn("kprobe name is invalid: %s\n", func_name); + return libbpf_err_ptr(err); + } + if (retprobe && offset != 0) { + err = -EINVAL; + pr_warn("kretprobes do not support offset specification\n"); + return libbpf_err_ptr(err); + } - pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name, - 0 /* offset */, -1 /* pid */); + pfd = perf_event_open_probe(false /* uprobe */, retprobe, func, + offset, -1 /* pid */); if (pfd < 0) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, retprobe ? "kretprobe" : "kprobe", func_name,