from Hacker News

Debugging C with Cosmopolitan Libc

by ahgamut on 10/24/22, 1:24 AM with 50 comments

  • by abainbridge on 10/24/22, 10:55 AM

    > Cosmopolitan Libc allows you to log every function call over the program’s execution – just pass --ftrace at the end of your program, like this:

    > ./hex16.com ./missing.txt --ftrace

    That seems like magic. It seems you have to build with a special flag that tells GCC to put a nop at the start of each function. Then, before main runs, Cosmopolitan sees the --ftrace on the command line, and modifies the code in memory to replace those nops with calls to something that does the logging. See https://justine.lol/ftrace/.

  • by andrewmcwatters on 10/24/22, 2:27 PM

    I'm fascinated specifically with how Justine must have collected the knowledge to write Cosmopolitan. I know some guys who play around with PEs and virtual memory manually, but I always wonder what sort of interests lead you to discovering this sort of thing.

    I suspect the combination of interests is a little out there. For instance, with game cheat developers, you tend to first at least have some interest in C++, then understanding memory scanning, then signature scanning, trampolining, and all of a sudden you have the skillset ingredients for authoring some rudimentary cheats. Advanced skills come with driver development, which you then pick up to figure out how to evade anti-cheat technology, etc.

    But very few developers I know say, oh yeah, I was just interested directly in this sort of thing from the get-go and decided to pick up all of the specific skills to go straight to cheat development.

    Usually, it's the guys who already have game development experience.

    What in the world did Justine see before Cosmopolitan? Maybe debugging tech? An interest in creating her own libc and understanding syscalls? Just fascinating.

  • by mgaunard on 10/24/22, 4:06 AM

    What's the advantage over calling gdb yourself?
  • by myuzio on 10/25/22, 8:14 PM

    If anyone's interested in how this actually works, take a look at this (originally linked) article: https://justine.lol/ape.html

    This reminds me a little bit about how "Wine" works, but because the support doesn't involve "everything an OS has to provide" the footprint is smaller.

  • by a-dub on 10/24/22, 5:26 AM

    so where does the machine code for the TUI live? does it get compiled into your binary? how big is it?
  • by gavinray on 10/24/22, 3:32 PM

    I've read that you can use Cosmopolitan to write C++ as well.

    Can you use similar features with C++ development?

  • by saagarjha on 10/24/22, 2:30 AM

    Perhaps I am missing something but what does this have to with Cosmopolitan?
  • by renox on 10/24/22, 7:11 PM

    I wonder if it would be possible to have a kind of logging function which would work similarly as the --ftrace? That would reduce significantly the no-trace cpu cost.