Skip to content

Instantly share code, notes, and snippets.

@sogaiu
Last active March 6, 2025 12:11
Show Gist options
  • Save sogaiu/b42cd233e397f65f2750ad1137f16ac4 to your computer and use it in GitHub Desktop.
Save sogaiu/b42cd233e397f65f2750ad1137f16ac4 to your computer and use it in GitHub Desktop.
using gdb to observe some values in janet

Using gdb to Observe Some Values in janet

Sometimes it's helpful to be able to look at what's going on in janet at the C level. The following demonstrates one way in the context of a recently fixed issue.

Flow Summary

  • Compile appropriate commit of janet with debugging info
  • Create sample janet source file
  • Run janet with sample file under gdb
  • Examine values via gdb

Detailed Steps

  • Get a relevant commit of janet ready

    $ git clone https://github.com/janet-lang/janet
    $ cd janet
    $ git checkout 61769c8f165762b85454757b2ce5a5ffb555c21c
    
  • Compile janet with debugging info -- but first tweak the Makefile

    $ sed -i "s/^CFLAGS?=-O2/CFLAGS?=-O0 -g3/" Makefile
    $ make
    
  • Verify that gensym is acting oddly -- note the return value of (gensym)

    $ ./build/janet
    Janet 1.17.0-dev-61769c8f linux/x64 - '(doc)' for help
    repl:1:> (gensym)
    
    repl:2:> 
    
  • Create a test janet file that calls gensym

    $ echo "(gensym)" > test.janet
    
  • Start gdb, telling it about the janet debug build and the test file

    $ gdb -quiet -cd . --args build/janet test.janet
    Reading symbols from build/janet...
    
  • Set a breakpoint at an appropriate part of janet_symbol_gen -- this is after a symbol name has been created

    (gdb) b src/core/symcache.c:235
    Breakpoint 1 at 0x52f65: file src/core/symcache.c, line 235.
    
  • Start build/janet test.janet

    (gdb) r
    Starting program: /home/user/src/janet/build/janet test.janet
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/usr/lib/libthread_db.so.1".
    
    Breakpoint 1, janet_symbol_gen () at src/core/symcache.c:235
    235	    janet_symcache_put((const uint8_t *)sym, bucket);
    
  • Print out what sym points at and verify it's not an underscore (_):

    (gdb) p *sym
    $1 = 0 '\000'
    
  • If a similar series of steps (though using line 236 instead of line 235 -- also make clean might help) is performed using commit a78cbd9, should see an underscore _ instead...

    (gdb) p *sym
    $1 = 95 '_'
    

Tips

  • Quit gdb

    (gdb) q
    A debugging session is active.
    
    Inferior 1 [process 192514] will be killed.
    
    Quit anyway? (y or n) y
    
  • Start getting help with gdb

    (gdb) h
    List of classes of commands:
    
    aliases -- User-defined aliases of other commands.
    breakpoints -- Making program stop at certain points.
    data -- Examining data.
    files -- Specifying and examining files.
    internals -- Maintenance commands.
    obscure -- Obscure features.
    running -- Running the program.
    stack -- Examining the stack.
    status -- Status inquiries.
    support -- Support facilities.
    text-user-interface -- TUI is the GDB text based interface.
    tracepoints -- Tracing of program execution without stopping the program.
    user-defined -- User-defined commands.
    
    Type "help" followed by a class name for a list of commands in that class.
    Type "help all" for the list of all commands.
    Type "help" followed by command name for full documentation.
    Type "apropos word" to search for commands related to "word".
    Type "apropos -v word" for full documentation of commands related to "word".
    Command name abbreviations are allowed if unambiguous.
    

References

Generic

Janet-Specific

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment