7 ms·
Debugging C with Cosmopolitan Libc
- saagarjha 4y agoPerhaps I am missing something but what does this have to with Cosmopolitan?
- detaro 4y agodoesn't the first paragraph clearly establish that?
- saagarjha 4y agoI mean it says these are part of Cosmopolitan but these just seem like standard tools that I don't need to use Cosmopolitan for…
- ddulaney 4y agoThere are some things from Cosmopolitan that demonstrate its integration with GDB and other standard tools. For example, the article demos ShowCrashReports() which is nicely integrated with GDB. Also --ftrace, not to be confused with the kernel feature of the same name.
- saagarjha 4y agoKind of have the same question as this commenter: https://news.ycombinator.com/item?id=33312835 https://news.ycombinator.com/item?id=33312835
- detaro 4y agoIt explains how to use them in the way cosmopolitan provides them, which is clearly different from how other environments do it, even though they of course have ways of getting the same kind of information.
- ahgamut 4y agoThere are many wonderful tools that help understand what our code is doing (compiler explorer, gdb, valgrind, ftrace, perf record, strace, ASAN/UBSAN). I wrote this blog post because using the debugging tools with Cosmopolitan Libc helped me learn and improve at programming. If you have other resources that can help me understand more about how my code works, I am happy to learn.
- taspeotis 4y ago
- b0rsuk 4y ago
- bscphil 4y ago> With Cosmopolitan Libc, you can have gdb integration and detailed backtraces for your C program in just one line: add ShowCrashReports(); at the start of the main function > Now if you have gdb available on $PATH, you would get a TUI (terminal user interace) showing the register contents and the backtrace of the crash
- mgaunard 4y agoWhat's the advantage over calling gdb yourself?
- ahgamut 4y agoYou run the executable as usual, and if it crashes, Cosmopolitan Libc's crash handler starts gdb for you with a nice TUI and all the information you need to work with. Probably you could set up a bunch of macros in your editor that do the same thing (and I do have something like that for godbolt compiler explorer), but for gdb at least it's pretty convenient to have it performed by the executable when running it across systems. You can start gdb by yourself too if you have a workflow setup like that :)
- mgaunard 4y agoI can launch gdb and attach it to the current process in 5 lines, hardly worth pulling a library dependency for.
- anewhnaccount2 4y agoCosmopolitan is a replacement standard library. It's not so much "pulling-in" a library as replacing e.g. glibc. The point of the blog post is that it's a libc but also a batteries included C development environment. On the one hand, it's nothing magic, on the other hand, it does have some nice concordances which are worth showing off.
- tom_ 4y agoThis is exactly the sort of drudgery from which computers were supposed to liberate us.
- mgaunard 4y agoDepending on any third-party software is a huge liability for any professional software development. Dependencies must be chosen and managed carefully. I'm not going to depend on something for something that's trivial; the time it takes for me to write it is less than it takes to do a license review and integrate it into my build system.
- a-dub 4y agoso where does the machine code for the TUI live? does it get compiled into your binary? how big is it?
- TheYumasi 4y agoThe TUI is from gdb.[1] Nothing from GDB is compiled in your binary. My guess (might be wrong!) is that the ShowCrashReports() call added in your program's main function sets up some signal handlers, so that in case of segfaults it starts GDB in a separate process and attaches it to your crashing process. [1]: https://sourceware.org/gdb/onlinedocs/gdb/TUI.html https://sourceware.org/gdb/onlinedocs/gdb/TUI.html
- surprisegock 4y ago
- jstanley 4y agoCan we just enjoy the code and not bring politics into it? > Cosmopolitan Libc makes C a build-once run-anywhere language, like Java, except it doesn't need an interpreter or virtual machine. Instead, it reconfigures stock GCC and Clang to output a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS with the best possible performance and the tiniest footprint imaginable. https://justine.lol/cosmopolitan/ https://justine.lol/cosmopolitan/
- abainbridge 4y ago> 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/ https://justine.lol/ftrace/.
- actionfromafar 4y agoSo much innovation in a tiny libc.
- tines 4y agoYou don't in principle have to put any nop's at the start of each function for this kind of function "hooking" to work. The Microsoft Detours library does the same thing with no special compilation needed.
- abainbridge 4y agoFrom https://www.microsoft.com/en-us/research/project/detours/ https://www.microsoft.com/en-us/research/project/detours/ "Detours is a library for instrumenting arbitrary Win32 functions Windows-compatible processors. Detours intercepts Win32 functions by re-writing the in-memory code for target functions." Doesn't that just re-write the DLL import table? ie it only allows interception of a Win32 API calls, not interception of my function A calling my function B?
- wheybags 4y agoNo, it inserts a jump instruction at the start of the function, and just overrides whatever is there already. To allow you to call the "original" function, it does some analysis to figure out what it's stomping on, and creates another little bootstrap function that executes that one destroyed instruction, then jumps to the second instruction of the original function. Basically: black magic
- andrewmcwatters 4y agoI'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.
- jart 4y agoJustine here. I worked on TensorFlow before working on this.
- ynx 4y agoMy true introduction to low level system internals was heavily influenced by Saleem (compnerd), who last time I caught up with them, was on Tensorflow at Google. He's a force of nature, but god damn if I didn't learn a lot from him and his peers. Cheers to you and to him!
- asveikau 4y agoI'm not Justine, but I have done some things involving object and program file formats. Before I was a professional dev, in teenage years, I was interested in OS internals and was hacking on a small kernel for fun and learning. Some of my early professional tasks involved kernel mode. I think some people just get bit with a low level bug.
- gavinray 4y agoI've read that you can use Cosmopolitan to write C++ as well. Can you use similar features with C++ development?
- renox 4y agoI 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.
- myuzio 4y agoIf anyone's interested in how this actually works, take a look at this (originally linked) article: https://justine.lol/ape.html 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.