Skip to content

Commit

Permalink
aot: Allow running aot binaries directly without calling runtime shim
Browse files Browse the repository at this point in the history
Currently, even though we make a copy of the runtime shim and then
embed the necessary metadata + bytecode into the binary, you still need
to pass that binary as an argument to the runtime shim in order to run
it (i.e. you need to do 'bpftrace-aotrt <file_name>'). Since the binary
can fully function on its own, we don't need to do this anymore.

This commit makes the executable just look inside itself for the
metadata and bytecode, skipping the need to call bpftrace-aotrt. (i.e.
you can now run the binary with just ./<file_name>.)
  • Loading branch information
arthurshau committed Apr 10, 2024
1 parent f6f113f commit 1b30ca2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/aot/aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#define AOT_ELF_SECTION ".btaot"
static constexpr auto AOT_MAGIC = 0xA07;
static constexpr auto AOT_SHIM_NAME = "bpftrace-aotrt";
static constexpr auto AOT_SECDATA_TEMPFILE = ".temp_btaot";

#define elf_error(fmt, ...) \
Expand Down
2 changes: 2 additions & 0 deletions src/aot/aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace bpftrace {
namespace aot {

constexpr auto AOT_SHIM_NAME = "bpftrace-aotrt-TEMP";

int generate(const RequiredResources &resources,
const BpfBytecode &bytecode,
const std::string &out);
Expand Down
20 changes: 13 additions & 7 deletions src/aot/aot_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

using namespace bpftrace;

void usage()
void usage(char* filename)
{
// clang-format off
std::cerr << "USAGE: bpftrace-aotrt filename" << std::endl;
std::cerr << "USAGE: " << filename << " [options]" << std::endl;
std::cerr << std::endl;
std::cerr << "OPTIONS:" << std::endl;
std::cerr << " -f FORMAT output format ('text', 'json')" << std::endl;
Expand Down Expand Up @@ -69,6 +69,12 @@ int main(int argc, char* argv[])
option{ nullptr, 0, nullptr, 0 }, // Must be last
};

if (strcmp(argv[0], aot::AOT_SHIM_NAME) == 0) {
LOG(INFO) << "Runtime shim should not be run directly, please generate a "
"binary using --aot option in bpftrace";
return 1;
}

while ((c = getopt_long(argc, argv, short_opts, long_opts, nullptr)) != -1) {
switch (c) {
case 'o':
Expand All @@ -78,7 +84,7 @@ int main(int argc, char* argv[])
output_format = optarg;
break;
case 'h':
usage();
usage(argv[0]);
return 0;
case 'V':
std::cout << "bpftrace " << BPFTRACE_VERSION << std::endl;
Expand All @@ -90,13 +96,13 @@ int main(int argc, char* argv[])
bt_verbose = true;
break;
default:
usage();
usage(argv[0]);
return 1;
}
}

if (!argv[optind]) {
usage();
if (argv[optind]) {
usage(argv[0]);
return 1;
}

Expand All @@ -105,7 +111,7 @@ int main(int argc, char* argv[])
return 1;

BPFtrace bpftrace(std::move(output));
int err = aot::load(bpftrace, argv[optind]);
int err = aot::load(bpftrace, argv[0]);
if (err) {
LOG(ERROR) << "Failed to load AOT script";
return err;
Expand Down

0 comments on commit 1b30ca2

Please sign in to comment.