Skip to content

Add check that gdb is on the path #4420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 82 additions & 69 deletions unit/memory-analyzer/gdb_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,19 @@ Author: Malte Mues <mail.mues@gmail.com>
#ifdef RUN_GDB_API_TESTS

#include <cstdio>
#include <cstdlib>
#include <regex>
#include <string>

#include <fstream>
#include <iostream>

#include <memory-analyzer/gdb_api.cpp>
#include <util/run.h>

void compile_test_file()
{
std::string test_file("memory-analyzer/test.c");

std::string cmd("gcc -g -o test ");
cmd += test_file;

const int r = system(cmd.c_str());
REQUIRE(!r);
REQUIRE(
run("gcc", {"gcc", "-g", "-o", "test", "memory-analyzer/test.c"}) == 0);
}

class gdb_api_testt : public gdb_apit
Expand Down Expand Up @@ -99,99 +94,117 @@ void gdb_api_internals_test()
}
}

bool check_for_gdb()
{
const bool has_gdb = run("gdb", {"gdb", "--version"}) == 0;

SECTION("check gdb is on the PATH")
{
REQUIRE(has_gdb);
}
return has_gdb;
}

#endif

TEST_CASE("gdb api internals test", "[core][memory-analyzer]")
{
#ifdef RUN_GDB_API_TESTS
gdb_api_internals_test();
if(check_for_gdb())
{
gdb_api_internals_test();
}
#endif
}

TEST_CASE("gdb api test", "[core][memory-analyzer]")
{
#ifdef RUN_GDB_API_TESTS
compile_test_file();

if(check_for_gdb())
{
gdb_apit gdb_api("test", true);
gdb_api.create_gdb_process();
compile_test_file();

try
{
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
REQUIRE(r);
}
catch(const gdb_interaction_exceptiont &e)
{
std::cerr << "warning: cannot fully unit test GDB API as program cannot "
<< "be run with gdb\n";
std::cerr << "warning: this may be due to not having the required "
<< "permissions (e.g., to invoke ptrace() or to disable ASLR)"
<< "\n";
std::cerr << "gdb_interaction_exceptiont:" << '\n';
std::cerr << e.what() << '\n';

std::ifstream file("gdb.txt");
CHECK_RETURN(file.is_open());
std::string line;

std::cerr << "=== gdb log begin ===\n";
gdb_apit gdb_api("test", true);
gdb_api.create_gdb_process();

while(getline(file, line))
try
{
std::cerr << line << '\n';
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
REQUIRE(r);
}
catch(const gdb_interaction_exceptiont &e)
{
std::cerr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we use message handlers in unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least not consistently, I found a few other tests that call std:cerr. Should I modify this one?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterschrammel What value would the use of message handlers add here?

<< "warning: cannot fully unit test GDB API as program cannot "
<< "be run with gdb\n";
std::cerr << "warning: this may be due to not having the required "
<< "permissions (e.g., to invoke ptrace() or to disable ASLR)"
<< "\n";
std::cerr << "gdb_interaction_exceptiont:" << '\n';
std::cerr << e.what() << '\n';

file.close();

std::cerr << "=== gdb log end ===\n";
std::ifstream file("gdb.txt");
CHECK_RETURN(file.is_open());
std::string line;

return;
}
}
std::cerr << "=== gdb log begin ===\n";

gdb_apit gdb_api("test");
gdb_api.create_gdb_process();
while(getline(file, line))
{
std::cerr << line << '\n';
}

SECTION("breakpoint is hit")
{
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
REQUIRE(r);
}
file.close();

SECTION("breakpoint is not hit")
{
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
REQUIRE(!r);
}
std::cerr << "=== gdb log end ===\n";

SECTION("breakpoint does not exist")
{
REQUIRE_THROWS_AS(
gdb_api.run_gdb_to_breakpoint("checkpoint3"), gdb_interaction_exceptiont);
}
return;
}
}

SECTION("query memory")
{
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
REQUIRE(r);
gdb_apit gdb_api("test");
gdb_api.create_gdb_process();

REQUIRE(gdb_api.get_value("x") == "8");
REQUIRE(gdb_api.get_value("s") == "abc");
SECTION("breakpoint is hit")
{
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
REQUIRE(r);
}

const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)");
SECTION("breakpoint is not hit")
{
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
REQUIRE(!r);
}

SECTION("breakpoint does not exist")
{
std::string address = gdb_api.get_memory("p");
REQUIRE(std::regex_match(address, regex));
REQUIRE_THROWS_AS(
gdb_api.run_gdb_to_breakpoint("checkpoint3"),
gdb_interaction_exceptiont);
}

SECTION("query memory")
{
std::string address = gdb_api.get_memory("vp");
REQUIRE(std::regex_match(address, regex));
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
REQUIRE(r);

REQUIRE(gdb_api.get_value("x") == "8");
REQUIRE(gdb_api.get_value("s") == "abc");

const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)");

{
std::string address = gdb_api.get_memory("p");
REQUIRE(std::regex_match(address, regex));
}

{
std::string address = gdb_api.get_memory("vp");
REQUIRE(std::regex_match(address, regex));
}
}
}

#endif
}
1 change: 1 addition & 0 deletions unit/memory-analyzer/module_dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
memory-analyzer
testing-utils
util