From 806aa9e6386e7c9f342d536cc0dc66210b7ffbe0 Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Thu, 26 Oct 2017 10:21:21 +0000 Subject: [PATCH] arm: handling size type differences between arm host and bpf Change-Id: I977bb03332040df8f49c06645abca7233a90aaac --- packaging/bcc-tools.spec | 1 + src/cc/CMakeLists.txt | 1 + src/cc/export/helpers.h | 2 + src/cc/export/host32.h | 10 +++ src/cc/export/host64.h | 10 +++ src/cc/exported_files.cc | 8 ++ src/cc/frontends/clang/loader.cc | 12 ++- src/cc/hostcompat/linux/sched.h | 10 +++ src/cc/hostcompat/uapi/linux/ptrace.h | 13 +++ tools/dbus-connection-message-size.c | 59 +++++++------ tools/dbus-glib-top.py | 13 +-- tools/dbus-latency.c | 116 +++++++++++++------------- tools/dbus-message-size.c | 6 +- tools/dbus-message-type.c | 102 +++++++++++----------- tools/dbus-messages-sent.py | 7 +- tools/dbus-top.c | 5 +- 16 files changed, 216 insertions(+), 159 deletions(-) create mode 100644 src/cc/export/host32.h create mode 100644 src/cc/export/host64.h create mode 100644 src/cc/hostcompat/linux/sched.h create mode 100644 src/cc/hostcompat/uapi/linux/ptrace.h diff --git a/packaging/bcc-tools.spec b/packaging/bcc-tools.spec index 134af996..7898480e 100644 --- a/packaging/bcc-tools.spec +++ b/packaging/bcc-tools.spec @@ -61,6 +61,7 @@ rm -rf %{buildroot} %{_libdir}/libbcc.so.* %{_prefix}/lib/python2.7/site-packages/bcc* %{_datadir}/bcc/* +%{_includedir}/bcc/hostcompat/* %files dbus %{_datadir}/bcc/tools/dbus-* diff --git a/src/cc/CMakeLists.txt b/src/cc/CMakeLists.txt index 7a4647c7..ad8c4312 100644 --- a/src/cc/CMakeLists.txt +++ b/src/cc/CMakeLists.txt @@ -72,6 +72,7 @@ install(TARGETS bcc-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${bcc_table_headers} DESTINATION include/bcc) install(FILES ${bcc_api_headers} DESTINATION include/bcc) install(DIRECTORY compat/linux/ DESTINATION include/bcc/compat/linux FILES_MATCHING PATTERN "*.h") +install(DIRECTORY hostcompat/ DESTINATION include/bcc/hostcompat FILES_MATCHING PATTERN "*.h") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbcc.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) endif(ENABLE_CLANG_JIT) install(FILES ${bcc_common_headers} DESTINATION include/bcc) diff --git a/src/cc/export/helpers.h b/src/cc/export/helpers.h index dbedb48e..c8a19de1 100644 --- a/src/cc/export/helpers.h +++ b/src/cc/export/helpers.h @@ -18,7 +18,9 @@ R"********( #define __BPF_HELPERS_H #include +#ifndef __arm__ #include +#endif #include #ifndef CONFIG_BPF_SYSCALL diff --git a/src/cc/export/host32.h b/src/cc/export/host32.h new file mode 100644 index 00000000..7ad59128 --- /dev/null +++ b/src/cc/export/host32.h @@ -0,0 +1,10 @@ +R"*****(#ifndef HOST32_H +#define HOST32_H + +typedef uint32_t uint_t; +typedef int32_t int_t; +typedef uint32_t ptr_t; +typedef uint32_t ulong_t; +typedef int32_t long_t; + +#endif // HOST32_H)*****" diff --git a/src/cc/export/host64.h b/src/cc/export/host64.h new file mode 100644 index 00000000..213a9bb1 --- /dev/null +++ b/src/cc/export/host64.h @@ -0,0 +1,10 @@ +R"*****(#ifndef HOST64_H +#define HOST64_H + +typedef uint64_t uint_t; +typedef int64_t int_t; +typedef uint64_t ptr_t; +typedef uint64_t ulong_t; +typedef int64_t long_t; + +#endif // HOST64_H)*****" diff --git a/src/cc/exported_files.cc b/src/cc/exported_files.cc index 448a6977..c440b74e 100644 --- a/src/cc/exported_files.cc +++ b/src/cc/exported_files.cc @@ -41,6 +41,14 @@ map ExportedFiles::headers_ = { "/virtual/lib/clang/include/stdarg.h", #include "clang/include/stdarg.h" }, + { + "/virtual/include/bcc/host32.h", + #include "export/host32.h" + }, + { + "/virtual/include/bcc/host64.h", + #include "export/host64.h" + }, }; } diff --git a/src/cc/frontends/clang/loader.cc b/src/cc/frontends/clang/loader.cc index c255dba4..23f1edcf 100644 --- a/src/cc/frontends/clang/loader.cc +++ b/src/cc/frontends/clang/loader.cc @@ -137,6 +137,8 @@ int ClangLoader::parse(unique_ptr *mod, TableStorage &ts, const st // noinline and optnone (if not always inlining). // Note that first argument is ignored in clang compilation invocation. vector flags_cstr({"-O0", "-O2", "-emit-llvm", "-I", dstack.cwd(), + "-I/usr/include/bcc", + "-I/usr/local/include/bcc", "-Wno-deprecated-declarations", "-Wno-gnu-variable-sized-type-not-at-end", "-Wno-pragma-once-outside-header", @@ -155,6 +157,13 @@ int ClangLoader::parse(unique_ptr *mod, TableStorage &ts, const st kflags.push_back("/virtual/include/bcc/bpf.h"); kflags.push_back("-include"); kflags.push_back("/virtual/include/bcc/helpers.h"); +#ifdef __arm__ + kflags.push_back("-include"); + kflags.push_back("/virtual/include/bcc/host32.h"); +#else + kflags.push_back("-include"); + kflags.push_back("/virtual/include/bcc/host64.h"); +#endif kflags.push_back("-isystem"); kflags.push_back("/virtual/include"); for (auto it = kflags.begin(); it != kflags.end(); ++it) @@ -187,7 +196,8 @@ int ClangLoader::parse(unique_ptr *mod, TableStorage &ts, const st #elif defined(__aarch64__) driver::Driver drv("", "aarch64-unknown-linux-gnu", diags); #elif defined(__arm__) - driver::Driver drv("", "armv7l-tizen-linux-gnueabi", diags); + driver::Driver drv("", "bpf-pc-linux", diags); + flags_cstr.push_back("-D__arm__"); #else driver::Driver drv("", "x86_64-unknown-linux-gnu", diags); #endif diff --git a/src/cc/hostcompat/linux/sched.h b/src/cc/hostcompat/linux/sched.h new file mode 100644 index 00000000..ecaccfac --- /dev/null +++ b/src/cc/hostcompat/linux/sched.h @@ -0,0 +1,10 @@ +#ifndef COMPAT_SCHED_H +#define COMPAT_SCHED_H + +#ifdef __arm__ +#define TASK_COMM_LEN 40 +#else +#include +#endif + +#endif // COMPAT_SCHED_H diff --git a/src/cc/hostcompat/uapi/linux/ptrace.h b/src/cc/hostcompat/uapi/linux/ptrace.h new file mode 100644 index 00000000..21c4c2d5 --- /dev/null +++ b/src/cc/hostcompat/uapi/linux/ptrace.h @@ -0,0 +1,13 @@ +#ifndef COMPAT_PTRACE_H +#define COMPAT_PTRACE_H + +#ifdef __arm__ +struct pt_regs +{ + uint32_t uregs[18]; +}; +#else +#include +#endif + +#endif // COMPAT_PTRACE_H diff --git a/tools/dbus-connection-message-size.c b/tools/dbus-connection-message-size.c index 1bdc3cc7..dd47d54a 100644 --- a/tools/dbus-connection-message-size.c +++ b/tools/dbus-connection-message-size.c @@ -1,18 +1,18 @@ -#include -#include +#include +#include struct DBusString { - void *str; - int len; - unsigned int allocated; - unsigned int constant : 1; - unsigned int locked : 1; - unsigned int invalid : 1; - unsigned int align_offset : 3; + ptr_t str; + int_t len; + uint_t allocated; + uint_t constant : 1; + uint_t locked : 1; + uint_t invalid : 1; + uint_t align_offset : 3; }; struct DBusHeaderFields { - int value_pos; + int_t value_pos; }; struct DBusHeader { @@ -26,32 +26,32 @@ struct DBusHeader { }; struct DBusMessage { - int refcount; + int_t refcount; struct DBusHeader header; struct DBusString body; - unsigned int locked : 1; + uint_t locked : 1; - void * list; - long size_counter_delta; - int timeout_ms; + ptr_t list; // void * + long_t size_counter_delta; + int_t timeout_ms; u32 changed_stamp : 21; - void *slot_list; + ptr_t slot_list; // void * - int generation; + int_t generation; - int *unix_fds; + ptr_t unix_fds; // int * - unsigned n_unix_fds; - unsigned n_unix_fds_allocated; + uint_t n_unix_fds; + uint_t n_unix_fds_allocated; - long unix_fd_counter_delta; + long_t unix_fd_counter_delta; - struct DBusString *signature; - struct DBusString *unique_sender; - size_t gvariant_body_last_offset; - size_t gvariant_body_last_pos; + ptr_t signature; // struct DBusString *signature; + ptr_t unique_sender; // struct DBusString *unique_sender; + ulong_t gvariant_body_last_offset; + ulong_t gvariant_body_last_pos; }; struct data_t { @@ -106,8 +106,7 @@ int g_get_message_size(struct pt_regs *ctx) { if (ptr_gsize == 0) { return 0; } - unsigned long *gsize = 0; - bpf_probe_read(&gsize, sizeof(gsize), ptr_gsize); + unsigned long *gsize = *ptr_gsize; unsigned long size = 0; bpf_probe_read(&size, sizeof(size), gsize); if(size > 0 && size < 1000000) { @@ -118,14 +117,12 @@ int g_get_message_size(struct pt_regs *ctx) { int dbus_message_size(struct pt_regs *ctx, void *conn, struct DBusMessage *message) { unsigned long size = 0; - int header_len = 0; - int body_len = 0; + int header_len = message->header.data.len; + int body_len = message->body.len; - bpf_probe_read(&header_len, sizeof(header_len), (char*)message + offsetof(struct DBusMessage, header.data.len)); if (header_len > 0) { size += header_len; } - bpf_probe_read(&body_len, sizeof(body_len), (char*)message + offsetof(struct DBusMessage, body.len)); if (body_len > 0) { size += body_len; } diff --git a/tools/dbus-glib-top.py b/tools/dbus-glib-top.py index 3be925be..8ac6312c 100755 --- a/tools/dbus-glib-top.py +++ b/tools/dbus-glib-top.py @@ -11,8 +11,9 @@ count = 100 interval = 5 loop = 0 # load BPF program -bpf_text="""#include -#include +bpf_text=""" +#include +#include typedef struct pdata_T { u32 pid; @@ -36,7 +37,7 @@ int do_entry_dbus(struct pt_regs *ctx) { u64 time = 0; pdata_t pdata; pdata_t new_pdata = {0, ' ', 0, 0, 0, 0, 0}; - u64 ts, *tsp, delta; + u64 ts, *tsp, delta; u32 pid; ts = bpf_ktime_get_ns() / 1000; @@ -68,7 +69,7 @@ int do_return_dbus(struct pt_regs *ctx) { u64 time = 0; pdata_t pdata; pdata_t new_pdata = {0, ' ', 0, 0, 0, 0, 0}; - u64 ts, *tsp, delta; + u64 ts, *tsp, delta; u32 pid; ts = bpf_ktime_get_ns() / 1000; @@ -99,7 +100,7 @@ int do_entry_glib(struct pt_regs *ctx) { u64 time = 0; pdata_t pdata; pdata_t new_pdata = {0, ' ', 0, 0, 0, 0, 0}; - u64 ts, *tsp, delta; + u64 ts, *tsp, delta; u32 pid; ts = bpf_ktime_get_ns() / 1000; @@ -131,7 +132,7 @@ int do_return_glib(struct pt_regs *ctx) { u64 time = 0; pdata_t pdata; pdata_t new_pdata = {0, ' ', 0, 0, 0, 0, 0}; - u64 ts, *tsp, delta; + u64 ts, *tsp, delta; u32 pid; ts = bpf_ktime_get_ns() / 1000; diff --git a/tools/dbus-latency.c b/tools/dbus-latency.c index 82f093c6..976206b1 100644 --- a/tools/dbus-latency.c +++ b/tools/dbus-latency.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include typedef enum { G_DBUS_MESSAGE_TYPE_INVALID, @@ -28,14 +28,14 @@ typedef enum } GDBusSendMessageFlags; struct GTypeInstance { - void *g_class; + ptr_t g_class; // void * }; struct GObject { struct GTypeInstance g_type_instance; - volatile unsigned int ref_count; - void *qdata; + volatile uint_t ref_count; + ptr_t qdata; // void * }; struct GDBusMessage { @@ -43,17 +43,17 @@ struct GDBusMessage { GDBusMessageType type; GDBusMessageFlags flags; bool locked; - GDBusMessageByteOrder byte_order; + uint_t byte_order; // GDBusMessageByteOrder byte_order; unsigned char major_protocol_version; unsigned int serial; - void *headers; - void *body; + ptr_t headers; // void *headers; + ptr_t body; // void *body; }; struct DBusString { - void *str; - int len; - unsigned int allocated; + ptr_t str; + int_t len; + uint_t allocated; unsigned int constant : 1; unsigned int locked : 1; unsigned int invalid : 1; @@ -61,13 +61,13 @@ struct DBusString { }; struct DBusLink { - struct DBusLink *prev; - struct DBusLink *next; - void *data; + ptr_t prev; // struct DBusLink *prev; + ptr_t next; // struct DBusLink *next; + ptr_t data; // void *data; }; struct DBusHeaderFields { - int value_pos; + int_t value_pos; }; struct DBusHeader { @@ -81,69 +81,69 @@ struct DBusHeader { }; struct DBusDataSlot { - void *data; - void *dummy1; + ptr_t data; // void *data; + ptr_t dummy1; // void *dummy1; }; struct DBusDataSlotList { - struct DBusDataSlot *slots; - int n_slots; + ptr_t slots; // struct DBusDataSlot *slots; + int_t n_slots; }; struct DBusList { - struct DBusList *prev; - struct DbusList *next; - void *data; + ptr_t prev; // struct DBusList *prev; + ptr_t next; // struct DbusList *next; + ptr_t data; // void *data; }; struct DBusMessage { - int refcount; + int_t refcount; struct DBusHeader header; struct DBusString body; - unsigned int locked : 1; - unsigned int in_cache : 1; + uint_t locked : 1; + uint_t in_cache : 1; - void *counters; - long size_counter_delta; - int timeout_ms; + ptr_t counters; // void *counters; + long_t size_counter_delta; + int_t timeout_ms; u32 changed_stamp : 21; - struct DBusDataSlotList *slot_list; + ptr_t slot_list; // struct DBusDataSlotList *slot_list; - int generation; + int_t generation; - int *unix_fds; + ptr_t unix_fds; // int *unix_fds; - unsigned n_unix_fds; - unsigned n_unix_fds_allocated; + uint_t n_unix_fds; + uint_t n_unix_fds_allocated; - long unix_fd_counter_delta; + long_t unix_fd_counter_delta; - struct DBusString *signature; - struct DBusString *unique_sender; - size_t gvariant_body_last_offset; - size_t gvariant_body_last_pos; + ptr_t signature; // struct DBusString *signature; + ptr_t unique_sender; // struct DBusString *unique_sender; + long_t gvariant_body_last_offset; + long_t gvariant_body_last_pos; }; struct DBusConnection { - int refcount; + int_t refcount; - void *mutex; + ptr_t mutex; // void *mutex; - void *dispatch_mutex; - void *dispatch_cond; - void *io_path_mutex; - void *io_path_cond; + ptr_t dispatch_mutex; // void *dispatch_mutex; + ptr_t dispatch_cond; // void *dispatch_cond; + ptr_t io_path_mutex; // void *io_path_mutex; + ptr_t io_path_cond; // void *io_path_cond; - struct DBusList *outgoing_messages; - struct DBusList *incoming_messages; - struct DBusList *expired_messages; + ptr_t outgoing_messages; // struct DBusList *outgoing_messages; + ptr_t incoming_messages; // struct DBusList *incoming_messages; + ptr_t expired_messages; // struct DBusList *expired_messages; - struct DBusMessage *message_borrowed; + ptr_t message_borrowed; // struct DBusMessage *message_borrowed; - int n_outgoing; - int n_incoming; + int_t n_outgoing; + int_t n_incoming; }; struct data_t { @@ -224,10 +224,8 @@ static int get_delay(int serial, struct data_t receiver_data) { int get_msg_addr(struct pt_regs *ctx, struct DBusConnection *conn, struct DBusMessage *msg){ struct data_t data = {}; get_process_data(&data); - struct header_data *header = 0; - bpf_probe_read(&header, sizeof(header), (char*)msg + offsetof(struct DBusMessage, header.data.str)); - u32 serial = 0; - bpf_probe_read(&serial, sizeof(serial), (char*)header + offsetof(struct header_data, serial)); + struct header_data *header = msg->header.data.str; + u32 serial = header->serial; msg_sent_addr.update(&serial,&data); return 0; } @@ -237,18 +235,16 @@ int get_msg_latency(struct pt_regs *ctx, struct DBusConnection *conn) { struct DBusMessage *msg = 0; struct header_data *header = 0; int serial = 0; - struct DBusList *link = 0; - - bpf_probe_read(&link, sizeof(link), (char *)conn + offsetof(struct DBusConnection, incoming_messages)); + struct DBusList *link = conn->incoming_messages; if (link == 0) { return 0; } get_process_data(&receiver_data); - bpf_probe_read(&msg, sizeof(msg), (char *)link + offsetof(struct DBusList, data)); - bpf_probe_read(&header, sizeof(header), (char*)msg + offsetof(struct DBusMessage, header.data.str)); - bpf_probe_read(&serial, sizeof(serial), (char*)header + offsetof(struct header_data, serial)); + msg = link->data; + header = msg->header.data.str; + serial = header->serial; get_delay(serial, receiver_data); return 0; } diff --git a/tools/dbus-message-size.c b/tools/dbus-message-size.c index b25fb889..592b3c0d 100644 --- a/tools/dbus-message-size.c +++ b/tools/dbus-message-size.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #define COUNTS COUNTS_T @@ -12,7 +12,7 @@ struct bytes_t { u64 bytes; }; -BPF_HASH(size_sent, struct data_t, struct bytes_t) +BPF_HASH(size_sent, struct data_t, struct bytes_t); BPF_HISTOGRAM(sizes); diff --git a/tools/dbus-message-type.c b/tools/dbus-message-type.c index bf79e7c9..6246d637 100644 --- a/tools/dbus-message-type.c +++ b/tools/dbus-message-type.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #define DBUS_TYPE_OFFSET 1 @@ -24,64 +24,64 @@ typedef enum } GDBusMessageByteOrder; struct GTypeInstance { - void *g_class; + ptr_t g_class; // void *g_class; }; struct GObject { struct GTypeInstance g_type_instance; - volatile unsigned int ref_count; - void *qdata; + volatile uint_t ref_count; + ptr_t qdata; // void *qdata; }; struct GHashTable { - int size; - int mod; - unsigned int mask; - int nnodes; - int noccupied; - - void *keys; - void *hashes; - void *values; + int_t size; + int_t mod; + uint_t mask; + int_t nnodes; + int_t noccupied; + + ptr_t keys; // void *keys; + ptr_t hashes; // void *hashes; + ptr_t values; // void *values; }; struct GVariant { - void *type_info; - unsigned long size; + ptr_t type_info; // void *type_info; + ulong_t size; union { struct { - void *bytes; - const char *gconstpointer; + ptr_t bytes; // void *bytes; + ptr_t gconstpointer; // const char *gconstpointer; } serialised; struct { - struct GVariant **children; - unsigned long n_children; + ptr_t children; // struct GVariant **children; + ulong_t n_children; } tree; } content; - int state; - int ref_count; + int_t state; + int_t ref_count; }; struct GDBusMessage { struct GObject parent_instance; - GDBusMessageType type; - GDBusMessageFlags flags; + uint_t type; // GDBusMessageType type; + uint_t flags; // GDBusMessageFlags flags; bool locked; - GDBusMessageByteOrder byte_order; + uint_t byte_order; // GDBusMessageByteOrder byte_order; unsigned char major_protocol_version; - unsigned int serial; - struct GHashTable *headers; - struct GVariant *body; + uint_t serial; + ptr_t headers; // struct GHashTable *headers; + ptr_t body; // struct GVariant *body; }; struct DBusString { - void *str; - int len; - unsigned int allocated; + ptr_t str; // void *str; + int_t len; + uint_t allocated; unsigned int constant : 1; unsigned int locked : 1; unsigned int invalid : 1; @@ -89,7 +89,7 @@ struct DBusString { }; struct DBusHeaderFields { - int value_pos; + int_t value_pos; }; struct DBusHeader { @@ -103,32 +103,32 @@ struct DBusHeader { }; struct DBusMessage { - int refcount; + int_t refcount; struct DBusHeader header; struct DBusString body; - unsigned int locked : 1; + uint_t locked : 1; - void * list; - long size_counter_delta; - int timeout_ms; + ptr_t list; // void * list; + long_t size_counter_delta; + int_t timeout_ms; u32 changed_stamp : 21; - void *slot_list; + ptr_t slot_list; // void *slot_list; - int generation; + int_t generation; - int *unix_fds; + ptr_t unix_fds; // int *unix_fds; - unsigned n_unix_fds; - unsigned n_unix_fds_allocated; + uint_t n_unix_fds; + uint_t n_unix_fds_allocated; - long unix_fd_counter_delta; + long_t unix_fd_counter_delta; - struct DBusString *signature; - struct DBusString *unique_sender; - size_t gvariant_body_last_offset; - size_t gvariant_body_last_pos; + ptr_t signature; // struct DBusString *signature; + ptr_t unique_sender; // struct DBusString *unique_sender; + uint_t gvariant_body_last_offset; + uint_t gvariant_body_last_pos; }; struct data_t { @@ -150,10 +150,8 @@ int dbus_get_message_type(struct pt_regs *ctx, void *conn, struct DBusMessage *m } int message_type(struct pt_regs *ctx, void *conn, struct DBusMessage *message) { - char *addr = 0; - bpf_probe_read(&addr, sizeof(addr), (char*)message + offsetof(struct DBusMessage, header.data.str)); - char type; - bpf_probe_read(&type, sizeof(type), addr + 1); - msg_type.increment(type); + const char *c = message->header.data.str; + c++; + msg_type.increment(*c); return 0; } diff --git a/tools/dbus-messages-sent.py b/tools/dbus-messages-sent.py index 2e915933..b2f9dbe5 100755 --- a/tools/dbus-messages-sent.py +++ b/tools/dbus-messages-sent.py @@ -11,11 +11,12 @@ count = 100 interval = 5 loop = 0 # load BPF program -bpf_text="""#include -#include +bpf_text=""" +#include +#include struct p_name{ - char buf[80]; + char buf[TASK_COMM_LEN]; u32 pid; }; diff --git a/tools/dbus-top.c b/tools/dbus-top.c index 76fe5dab..a31b44db 100644 --- a/tools/dbus-top.c +++ b/tools/dbus-top.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include typedef struct pdata_T { u32 pid; @@ -58,4 +58,3 @@ int do_return(struct pt_regs *ctx) { stats.update(&pid, &pdata); return 0; } - -- 2.34.1