Debugging Concurrency Is Hard, but We Can Do Something About It!

When we have to debug applications that use concurrency, perhaps written in Java, all we get from the debugger is a list of threads, perhaps some information about held locks, and the ability to step through each thread separately.

What we usually don’t get is any kind of high-level support for the concurrency abstractions that we used in our applications. We might have used a library for actors, channels from CSP, Java’s fork/join pool, or perhaps even some software transactional memory. This means, in the debugger, we cannot reason anymore about the same concepts we used to built the application. Instead, we can only vaguely recognize them from how the threads interact. We might see that one thread puts an object, which is perhaps a message, into another object, which represents a mailbox. But we don’t get any support to actually follow that message as it is being processed. We also have no way of jumping from the point where we spawn a fork/join task to its activation to see the computation unfold recursively and in parallel. And wouldn’t it be nice if we could simple set a breakpoint on a promise and see where we continue executing when it got resolved?

I am pretty sure these things would make it easier to understand what’s going on in my applications. Unfortunately, debugger support for such high-level concepts is rare or non-existing.

One of the biggest problems is that there are too many different concurrency models, concepts, languages, and libraries. This means designing debugging support for all these variations is a huge problem. And using the same debugger for Akka actors, Erlang, Pony, or your favorite actor library doesn’t seem feasible. Not to mention all those other systems. You want to debug goroutines and their use of channels, Clojure’s core.async, or perhaps Java’s JCSP?

Turns out, building a debugger for all these things isn’t impossible. We can design the debugger in a way that it can remain completely oblivious of the various concurrency concepts. This means, it can be used for a wide range of concurrent systems without having to be adapted specifically for them. To make this work, we propose the Kómpos protocol to communicate the necessary details from a language runtime to the debugger in a concurrency-agnostic way.

For a small demo, see the video below. The abstract for our paper describing the details of the approach, as well as links to it follow afterwards.

For questions or comments, please find me on Twitter @smarr.

Abstract

Today’s complex software systems combine high-level concurrency models. Each model is used to solve a specific set of problems. Unfortunately, debuggers support only the low-level notions of threads and shared memory, forcing developers to reason about these notions instead of the high-level concurrency models they chose.

This paper proposes a concurrency-agnostic debugger protocol that decouples the debugger from the concurrency models employed by the target application. As a result, the underlying language runtime can define custom breakpoints, stepping operations, and execution events for each concurrency model it supports, and a debugger can expose them without having to be specifically adapted.

