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

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