Skip to content
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

Add start_and_break_on decorator to simplify gdb tests #2146

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/gdb-tests/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
from __future__ import annotations

import functools

import gdb

import tests

from . import binaries


def start_and_break_on(binary, bps, *args):
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we make it a pytest fixture instead of a decorator?

Copy link
Member Author

Choose a reason for hiding this comment

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

Two reasons:

  1. A function that the user could just import themselves is not a good pytest fixture. The way we use start_binary right now is not great. If we instead automatically started it for them and passed in the process object, that would be a better fixture, but then the user can't customize the binary, breakpoints, etc.
  2. Even if we pass this function in as a fixture, the user still has to call it as one of the first things they do. Tests are usually better when the initialization has happened before the function is even called, which can be done with a decorator.

Copy link
Member Author

Choose a reason for hiding this comment

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

@disconnect3d lmk what you think. I prefer this approach and was going to migrate some other tests to it, but will wait until you let me know you're ok with it.

Copy link
Member

Choose a reason for hiding this comment

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

@gsingh93 I think making it a decorator makes things more complex unnecessarily? Why not just a standard function call that would be called within the test function instead? Some tests could want to call it multiple times too.

Regarding fixtures, the fixtures are kinda broken bcoz we never run tests all together but instead we run them each one by one. Fixtures allows for 'session fixtures' where the fixture would be called once for all tests in a test session etc. Another good thing of fixtures is that u can yield in them and whatever code is after the yield will be executed after the test finishes, as a kind of 'destructor'. But yeah, maybe its not so needed here.

Copy link
Member

Choose a reason for hiding this comment

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

@gsingh93 ping :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think making it a decorator makes things more complex unnecessarily?

Do you mean it makes things more complex for the test writer or just in the testing framework in general?

I do think it makes the testing framework a bit more complicated, but I think declarative approaches like this are better in a lot of cases, especially for tests.

That being said, I think even just calling a function like you said would be better than the current fixture approach, so if you still don't want the decorator, I'll change it to that.

BTW, part of the motivation for this was thinking of a possible future extension to the testing framework that could look like this:

@binary("linked-lists.out")
class TestSomeFeature:
    @on_break("break1")
    def test_on_break1(self):
        # run some test code when we hit break1

    @on_break("break2")
    def test_on_break2(self):
        # run some test code when we hit break2

Essentially we could just define functions that would test some functionality when the specific breakpoint in on_break was hit. No need to manually start the binary, set breakpoints, or continue, everything is handled for you and the test author literally only writes the test code.

I think this could be useful later in the LLDB rewrite, if we abstract all the code dealing with GDB away right now, then later we can just modify the test framework to handle LLDB and the tests should still work.

Copy link
Member

Choose a reason for hiding this comment

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

I mean its generally more code and more indirection. Not everyone is a Python expert and knows how exactly decorators work, when and how to use it, it requires looking it up and so on.

(There is a similar problem with pytest fixtures, but yeah)

The future extension looks nice, but anytime something breaks and someone would have to debug it, they will have harder time to do it due to all misdirections :P

Copy link
Member

Choose a reason for hiding this comment

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

So yeah, lets maybe switch to calling a function for now just for the sake of simplicity (?)

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
path = binaries.get(binary)

gdb.execute("file " + path)
gdb.execute("set exception-verbose on")
gdb.execute("starti " + " ".join(args))

if bps is not None and len(bps) > 0:
for bp in bps:
gdb.execute(f"break {bp}")
gdb.execute("continue")

return func(*args, **kwargs)

return wrapper

return decorator
37 changes: 16 additions & 21 deletions tests/gdb-tests/tests/test_command_plist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@

import gdb

import tests
from . import start_and_break_on

LINKED_LISTS_BINARY = tests.binaries.get("linked-lists.out")


def startup(start_binary):
start_binary(LINKED_LISTS_BINARY)

gdb.execute("break break_here")
gdb.execute("run")
gdb.execute("up")


def test_command_plist_flat_no_flags(start_binary):
@start_and_break_on("linked-lists.out", ["break_here"])
def test_command_plist_flat_no_flags():
"""
Tests the plist for a non-nested linked list
"""
startup(start_binary)
gdb.execute("up")

expected_out = re.compile(
"""\
Expand All @@ -43,11 +34,12 @@ def test_command_plist_flat_no_flags(start_binary):
assert expected_out.match(result_str) is not None


def test_command_plist_flat_field(start_binary):
@start_and_break_on("linked-lists.out", ["break_here"])
def test_command_plist_flat_field():
"""
Tests the plist command for a non-nested linked list with field flag
"""
startup(start_binary)
gdb.execute("up")

expected_out = re.compile(
"""\
Expand All @@ -61,11 +53,12 @@ def test_command_plist_flat_field(start_binary):
assert expected_out.match(result_str) is not None


def test_command_plist_flat_sentinel(start_binary):
@start_and_break_on("linked-lists.out", ["break_here"])
def test_command_plist_flat_sentinel():
"""
Tests the plist command for a non-nested linked list with field flag
"""
startup(start_binary)
gdb.execute("up")

sentinel = int(gdb.lookup_symbol("node_c")[0].value().address)
expected_out = re.compile(
Expand All @@ -84,11 +77,12 @@ def test_command_plist_flat_sentinel(start_binary):
assert expected_out.match(result_str) is not None


def test_command_plist_nested_direct(start_binary):
@start_and_break_on("linked-lists.out", ["break_here"])
def test_command_plist_nested_direct():
"""
Tests the plist for a nested linked list pointing to the outer structure
"""
startup(start_binary)
gdb.execute("up")

expected_out = re.compile(
"""\
Expand Down Expand Up @@ -116,11 +110,12 @@ def test_command_plist_nested_direct(start_binary):
assert expected_out.match(result_str) is not None


def test_command_plist_nested_indirect(start_binary):
@start_and_break_on("linked-lists.out", ["break_here"])
def test_command_plist_nested_indirect():
"""
Tests the plist for a nested linked list pointing to the inner structure
"""
startup(start_binary)
gdb.execute("up")

expected_out = re.compile(
"""\
Expand Down