Blame SOURCES/bpftrace-0.13.1-tools-Make-tools-rely-on-BTF-instead-of-header-files.patch

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