We evaluated the generality of the protocol by applying it to SOMns, a Newspeak implementation, which supports a diversity of concurrency models including communicating sequential processes, communicating event loops, threads and locks, fork/join parallelism, and software transactional memory. We implemented 21 breakpoints and 20 stepping operations for these concurrency models. For none of these, the debugger needed to be changed. Furthermore, we visualize all concurrent interactions independently of a specific concurrency model. To show that tooling for a specific concurrency model is possible, we visualize actor turns and message sends separately.

  • A Concurrency-Agnostic Protocol for Multi-Paradigm Concurrent Debugging Tools, S. Marr, C. Torres Lopez, D. Aumayr, E. Gonzalez Boix, H. Mössenböck; In Proceedings of the 13th ACM SIGPLAN International Symposium on Dynamic Languages, DLS'17, ACM, 2017.
  • Paper: HTML, PDF
  • DOI: 10.1145/3133841.3133842
  • BibTex: bibtex
    @inproceedings{Marr:2017:CPCD,
      abstract = {Today's complex software systems combine high-level concurrency models. Each model is used to solve a specific set of problems. Unfortunately, debuggers support only the low-level notions of threads and shared memory, forcing developers to reason about these notions instead of the high-level concurrency models they chose.
      
      This paper proposes a concurrency-agnostic debugger protocol that decouples the debugger from the concurrency models employed by the target application. As a result, the underlying language runtime can define custom breakpoints, stepping operations, and execution events for each concurrency model it supports, and a debugger can expose them without having to be specifically adapted.
      
      We evaluated the generality of the protocol by applying it to SOMns, a Newspeak implementation, which supports a diversity of concurrency models including communicating sequential processes, communicating event loops, threads and locks, fork/join parallelism, and software transactional memory. We implemented 21 breakpoints and 20 stepping operations for these concurrency models. For none of these, the debugger needed to be changed. Furthermore, we visualize all concurrent interactions independently of a specific concurrency model. To show that tooling for a specific concurrency model is possible, we visualize actor turns and message sends separately.},
      acceptancerate = {0.64},
      author = {Marr, Stefan and Torres Lopez, Carmen and Aumayr, Dominik and Gonzalez Boix, Elisa and Mössenböck, Hanspeter},
      blog = {http://stefan-marr.de/2017/08/concurrency-agnostic-protocol-for-debugging/},
      booktitle = {Proceedings of the 13th ACM SIGPLAN International Symposium on Dynamic Languages},
      day = {24},
      doi = {10.1145/3133841.3133842},
      html = {http://stefan-marr.de/papers/dls-marr-et-al-concurrency-agnostic-protocol-for-debugging/},
      interhash = {fd0460a53c7ec4f41c14ccb10cc22a9f},
      intrahash = {9f119d2833c39979c7d679e49b067abe},
      isbn = {978-1-4503-5526-1/17/10},
      location = {Vancouver, Canada},
      month = oct,
      note = {(acceptance rate 64%)},
      numpages = {12},
      pdf = {http://stefan-marr.de/downloads/dls17-marr-et-al-concurrency-agnostic-protocol-for-debugging.pdf},
      publisher = {ACM},
      series = {DLS'17},
      title = {{A Concurrency-Agnostic Protocol for Multi-Paradigm Concurrent Debugging Tools}},
      year = {2017},
      month_numeric = {10}
    }
    

Hello Canterbury!

In case you have been reading the previous post or following me on Twitter or Facebook, you might know that I had the silly idea of cycling from Linz in Austria all the way to Canterbury in England.

And, I made it :)

After 12.5 days of cycling, I arrived in sunny Canterbury. My legs felt great, and my butt didn’t mind. It was a quite interesting journey, for large parts through Germany, but also the Netherlands, Belgium, and France. The third and forth day were the biggest challenges. The landscape was very nice, but very hilly. Compared to the ca. 100-120km on later days, I also cycled an extra 30-40km, which in combination was a little exhausting. Expect for those two days, I don’t think I pushed myself very much. On the contrary, it was pretty relaxed cycling. Especially along the Rhine the landscape was nice, the cycle paths pretty flat, and every 2km or so, there was another castle, which made the ride interesting.

Another thing I enjoyed was the absence of borders. I don’t even remember much signage. However, the cycling infrastructure could change noticeably all of a sudden. Germany, you could learn a lot from the Netherlands in that regard…

If you’re interested in my whole trip, the route was this:

More pictures and details on the separate days are on the Komoot pages: Day 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

It was the first time for me to do such a cycling trip. While I did train since April, cycling in the mornings, and a bit more on the weekends, I actually only did a single 120km trip before. From that perspective, I have to say, it all went surprisingly well.

And besides spending a few days in Brussels to see friends, and an extra day in Lille, because I missed it a little, the trip was completed pretty much in one go. Originally I planed another day of rest, but it wasn’t really necessary, and so I just had a short day in-between and then continued on.

The final day of the trip was also a short one. I suppose, I could have gone the whole way from Lille to Canterbury in a single day. But I wanted to spend a night in Calais, refreshing some good memories, having some crêpe and cider, looking out on the channel, and enjoying Europe for one more night.

The next morning, England welcomed me on a lovely sunny and warm late summer day. After a rather steep climb up the hill in Dover, to see the castle and take a scenic long-distance cycling path, I arrived in Canterbury. You see the picture above. Bright and sunny :)

And that’s where this story ends, and a whole new one starts: being a lecturer at the University of Kent.

Building High-level Debuggers for Concurrent Languages with Truffle: The Missing Bits

Note: This post is meant for people familiar with Truffle. For introductory material, please see for instance this list.

With its debugger support, Truffle provides a rich foundation for custom debugging features for a wide range of language concepts.

However, for our implementation of various breakpoint and stepping semantics for fork/join, threads and locks, software transactional memory, actors, and communicating processes, we needed a number of custom features, which somewhat duplicate part of the framework. One reason is that the debugger’s granularity is on the level of nodes, which can be too coarse-grained and requires restructuring the node implementations. Another reason is that some important aspects of concurrency are dealt with outside of the AST, for instance, in an event loop like JavaScript has it.

This post details our experience with Truffle and discusses the custom mechanisms that we implemented to deal with tradeoffs such as implementation complexity and fine-grained breakpoint semantics. Specifically, I am going into:

  1. custom breakpoint checks
  2. support for additional sequential stepping strategies
  3. support for activity-specific stepping strategies
  4. expression breakpoints
  5. Java conditions for breakpoints

Examples for High-level Breakpoints and Stepping

Before I go into what we did and what Truffle could still provide, I briefly give some examples of what wanted to achieve.

Languages such as JavaScript or Newspeak have the notion of event loops. Event loops provide a convenient abstraction to handle lightweight concurrency, for instance to react to events generated in a user interface or from external sources. However when debugging such applications, the callback-based programming style, with or without promises, inverts the control flow, and it becomes hard to reason about programs in a natural way.

In a debugger, it would thus be nice to be able to step through the callbacks in a promise chain and focus on their execution and effects. In our debugger, we offer such a mechanism with step to resolution, which breaks execution when callbacks are executed following the current promise being resolved with a value. On the operation that resolves the promise, we can also set a corresponding breakpoint. Thus, whenever we resolve a promise from the lexical location, we trigger the debugger as soon as the callbacks are executed.

Let’s look at a JavaScript example:

1
2
3
4
5
6
7
8
9
10
11
12
let rootPromise = new Promise((resolve, _) => {
  console.log("chance to capture resolve() and execute it");
  resolve("Promise done");
});

rootPromise.then(msg => {
  console.log("Step 1");
  return "Next step";
}).then(msg => {
  console.log("Step 2");
  return "Final step";
});

Ideally, we would be able to set a breakpoint on the resolve() call in line 3, which says that we want to break whenever a callback on the rootPromise is triggered. In this case, it would only be the one on line 7. Once reaching execution there, we might want to simply step to the next callback, which is triggered by resolving the promise that’s returned by then(), i.e., the one on line 10.

So, what exactly do we need in a Truffle language implementation to realize such breakpoints and stepping operations?

1. Custom Breakpoint Checks

One of the features not provided by Truffle is the ability to check for breakpoint information within a language implementation. The main reason is that everything should be done with wrapper nodes of the instrumentation API.

Unfortunately, there are some cases where that is not convenient, because we would need to restructure AST nodes. Or more importantly, it is not sufficient, because we need the information that a breakpoint was set at another point in the execution, and triggering it at the lexical location would not be useful. This is for instance the case for the example with the promises above.

To realize such promise breakpoints and stepping in SOMns, we set a flag for the breakpoint on the ‘message’ that is going to lead to the promise resolution. This looks something like this:

@Child BreakpointNode promiseResolverBreakpoint;

@Specialization
void sendPromiseMessage(Object[] args, Promise rcvr) {
  PromiseSendMessage msg = new PromiseSendMessage(args, rcvr,
      promiseResolverBreakpoint.executeShouldHalt());
  msg.send();
}

The key here is promiseResolverBreakpoint, which is an instance of our own BreakpointNode. The BreakpointNode works similar to Truffle’s builtin breakpoints and specializes itself on whether the breakpoint is set or not. So that at run time, there would not be any overhead for checking it, since it merely needs to return true or false as a compile-time constant.

One important difference to normal Truffle breakpoints is that our breakpoint nodes do not only have a source location, but also a type. This enables us to distinguish various different types of breakpoints for the same source location, which is important if one wants more complex operations than single stepping and line breakpoints.

The breakpoint node is roughly implemented as follows, skipping the breakpoint type here for brevity:

public abstract class BreakpointNode {

  protected final BreakpointEnabling bE;

  protected BreakpointNode(final BreakpointEnabling breakpoint) {
    this.bE = breakpoint;
  }

  public abstract boolean executeShouldHalt();

  @Specialization(assumptions = "bEUnchanged", guards = "!bE.enabled")
  public final boolean breakpointDisabled(
      @Cached("bE.unchanged") final Assumption bEUnchanged) {
    return false;
  }

  @Specialization(assumptions = "bEUnchanged", guards = "bE.enabled")
  public final boolean breakpointEnabled(
      @Cached("bE.unchanged") final Assumption bEUnchanged) {
    return true;
  }
}

public final class BreakpointEnabling {
  public boolean              enabled;
  public transient Assumption unchanged;

  BreakpointEnabling() {
    this.unchanged = Truffle.getRuntime().createAssumption("unchanged breakpoint");
    this.enabled = breakpointInfo.isEnabled();
  }

  public synchronized void setEnabled(final boolean enabled) {
    if (this.enabled != enabled) {
      this.enabled = enabled;
      this.unchanged.invalidate();
      this.unchanged = Truffle.getRuntime().createAssumption("unchanged breakpoint");
    }
  }
}

With this node in place, we can efficiently determine whether a breakpoint is set. When scheduling a callback, we can now check the flag on the message to see whether a breakpoint was set at the sending side. Triggering the breakpoint is however a little bit more involved, because we want it to trigger in the right position, but such callbacks are handled by event loops outside of a Truffle AST. This is going to be detailed in the next section.

For other types of breakpoints, it might be simpler, because we might already be at the right place in the AST. For these cases, we simple construct a node marked with Truffle’s AlwaysHalt tag, which ensures the debugger will trigger a breakpoint for us. After checking the condition, we simply execute the node like this:

@Specialization
Object doSomethingComplex(...) {
  // ...
  if (obj.breakpointWasSet) {
    suspendExec.executeGeneric(frame); // tagged with AlwaysHalt, triggers debugger
  }
  // ...
}

This ensures that Truffle triggers the breakpoint and uses its normal facilities to obtain information about current stack and local variables in the debugger.

2. Support for Additional Sequential Stepping Strategies

Back to the example of breaking when a callback is triggered from a promise.

As mentioned before, the main problem here is that we have the relevant information outside of the Truffle AST. This means, we cannot really trigger a debugger, and even if we could, the available state would possibly be confusing and not very helpful for the developers. What we do instead is to use a stepping strategy to execute the callback until it reaches a useful point. Fortunately, Truffle already has the notion of a RootTag to mark the first node of method that belongs to what a developer would consider the body of a method, i.e., ignoring possible pro- and epilogues.

We added a corresponding stepping strategy to Truffle to be able to say: execute this method until you reach the first node tagged with RootTag.

This is implemented in the following class:

class StepUntilNextRootNode extends SteppingStrategy {
  @Override
  boolean step(DebuggerSession s, EventContext ctx, SteppingLocation location) {
    return location == SteppingLocation.BEFORE_ROOT_NODE;
  }
}

For other kind of breakpoints, we also like to be able to step to the point after executing the root node. For that case we added a StepAfterNextRootNode strategy. This one is a little bit more complex, because we need to remember the first root found, and only trigger a suspension when we are in the AFTER_ROOT_NODE location for the same root node, and for the same stack height. This is necessary to account for recursion. Note, the AFTER_ROOT_NODE location is also a custom addition and comes with the corresponding AFTER_STATEMENT location, which allow us to set breakpoints or step to the point after which a node is executed.

Overall, I find stepping strategies a rather useful way of expressing what the debugger should do, and stopping after the execution of a node seems also useful. Unfortunately, the current design in Truffle does not support any extension by tools. While we only desired to step before and after (root) nodes for sequential debugging so far, general stepping strategies are another topic.

3. Activity-specific Stepping Strategies

To implement stepping from one turn of the event loop to the next, we use an entirely custom approach to stepping strategies. But the next turn is only one possible point we are interested in. Others could be more specific to a promise or a message to break at a corresponding callback or when the message is received.

For the various stepping operations, we have a thread-local field with the strategy, which then can be checked, depending on the type of strategy, either in the event loop or in the BreakpointNode.

In our event loop, we currently check for instance for the stepping to the next turn and to return to a promise resolution, which corresponds to following execution to the next promise chained to a then(.) in the JavaScript example from the beginning. In these cases, we set flags on the activity or promise objects to indicate a breakpoint later on.

For other stepping operations, for instance step to next promise resolution, we rely on a check in the BreakpointNode.

The previously shown code of BreakpointNode ignored this detail. So, it should look more like this:

@Specialization(assumptions = "bpUnchanged", guards = "!bp.enabled")
public final boolean breakpointDisabled(
    @Cached("bp.unchanged") final Assumption bpUnchanged) {
  return breakpoint.getSteppingType().isSet();
}

This means, if the breakpoint is disabled, we check whether the thread-local field with the stepping strategy is set to the type of the current breakpoint. This check is conditional on whether debugging is enabled, but has an overhead on the peak performance during a debugging session.

This seems to duplicate quite some of the mechanisms already included in Truffle for stepping. However, since we need different types of stepping strategies, it did not seem possible to integrate it into Truffle without approaching the question of how to design it in an extensible way.

4. Expression Breakpoints

After talking about the dynamics of breakpoints let’s briefly switch to setting breakpoints from a user interface. As you might imagine, that’s somewhat relevant to support fancy breakpoints in an IDE and expose them to users.

The biggest gap Truffle currently has here is that it misses an API to set breakpoints on expressions. Its Breakpoint.Builder currently only supports setting breakpoints on lines. If we consider the earlier JavaScript example with promises, we might want to set breakpoints on the then(.) or resolve(.) method calls, and perhaps chose what type of breakpoint to set. We could break just before executing the methods or when their consequences take effect.

We actually take two different approaches to the problem. For some use cases, for instance breaking before a call to resolve(.), we use a custom extension of the Breakpoint.Builder that supports filtering by source section in terms of line number, column, and section length, as well as a specific tag for the source section. So, in this case, we just rely on Truffle’s breakpoints with a more fine-grained way of setting them.

For other cases, we completely manage the breakpoints ourselves with the aforementioned BreakpointNode as discussed in sec. 1.

5. Java Conditions for Breakpoints

One final mechanism we rely on is the ability to set simple Java conditions on breakpoints. Currently, Truffle only supports setting conditions from the language itself. Thus, I could set a JavaScript condition to break on a line if some condition holds, for instance a variable has a certain value. However, there is no corresponding support to set conditions inside the implementation.

Our use case is to set breakpoints on functions that are triggered by messages, or in JavaScript it would correspond to set a breakpoint specifically for the case that a function is directly executed from the even loop, i.e., as a callback. This can be helpful for stepping or to trigger a breakpoint only for the case one is specifically interested in.

To realize this, we extended Breakpoint with a simple callback that returns a boolean. We use it to check whether the calling method is of a specific type, which signifies in SOMns that a method/function was activated via a message send, i.e., from the event loop.

Conclusion

In this post, I discussed a number of things we need to do in SOMns to get advanced breakpoints for various concurrency models. Some of these duplicate mechanisms in Truffle, which could perhaps just be leveraged if they would be designed to be more extensible. This includes the notion of breakpoints, which should allow the notion of different types on the same line/expression. It also includes nodes that allow run-time queries of a breakpoint status to propagate it as part of the application, and stepping strategies that can be queried and acted upon outside the Truffle AST, i.e., for instance as part of an event loop.

The other things I am missing in Truffle could be offered by extending the existing implementation. This includes additional sequential stepping strategies and locations, expression breakpoints, and simple condition on breakpoints.

These facilities could make a major difference for people interested in building better debuggers for dynamic languages. Looking at other people’s debuggers, for instance IntelliJ IDEA’s Java support or Chrome’s step into async, their extensions look cool and very helpful, but with the power we have in Truffle, they are more than trivial! It’s a shame to leave all the potential unused. Dynamic languages such as Ruby, JavaScript, Python, or R could really benefit from great debuggers, because most productive development is done in the debugger, where you see what you do.

Older Posts

Subscribe via RSS