bpftrace/SOURCES/bpftrace-0.13.1-tools-Make-...

286 lines
7.0 KiB
Diff

From 9c9ac55573ac33abbbbad256eda08dafba18bf9f Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Thu, 29 Apr 2021 12:03:28 +0200
Subject: [PATCH 9/9] tools: Make tools rely on BTF instead of header files.
Many distribution already ship BTF and this is more robust that
parsing header files.
Fixes #1820
---
docs/reference_guide.md | 3 ++-
src/clang_parser.cpp | 4 ++++
tools/biosnoop.bt | 5 ++++-
tools/dcsnoop.bt | 2 ++
tools/mdflush.bt | 2 ++
tools/naptime.bt | 2 ++
tools/oomkill.bt | 2 ++
tools/runqlen.bt | 6 ++++--
tools/tcpaccept.bt | 4 ++++
tools/tcpconnect.bt | 4 ++++
tools/tcpdrop.bt | 4 ++++
tools/tcplife.bt | 4 ++++
tools/tcpretrans.bt | 4 ++++
tools/tcpsynbl.bt | 2 ++
14 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/docs/reference_guide.md b/docs/reference_guide.md
index 69a8ed22..11a685de 100644
--- a/docs/reference_guide.md
+++ b/docs/reference_guide.md
@@ -3455,7 +3455,8 @@ Attaching 1 probe...
# BTF Support
If kernel has BTF, kernel types are automatically available and there is no need to include additional headers
-to use them.
+to use them. To allow users to detect this situation in scripts, the preprocessor macro `BPFTRACE_HAVE_BTF`
+is defined if BTF is detected. See tools/ for examples of its usage.
Requirements for using BTF:
diff --git a/src/clang_parser.cpp b/src/clang_parser.cpp
index 20269a71..7fb39949 100644
--- a/src/clang_parser.cpp
+++ b/src/clang_parser.cpp
@@ -747,6 +747,9 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
// Since we're omitting <linux/types.h> there's no reason to
// add the wokarounds for it
args.push_back("-D__CLANG_WORKAROUNDS_H");
+ // Let script know we have BTF -- this is useful for prewritten tools to
+ // conditionally include headers if BTF isn't available.
+ args.push_back("-DBPFTRACE_HAVE_BTF");
if (handler.parse_file("definitions.h", input, args, input_files, false) &&
handler.has_redefinition_error())
@@ -779,6 +782,7 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
// taken from BTF. We cannot use BTF in such a case.
args.pop_back();
args.pop_back();
+ args.pop_back();
input_files.back() = get_empty_btf_generated_header();
}
diff --git a/tools/biosnoop.bt b/tools/biosnoop.bt
index aa88f4ba..33ad75de 100755
--- a/tools/biosnoop.bt
+++ b/tools/biosnoop.bt
@@ -1,5 +1,4 @@
#!/usr/bin/env bpftrace
-#include <linux/blkdev.h>
/*
* biosnoop.bt Block I/O tracing tool, showing per I/O latency.
* For Linux, uses bpftrace, eBPF.
@@ -11,6 +10,10 @@
* 15-Nov-2017 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
+#include <linux/blkdev.h>
+#endif
+
BEGIN
{
printf("%-12s %-7s %-16s %-6s %7s\n", "TIME(ms)", "DISK", "COMM", "PID", "LAT(ms)");
diff --git a/tools/dcsnoop.bt b/tools/dcsnoop.bt
index 183f0fb5..e85ab1aa 100755
--- a/tools/dcsnoop.bt
+++ b/tools/dcsnoop.bt
@@ -15,6 +15,7 @@
* 08-Sep-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/fs.h>
#include <linux/sched.h>
@@ -24,6 +25,7 @@ struct nameidata {
struct qstr last;
// [...]
};
+#endif
BEGIN
{
diff --git a/tools/mdflush.bt b/tools/mdflush.bt
index 541abba1..1db547f6 100755
--- a/tools/mdflush.bt
+++ b/tools/mdflush.bt
@@ -13,8 +13,10 @@
* 08-Sep-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/genhd.h>
#include <linux/bio.h>
+#endif
BEGIN
{
diff --git a/tools/naptime.bt b/tools/naptime.bt
index eb96b677..a84652a3 100755
--- a/tools/naptime.bt
+++ b/tools/naptime.bt
@@ -13,8 +13,10 @@
* 16-Feb-2019 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/time.h>
#include <linux/sched.h>
+#endif
BEGIN
{
diff --git a/tools/oomkill.bt b/tools/oomkill.bt
index 6126682d..1c9b16a3 100755
--- a/tools/oomkill.bt
+++ b/tools/oomkill.bt
@@ -20,7 +20,9 @@
* 07-Sep-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/oom.h>
+#endif
BEGIN
{
diff --git a/tools/runqlen.bt b/tools/runqlen.bt
index 02d82d74..1be42adc 100755
--- a/tools/runqlen.bt
+++ b/tools/runqlen.bt
@@ -11,17 +11,19 @@
* 07-Oct-2018 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/sched.h>
// Until BTF is available, we'll need to declare some of this struct manually,
// since it isn't available to be #included. This will need maintenance to match
// your kernel version. It is from kernel/sched/sched.h:
-struct cfs_rq_partial {
+struct cfs_rq {
struct load_weight load;
unsigned long runnable_weight;
unsigned int nr_running;
unsigned int h_nr_running;
};
+#endif
BEGIN
{
@@ -31,7 +33,7 @@ BEGIN
profile:hz:99
{
$task = (struct task_struct *)curtask;
- $my_q = (struct cfs_rq_partial *)$task->se.cfs_rq;
+ $my_q = (struct cfs_rq *)$task->se.cfs_rq;
$len = $my_q->nr_running;
$len = $len > 0 ? $len - 1 : 0; // subtract currently running task
@runqlen = lhist($len, 0, 100, 1);
diff --git a/tools/tcpaccept.bt b/tools/tcpaccept.bt
index b40a041b..2f4dfe1f 100755
--- a/tools/tcpaccept.bt
+++ b/tools/tcpaccept.bt
@@ -16,8 +16,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpconnect.bt b/tools/tcpconnect.bt
index 4ae30d65..05c3ca0e 100755
--- a/tools/tcpconnect.bt
+++ b/tools/tcpconnect.bt
@@ -19,8 +19,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpdrop.bt b/tools/tcpdrop.bt
index 79a86b08..2de3e507 100755
--- a/tools/tcpdrop.bt
+++ b/tools/tcpdrop.bt
@@ -17,8 +17,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcplife.bt b/tools/tcplife.bt
index 9c0d8814..a9a054bf 100755
--- a/tools/tcplife.bt
+++ b/tools/tcplife.bt
@@ -13,10 +13,14 @@
* 17-Apr-2019 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <net/tcp_states.h>
#include <net/sock.h>
#include <linux/socket.h>
#include <linux/tcp.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpretrans.bt b/tools/tcpretrans.bt
index 8b9500c0..777d78fa 100755
--- a/tools/tcpretrans.bt
+++ b/tools/tcpretrans.bt
@@ -17,8 +17,12 @@
* 23-Nov-2018 Dale Hamel created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <linux/socket.h>
#include <net/sock.h>
+#else
+#include <sys/socket.h>
+#endif
BEGIN
{
diff --git a/tools/tcpsynbl.bt b/tools/tcpsynbl.bt
index 4b3c99c3..0570f29c 100755
--- a/tools/tcpsynbl.bt
+++ b/tools/tcpsynbl.bt
@@ -13,7 +13,9 @@
* 19-Apr-2019 Brendan Gregg Created this.
*/
+#ifndef BPFTRACE_HAVE_BTF
#include <net/sock.h>
+#endif
BEGIN
{
--
2.35.3