Commit 2ee7b564 authored by Alastair Robertson's avatar Alastair Robertson

Stop running and print out maps on ctrl-c

parent 9bedb630
#include <iostream> #include <iostream>
#include <tuple>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <unistd.h> #include <unistd.h>
......
#pragma once #pragma once
#include "bpftrace.h" #include "types.h"
namespace bpftrace { namespace bpftrace {
......
...@@ -22,9 +22,8 @@ int BPFtrace::add_probe(ast::Probe &p) ...@@ -22,9 +22,8 @@ int BPFtrace::add_probe(ast::Probe &p)
return 0; return 0;
} }
int BPFtrace::run() int BPFtrace::start()
{ {
std::vector<std::unique_ptr<AttachedProbe>> attached_probes;
for (Probe &probe : probes_) for (Probe &probe : probes_)
{ {
auto func = sections_.find(probe.name); auto func = sections_.find(probe.name);
...@@ -35,7 +34,7 @@ int BPFtrace::run() ...@@ -35,7 +34,7 @@ int BPFtrace::run()
} }
try try
{ {
attached_probes.push_back(std::make_unique<AttachedProbe>(probe, func->second)); attached_probes_.push_back(std::make_unique<AttachedProbe>(probe, func->second));
} }
catch (std::runtime_error e) catch (std::runtime_error e)
{ {
...@@ -43,13 +42,14 @@ int BPFtrace::run() ...@@ -43,13 +42,14 @@ int BPFtrace::run()
return -1; return -1;
} }
} }
// TODO wait here while script is running
getchar();
return 0; return 0;
} }
void BPFtrace::stop()
{
attached_probes_.clear();
}
int BPFtrace::print_maps() int BPFtrace::print_maps()
{ {
for(auto &mapmap : maps_) for(auto &mapmap : maps_)
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include "ast.h" #include "ast.h"
#include "attached_probe.h"
#include "map.h" #include "map.h"
#include "types.h" #include "types.h"
...@@ -15,7 +16,8 @@ class BPFtrace ...@@ -15,7 +16,8 @@ class BPFtrace
public: public:
virtual ~BPFtrace() { } virtual ~BPFtrace() { }
virtual int add_probe(ast::Probe &p); virtual int add_probe(ast::Probe &p);
int run(); int start();
void stop();
int print_maps(); int print_maps();
std::map<std::string, std::unique_ptr<Map>> maps_; std::map<std::string, std::unique_ptr<Map>> maps_;
...@@ -23,6 +25,8 @@ public: ...@@ -23,6 +25,8 @@ public:
private: private:
std::vector<Probe> probes_; std::vector<Probe> probes_;
std::vector<std::unique_ptr<AttachedProbe>> attached_probes_;
int print_map(Map &map); int print_map(Map &map);
int print_map_quantize(Map &map); int print_map_quantize(Map &map);
int print_quantize(std::vector<uint64_t> values); int print_quantize(std::vector<uint64_t> values);
......
#include <iostream> #include <iostream>
#include <signal.h>
#include <unistd.h> #include <unistd.h>
#include "bpftrace.h" #include "bpftrace.h"
#include "codegen_llvm.h" #include "codegen_llvm.h"
#include "driver.h" #include "driver.h"
...@@ -89,10 +91,23 @@ int main(int argc, char *argv[]) ...@@ -89,10 +91,23 @@ int main(int argc, char *argv[])
if (debug) if (debug)
return 0; return 0;
err = bpftrace.run(); // Empty signal handler just to break out of the sleep below
struct sigaction act;
act.sa_handler = [](int) { };
sigaction(SIGINT, &act, NULL);
std::cout << "Running... press Ctrl-C to stop" << std::endl;
err = bpftrace.start();
if (err) if (err)
return err; return err;
sleep(99999999);
bpftrace.stop();
std::cout << "\n\n";
err = bpftrace.print_maps(); err = bpftrace.print_maps();
if (err) if (err)
return err; return err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment