acton icon indicating copy to clipboard operation
acton copied to clipboard

Add `#line` directives to generated C code for better traceback output

Open plajjan opened this issue 2 years ago • 3 comments

Now that we have exceptions, it would be really nice to get the source file / module and line number where it occurs. Since we compile through C the line number obviously do not line up. However, there is this #line directive where we can tell the compiler what the current line is and reset its counter.

Could we use this? Is the source line number & file name kept through the compiler all the way to codegen? Even an approximate location is better than no location at all and we can improve on the precision gradually.

Is there another way to record line numbers that we think is better?

Here's a simple example:

#include <stdio.h>
#include <assert.h>

int main() {
    printf("Hello, World!\n");

    #line 300 "hiaho.act"
    assert(0); // This will fail

    return 0;
}

which prints:

kll@Boxy:~/terastream/acton$ gcc linenum.c 
kll@Boxy:~/terastream/acton$ ./a.out 
Hello, World!
a.out: hiaho.act:300: main: Assertion `0' failed.
Aborted

If we add a file & line number arg to our $RAISE function we could use it directly?

    #line 30 "foo.act"
    $RAISE(__LINE__, __FILE__, (B_BaseException)$NEW(B_ValueError, to$str("cannot do the foobar")));
Unhandled exception in actor: foo.main[-12] at foo.act:30:
  ValueError: cannot do the foobar

plajjan avatar Sep 17 '23 06:09 plajjan

Hmm I guess the big downside is that by adding this we mess up segfault debugging with gdb? but that's like acceptable I guess... that should be very rare. Maybe we can have an actonc option to turn off emitting #line directives, thus maintaining the original C lines when doing gdb debugging but otherwise we map to acton source code lines.

plajjan avatar Sep 17 '23 06:09 plajjan

Okay so for the very simple case of getting line numbers just for where the exception is raised, we can just add line / file args to like $RAISE, print those and in codegen we insert the line and file as constants. That's actually a good starting point for this, maybe we should start with just that?

What I really had in mind for line and file and why it would make sense to set with #line is that we could use that in a traceback to get not just the last entry but the rest of the stack... for CPS converted code this obviously doesn't work but that's difficult regardless and this can still be useful, I suppose?

plajjan avatar Sep 17 '23 13:09 plajjan

I think this is an excellent idea, shouldn't be hard to implement. Must not forget the RAISE callis in the builtin library, though. Do we want the C source file / line number to be printed in those cases? Or perhaps the acton source location where the corresponding builtin operation is called?

Hmm, seems limke the latter would require some further mechanism that might be best to defer for now...

nordlander avatar Sep 17 '23 20:09 nordlander