Skip to content

Improve hello_debugger - #20

Closed
amaiorano wants to merge 1 commit into
masterfrom
improve-hello-debugger
Closed

Improve hello_debugger#20
amaiorano wants to merge 1 commit into
masterfrom
improve-hello-debugger

Conversation

@amaiorano

Copy link
Copy Markdown
Member

Make it so that the debugger is actually looping through all lines while
in running, so that pause stops on whatever current line it's on, or
setting a breakpoint will eventually hit.

Also fixed breakpoint verified state for the last line, as line numbers
are 1-based.

Make it so that the debugger is actually looping through all lines while
in running, so that pause stops on whatever current line it's on, or
setting a breakpoint will eventually hit.

Also fixed breakpoint verified state for the last line, as line numbers
are 1-based.
Debugger(const EventHandler&);

// run() instructs the debugger to continue execution.
// runs the debugger's main loop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: The documentation pattern I've been using for functions / methods is verb() does blah...
Also super-dooper-uber-nit, fullstops.

@amaiorano amaiorano Feb 25, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No problem, can do.

}

void Debugger::resume() {
running = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Although you're using an atomic, this is racy. running can be set to false at line 143.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure I see the problem here. If a separate thread were to set running to false at line 143 at the same time as another thread sets it to true here, things still remain consistent. In effect, hitting a breakpoint and pressing continue at the same time would mean one of them would win. In reality, the UI wouldn't let you press continue unless you were already paused, so it should be fine.

Maybe I'm not seeing it, though. What would you suggest here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My concern is that whenever I see the usage of std::atomic<bool> as signals there's usually a race condition. Here there is, but it looks like it'll recover.
However, this is example code, and I'm not sure we should be demonstrating patterns that can hide raciness.

Ideally in this sort of set up, I like to use a thread-safe queue of events for controlling the worker, and a query API for current state. This seems overkill for what we have here. Instead, we can adapt the Event a bit, and still keep things synchronized with a mutex lock: ben-clayton@3027fc0

What do you think?

@amaiorano amaiorano Feb 25, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instead, we can adapt the Event a bit, and still keep things synchronized with a mutex lock: ben-clayton/cppdap@3027fc0
What do you think?

Yes, I like this a lot more. I think it 'test' should be called 'test_and_reset' to make it clear that it resets the fire state, but yeah, your solution of using two events is much better. In fact, originally I had wanted to query to state of the "canRun" event, but saw that it didn't have that, and figured an atomic would do the same; but it's less clean, as you pointed out.

EDIT: have you tested it? I'll give it a go later to see if it behaves as expected. Am a bit concerned with the sleep(100ms) in there, as it may slow down stepping. I think we only want to do that when we're not paused.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

test_and_reset - maybe, it's a bit verbose though. Note that wait() also resets. I did update to comments on both. FWIW, a test() without resetting the signal state is also a bad idea for all the reasons I described above.

The sleep is probably quite excessive. Feel free to reduce it.

Yes, I gave it a quick test. Seemed to do the trick.

Comment thread examples/hello_debugger.cpp
@ben-clayton

Copy link
Copy Markdown
Contributor

@amaiorano - what do you want to do with this PR? Do you want me to adopt it, close it?

@amaiorano

Copy link
Copy Markdown
Member Author

Sorry for not responding earlier. I suppose we could close this. I think it would be nice to have a sample that's a bit closer to how a debugger would actually work.

@amaiorano amaiorano closed this Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants