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 May 22, 2024
1 parent 4d52311 commit 1300bac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/aot/aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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";

// AOT payload will have this header at the beginning. We don't worry about
Expand Down Expand Up @@ -190,7 +189,7 @@ int generate(const RequiredResources &resources,
if (!section)
return 1;

auto shim = find_in_path(AOT_SHIM_NAME);
auto shim = find_in_path(AOT_SHIM_NAME.data());
if (!shim) {
LOG(ERROR) << "Failed to locate " << AOT_SHIM_NAME
<< " shim binary. Is it in $PATH?";
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 {

static constexpr std::string_view AOT_SHIM_NAME = "bpftrace-aotrt";

int generate(const RequiredResources &resources,
const std::string &out,
void *const elf,
Expand Down
22 changes: 15 additions & 7 deletions src/aot/aot_main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <filesystem>
#include <fstream>
#include <getopt.h>
#include <iostream>
Expand All @@ -11,10 +12,10 @@

using namespace bpftrace;

void usage()
void usage(std::string_view 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 @@ -70,6 +71,13 @@ int main(int argc, char* argv[])
option{ nullptr, 0, nullptr, 0 }, // Must be last
};

std::filesystem::path p(argv[0]);
if (p.filename() == aot::AOT_SHIM_NAME) {
LOG(ERROR) << "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 @@ -79,7 +87,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 @@ -91,13 +99,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 @@ -108,7 +116,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 1300bac

Please sign in to comment.