spevnev / uprintf

Single-header library for printing anything in C (on Linux)
MIT License
31 stars 2 forks source link

Feature request: Support printing function signatures for external functions #1

Open theSprog opened 1 week ago

theSprog commented 1 week ago

Hi, First of all, thanks for this amazing library! I've been experimenting with it and found it incredibly useful.

I noticed that while uprintf can handle function pointers nicely, it currently cannot print the signatures of external functions like those from libc. For example:

uprintf("%S", printf);  // Only prints the function address

I believe this could be a valuable addition because:

  1. It would help with debugging by providing more context about function pointers, especially when working with callbacks or function tables
  2. The information should be available in the dynamic linker's symbol tables (dynsym/symtab sections)
  3. DWARF debug info for common libraries (like libc) is often available via debuginfo packages

Here's roughly what I imagine the output could look like:

uprintf("%S", printf); 
// Ideal output: int printf(const char *format, ...))
// Or else: 0x7f123456 (int (*)(const char *format, ...))
// Or at minimum: 0x7f123456 (printf)

Let me know your thoughts on this!

spevnev commented 1 week ago

Hi, this sure seems like a great feature to add. Thanks for suggestion. It seems easy for functions that are defined in the same compilation unit or passed by name, however I'm not yet sure about the ones from libraries. I'm busy right now, so I'll look into it on the next week.

theSprog commented 1 week ago

Hi, this sure seems like a great feature to add. Thanks for suggestion.你好,这看起来确实是一个很棒的功能。感谢您的建议。 It seems easy for functions that are defined in the same compilation unit or passed by name, however I'm not yet sure about the ones from libraries.对于在同一编译单元中定义或按名称传递的函数来说,这似乎很容易,但是我还不确定库中的函数。 I'm busy right now, so I'll look into it on the next week.我现在很忙,所以下周再研究一下。

cool😎

spevnev commented 6 days ago

Okay, so:

  1. I'm definitely going to implement it for functions from the same executable.
  2. For functions from shared libraries, it is a bit more complicated, but we can map pointer/address to its name:
    1. GCC (always?) generates entries for "external" functions, which makes it easy to get the type.
    2. Clang, however, does not always do it (I haven't yet figured out what is the requirement for it to produce such an entry). This only leaves the option of using debuginfo: I assume you mean debuginfod. In case the info is not available locally, it might take relatively long to download it, making program "hang". In which case I would rather only have same-executable function name/type resolution for clang.
spevnev commented 3 days ago

Implemented for locally defined functions. You can see formatting in function.c test. It doesn't work for functions that get inlined (yet).

theSprog commented 2 days ago

Ok, I've seen it. I've tested it and found it works well for my debugging needs.

And I tried other functions, it seems don't work for function in lib, such as printf. But now it's enough good for me to debug

I believe this feature is now complete enough for its intended purpose. feel free to close this issue, or keep it open if you plan to enhance it further

Thank you for your time ! 😃

spevnev commented 1 day ago

Although it's still rough around the edges, it should now work for all functions, i.e. printf (in GCC, only local in clang). Could you test it?