Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9280561ybi; Wed, 10 Jul 2019 07:39:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqx2+arA1dABvIX6NGOBg3q8Wy0Skr2HQzlzL3K1B5zIeWRPG9QlPy03r503P9XB4WwaSNKT X-Received: by 2002:a17:902:6b0c:: with SMTP id o12mr38144927plk.113.1562769595846; Wed, 10 Jul 2019 07:39:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562769595; cv=none; d=google.com; s=arc-20160816; b=KCsgR9EnCPWtNmwLMN9EJ6ZUCwWBdtgOQOBT8sa2yEBIuCcbGhSwMyQ7iybTF5b6RE ZxeQgsS/1PWYqsSIc8JpBp3n+JMGhUR+9GcUNpCkdVIeIhlgdYXWUH6tEo+qgtqVOIsh Yo8bWXBVjU4b/LNA9RRK9zlU+aClbJ0tq8bt3uwGmaUHePaLuX689cx00ljej/3hUvVt nmBWrM4rYVl3MH9fGU7n5XKQuvRIF89faiKPh0j3+IjhdpM3DnR/ezkjlXxY5/VLtOCF 6XLhUtQ0doJF1+O3Wpb3V28Q80HmPkiZI3xruV/dhgxSPXTmxCNrqNr+bzi8Ec8ntYQd 6YdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qaZWTPLSZP/SuE1pr+x4mGiPN4X4stJUVD9vrV0URR8=; b=ErYTcqD50CzlIFkoNRZjFC6oCggJV4bpcXjUQwxiUrHvu1NK2GaMSNokA3LwyA47US ZvAdCRBsOXiPvExvVk0uyeobIiZW96hvIs66U+uJyNvlYvlycxmdTMdb36IfNx1RwRgO IAJWOM+IYVvaByUSQAm9hl/bFzmDXVgsA4rYAILLVRmBL7SYmeg4FlTxKGMdGKK0v6pv bsSYn7v6I136fLyTfqtHAwzslVjDxxXtQ4hb/U9K3sjC1QAGD7qZKR2qInTe51ddRVG9 75px6Qa7XQZfBfaaER/MC0zPJmbO8VBA2u+ZsKRVzfWRMzsBwqSF2aMv1lEW5f+JmyNX Jofg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=GwgIjykL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r4si2544212pgb.245.2019.07.10.07.39.40; Wed, 10 Jul 2019 07:39:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=GwgIjykL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727920AbfGJOQM (ORCPT + 99 others); Wed, 10 Jul 2019 10:16:12 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:38871 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727678AbfGJOQK (ORCPT ); Wed, 10 Jul 2019 10:16:10 -0400 Received: by mail-pf1-f194.google.com with SMTP id y15so1180277pfn.5 for ; Wed, 10 Jul 2019 07:16:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qaZWTPLSZP/SuE1pr+x4mGiPN4X4stJUVD9vrV0URR8=; b=GwgIjykLh5eqei88bOMpZHwO9qvLmQGem/1teyQxrRIg3YrPgEf4AC8HboS8y7PhCl sy7rYfBqLYAeSY65gUJlTPXjcgqU4IhpXMXWiHRkx7xCL6pL55RQehoZUGCSFc9yfrSt fHOMiuneC4CLBcIHhOEHHaXtAta0bdXQkhlFM= 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=qaZWTPLSZP/SuE1pr+x4mGiPN4X4stJUVD9vrV0URR8=; b=rDChJEPiCZFWxclUFx76kY6HxWfKw0Dwwq88h0O/Jwfw7pSE4NVZvULrL01cGWOknu iydx3eCMFAGqhWxUSfW2S01YjuMXQXeBEzZMrVW/JjT7f7wbS8GKN+CVoOV3RFyHQZ2a y6z6jWVW2o3bmHmLDZaS2vaIa3/9lBvXmM+hK7owhs4lAB7pmesY8lmNzIEDFUL55KWL uHt9sec9Q74kGL/sJ8uc02mhM0x5TifGh1XgUDNIBQhwJ+p/cpNhqnZ5LD08OSB9GYsj BAizplAeFlKLHKAvIThoj95LfJEnZ9LYASXbY6FjpGxdh7zHRJv1PLK13RPvgigZsNZx 3M2g== X-Gm-Message-State: APjAAAXZRftGd+Nq2HN541b47WwvPsmvZIhDquTZMSiWkZO0v062ERS0 domDkyqsCubM22KKIZcNpA23MIgj X-Received: by 2002:a17:90a:bb94:: with SMTP id v20mr7412797pjr.88.1562768168516; Wed, 10 Jul 2019 07:16:08 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id l124sm2589249pgl.54.2019.07.10.07.16.04 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 07:16:07 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Adrian Ratiu , Alexei Starovoitov , bpf@vger.kernel.org, Brendan Gregg , connoro@google.com, Daniel Borkmann , duyuchao , Ingo Molnar , jeffv@google.com, Karim Yaghmour , kernel-team@android.com, linux-kselftest@vger.kernel.org, Manali Shukla , Manjo Raja Rao , Martin KaFai Lau , Masami Hiramatsu , Matt Mullins , Michal Gregorczyk , Michal Gregorczyk , Mohammad Husain , namhyung@google.com, namhyung@kernel.org, netdev@vger.kernel.org, paul.chaignon@gmail.com, primiano@google.com, Qais Yousef , Shuah Khan , Song Liu , Srinivas Ramana , Steven Rostedt , Tamir Carmeli , Yonghong Song Subject: [PATCH RFC 3/4] lib/bpf: Add support for ftrace event attach and detach Date: Wed, 10 Jul 2019 10:15:47 -0400 Message-Id: <20190710141548.132193-4-joel@joelfernandes.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog In-Reply-To: <20190710141548.132193-1-joel@joelfernandes.org> References: <20190710141548.132193-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add the needed library support in this commit. Signed-off-by: Joel Fernandes (Google) --- tools/lib/bpf/bpf.c | 53 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 4 +++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 59 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index c4a48086dc9a..28c5a7d00d14 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -57,6 +60,8 @@ #define min(x, y) ((x) < (y) ? (x) : (y)) #endif +#define TRACEFS "/sys/kernel/debug/tracing" + static inline __u64 ptr_to_u64(const void *ptr) { return (__u64) (unsigned long) ptr; @@ -658,6 +663,54 @@ int bpf_raw_tracepoint_open(const char *name, int prog_fd) return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); } +int bpf_raw_tracepoint_ftrace_attach(const char *subsys, const char *name, + int prog_fd) +{ + char buf[256]; + int len, ret, tfd; + + sprintf(buf, "%s/events/%s/%s/bpf", TRACEFS, subsys, name); + tfd = open(buf, O_WRONLY); + if (tfd < 0) + return tfd; + + sprintf(buf, "attach:%d", prog_fd); + len = strlen(buf); + ret = write(tfd, buf, len); + + if (ret < 0) + goto err; + if (ret != len) + ret = -1; +err: + close(tfd); + return ret; +} + +int bpf_raw_tracepoint_ftrace_detach(const char *subsys, const char *name, + int prog_fd) +{ + char buf[256]; + int len, ret, tfd; + + sprintf(buf, "%s/events/%s/%s/bpf", TRACEFS, subsys, name); + tfd = open(buf, O_WRONLY); + if (tfd < 0) + return tfd; + + sprintf(buf, "detach:%d", prog_fd); + len = strlen(buf); + ret = write(tfd, buf, len); + + if (ret < 0) + goto err; + if (ret != len) + ret = -1; +err: + close(tfd); + return ret; +} + int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log) { diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 9593fec75652..5b9c44658037 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -163,6 +163,10 @@ LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_ftrace_attach(const char *subsys, + const char *name, int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_ftrace_detach(const char *subsys, + const char *name, int prog_fd); LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log); LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 673001787cba..fca377b688c2 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -163,4 +163,6 @@ LIBBPF_0.0.3 { bpf_map__is_internal; bpf_map_freeze; btf__finalize_data; + bpf_raw_tracepoint_ftrace_attach; + bpf_raw_tracepoint_ftrace_detach; } LIBBPF_0.0.2; -- 2.22.0.410.gd8fdbe21b5-goog