Skip to content

Backtrace screen (improvements over #1270)#1821

Merged
BenBE merged 9 commits intohtop-dev:mainfrom
Explorer09:configure-enable-backtrace
Mar 25, 2026
Merged

Backtrace screen (improvements over #1270)#1821
BenBE merged 9 commits intohtop-dev:mainfrom
Explorer09:configure-enable-backtrace

Conversation

@Explorer09
Copy link
Copy Markdown
Contributor

@Explorer09 Explorer09 commented Nov 15, 2025

htop backtrace screenshot

This branch is experimental and related to #1270 (the backtrace screen feature).

I've written and improved the configure script so that it can check libunwind-ptrace and libiberty for the backtrace feature proposed in #1270.

The changes come with two parts:

The first part can be reviewed and cherry-picked into main early. It renames the configure option --enable-unwind to --with-libunwind to better reflect it's purpose. The old option name is preserved for compatibility but will trigger warnings when used.

The second part consists of several commits that add the new configure options --enable-backtrace and --enable-demangling and the checking code of libunwind-ptrace and libiberty. The code might look large and complicated, but I've tried a lot on the usability and correctness of the check. For example this libunwind-ptrace check can work with and without pkg-config, and I tried to ensure the cplus_demangle() function has a correct prototype.

The libunwind-ptrace and libiberty libraries can be detected both in Linux and in FreeBSD. However, I haven't ported the backtrace screen code from @MrCirdo to ensure they really build, especially in FreeBSD.

@BenBE BenBE added enhancement Extension or improvement to existing feature code quality ♻️ Code quality enhancement build system 🔧 Affects the build system rather then the user experience labels Nov 15, 2025
@BenBE BenBE added this to the 3.5.0 milestone Nov 15, 2025
@BenBE
Copy link
Copy Markdown
Member

BenBE commented Nov 16, 2025

Can you note, which commits for the first vs. second part? I assume the first commit ("Rename *" for the first and the remaining 4 later?

I think we can also do this all at once. But I'm ope on that front.

@Explorer09
Copy link
Copy Markdown
Contributor Author

Explorer09 commented Nov 16, 2025

Can you note, which commits for the first vs. second part? I assume the first commit ("Rename *" for the first and the remaining 4 later?

Yes, the first commit is the option rename, and the remaining 4 commits are the new feature / configure options.

The reason I suggest the rename is that --enable-backtrace=unwind-ptrace will depend on the --with-libunwind option and it's hard to split the two apart. That means the --enable-backtrace=unwind-ptrace --without-libunwind combination will being you an error.

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Nov 16, 2025

Do we have alternative backends for backtracing/demangling besides unwind-ptrace and iberty? Given there's both an unwind implementation in LLVM and GCC, what else are sensible alternatives that warrent implementation of the --enable-backtrace switch as --enable-backtrace=unwind (if I saw that correctly in the source)?

@Explorer09
Copy link
Copy Markdown
Contributor Author

Do we have alternative backends for backtracing/demangling besides unwind-ptrace and iberty? Given there's both an unwind implementation in LLVM and GCC, what else are sensible alternatives that warrent implementation of the --enable-backtrace switch as --enable-backtrace=unwind (if I saw that correctly in the source)?

Well, I don't know what other libraries are possible for the backtrace feature, but I leave room for those new backends.

For the demangling function, I can see libdemangle from Solaris / Sun Studio as a possible alternative, but I didn't check that yet. It's better for @MrCirdo 's code (#1270) get merged first before considering alternatives.

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Nov 16, 2025

NP. No need to decide on all the possible backends and variants now. That's something we can leave open for the first implementation. Priority is getting things work; make them flexible and fast is the next step. No need to over-engineer things from the get-go. I just wanted to evaluate if there's a reasonable need for allowing the backends for backtraces and demangling to be replaced.

@MrCirdo MrCirdo mentioned this pull request Nov 16, 2025
@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch 9 times, most recently from a964363 to 798b603 Compare November 23, 2025 01:38
@Explorer09 Explorer09 marked this pull request as ready for review November 23, 2025 01:44
@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch from 798b603 to 9ced4f4 Compare November 23, 2025 03:27
@Explorer09
Copy link
Copy Markdown
Contributor Author

I would like to hand this branch over to @MrCirdo. I think I have done with this. After this is handed over, I have some comments on @MrCirdo 's code, things I suggest that can be written better.

@Explorer09 Explorer09 changed the title Configure script changes for backtrace feature Backtrace screen (improvements over #1270) Nov 23, 2025
@MrCirdo
Copy link
Copy Markdown
Contributor

MrCirdo commented Nov 23, 2025

I think I have done with this.

Great! Perfect! Thank you very much!

After this is handed over, I have some comments on @MrCirdo 's code, things I suggest that can be written better.

With pleasure!

Wait, let’s get on the same page @Explorer09.
My idea was to split my PR into parts. The first part, which is presented here, involves adding a better configuration to my original PR. The second part is the behavior of the backtrace screen. And here you split both (Deeply apologize if I explain my idea very badly). If I understand well what @BenBE said, this PR would serve as a good base for #1270. So my question is : Do I have to close #1270 and continue my work on this one? I'm confused 😄

My primary concern about supporting the BSD platform (FreeBSD, NetBSD, macOS, etc) is that I don't know if ptrace(2) has the same behavior everywhere. (I suppose yes, but I've never used a BSD platform). So, reassure me, @Explorer09, that's the case?

NB : Sorry if I take time to answer because I need to do some preparation for a coding interview.

@Explorer09
Copy link
Copy Markdown
Contributor Author

Explorer09 commented Nov 23, 2025

My primary concern about supporting the BSD platform (FreeBSD, NetBSD, macOS, etc) is that I don't know if ptrace(2) has the same behavior everywhere. (I suppose yes, but I've never used a BSD platform). So, reassure me, @Explorer09, that's the case?

I didn't test running the code on BSD yet. The only thing I've tested is building htop with libunwind-ptrace and libiberty on FreeBSD on a CI server. I've read the man page of ptrace(2) for FreeBSD and got the equivalent keywords (PT_ATTACH and PT_DETACH), and those are what I've done.

My aim is to make it buildable in Linux and FreeBSD; other OSes are too difficult and I would expect libunwind port to those platforms first before we try making our backtrace feature work there.

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Nov 24, 2025

I think I have done with this.

Great! Perfect! Thank you very much!

I.e. no further changes from @Explorer09 unless needed/requested. And possibly rebases onto main maybe.

After this is handed over, I have some comments on @MrCirdo 's code, things I suggest that can be written better.

With pleasure!

Wait, let’s get on the same page @Explorer09.
My idea was to split my PR into parts. The first part, which is presented here, involves adding a better configuration to my original PR. The second part is the behavior of the backtrace screen. And here you split both (Deeply apologize if I explain my idea very badly). If I understand well what @BenBE said, this PR would serve as a good base for #1270. So my question is : Do I have to close #1270 and continue my work on this one? I'm confused 😄

I think this PR will likely stay as is; i.e. no major changes.

My primary concern about supporting the BSD platform (FreeBSD, NetBSD, macOS, etc) is that I don't know if ptrace(2) has the same behavior everywhere. (I suppose yes, but I've never used a BSD platform). So, reassure me, @Explorer09, that's the case?

You can ask @fraggerfox regarding NetBSD details if necessary.

NB : Sorry if I take time to answer because I need to do some preparation for a coding interview.

NP.

@Explorer09
Copy link
Copy Markdown
Contributor Author

Explorer09 commented Nov 24, 2025

My primary concern about supporting the BSD platform (FreeBSD, NetBSD, macOS, etc) is that I don't know if ptrace(2) has the same behavior everywhere. (I suppose yes, but I've never used a BSD platform). So, reassure me, @Explorer09, that's the case?

You can ask @fraggerfox regarding NetBSD details if necessary.

The libunwind README says its supported operating systems are Linux, FreeBSD, Solaris, QNX and HP-UX (partially). NetBSD is not included.

For Solaris, there's no pre-built binary package for libunwind so testing on it would be difficult. QNX and HP-UX are out of htop's scope. So there remains Linux and FreeBSD, which are what I tried to make htop's backtrace work for.

@Explorer09
Copy link
Copy Markdown
Contributor Author

I prefer FILE to MODULE :) Let's go for FILE.

File is ambiguous when you have both a module name AND source symbols, where file usually refers to the source code file, not the compiled module.

This question depends on the backend. There could a hypothetical backend that can trace the debug file (DWARF format?) of the program and print the source file names. For the purpose of the UI, I don't think "FILE" poses ambiguity.

@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch 4 times, most recently from 3496ddf to ef82218 Compare February 23, 2026 22:02
@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch from ef82218 to 3ffafb7 Compare February 28, 2026 18:45
@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch from 3ffafb7 to f648dcc Compare March 9, 2026 17:33
@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch from f648dcc to a0d1cc4 Compare March 18, 2026 11:13
@BenBE
Copy link
Copy Markdown
Member

BenBE commented Mar 18, 2026

Apart from the "FILE"->"MODULE" column rename any other open issues for this PR?

@MrCirdo MrCirdo force-pushed the configure-enable-backtrace branch from a0d1cc4 to 0529f5b Compare March 24, 2026 20:52
@MrCirdo
Copy link
Copy Markdown
Contributor

MrCirdo commented Mar 24, 2026

Apart from the "FILE"->"MODULE" column rename any other open issues for this PR?

I just pushed the modification to the parameters of the cplus_demangle function in the iberty library. I included it directly in the commit.

Apart from that point you mentioned, I don't have any issues with this PR.

@Explorer09 Explorer09 force-pushed the configure-enable-backtrace branch from 0529f5b to 1751ac0 Compare March 25, 2026 04:18
@Explorer09
Copy link
Copy Markdown
Contributor Author

I would say this branch is all ready to be merged.

Apart from the "FILE"->"MODULE" column rename any other open issues for this PR?

The consensus from the previous discussions is to use "FILE" for the column, not "MODULE".

Copy link
Copy Markdown
Member

@fasterit fasterit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well, RTM

Explorer09 and others added 3 commits March 25, 2026 16:47
* Explain that "/usr/include/libunwind/libunwind.h" is a Debian package
  thing. (LLVM libunwind upstream doesn't install the headers in that
  strange location.)
  (See also 6388033 and issue htop-dev#894)
* Clarifications on why we define UNW_LOCAL_ONLY during redection of
  libunwind functions.

No change in configure script behavior.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
The configure option '--enable-unwind' doesn't actually enable a
feature in htop but selects a library dependency for a feature
(printing local backtrace for this case). Use a '--with-' naming
to reflect the option's purpose better.

For backward compatibility (with automated build scripts), using
'--enable-unwind' will still work but it will print a warning.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
For the remote backtrace feature that would be introduced in the next
commit.

For now the feature supports only one backend (libunwind-ptrace). This
commit adds checks to libunwind-ptrace.

Co-authored-by: Odric Roux-Paris <odric@roux-paris.fr>
Co-authored-by: Benny Baumann <BenBE@geshi.org>
Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
@BenBE BenBE force-pushed the configure-enable-backtrace branch from 1751ac0 to 2c893ed Compare March 25, 2026 15:53
MrCirdo and others added 6 commits March 25, 2026 17:10
Add a new feature to display backtrace of any process.
The new backtrace screen can be started by pressing the key 'b'
when a process is selected.

This commit contains only the very basic information of the backtrace
screen (frame indices, addresses and symbol names). Additional
information like library names and demangling support will be added in
subsequent commits.

The code in this commit was primarily written by Odric.
Kang-Che and Benny provided code improvements and bug fixes that were
incorporated into this main commit.

Co-authored-by: Odric Roux-Paris <odric@roux-paris.fr>
Co-authored-by: Kang-Che Sung <explorer09@gmail.com>
Co-authored-by: Benny Baumann <BenBE@geshi.org>
This covers Linux and FreeBSD build jobs

Co-authored-by: Odric Roux-Paris <odric@roux-paris.fr>
Co-authored-by: Kang-Che Sung <explorer09@gmail.com>
Extend the backtrace screen feature to show library filenames as a new
column in the backtrace screen.

This feature depend on libunwind 1.8 or later (unw_get_elf_filename())

Co-authored-by: Benny Baumann <BenBE@geshi.org>
Co-authored-by: Kang-Che Sung <explorer09@gmail.com>
For demangling support in the backtrace screen, which will be added in
the next commit.

Two backends will be supported: libiberty (GNU) and libdemangle
(Solaris). This commit adds checks for both of them.

Co-authored-by: Odric Roux-Paris <odric@roux-paris.fr>
Co-authored-by: Benny Baumann <BenBE@geshi.org>
Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Add support of demangling symbol names in the backtrace screen.

Two backends are supported: libiberty (GNU) and libdemangle (Solaris).

[The initial code for demangling support with libiberty was written by
Odric. Kang-Che expanded the code to support libdemangle. Benny provided
code reviewes and minor fixes.]

Co-authored-by: Kang-Che Sung <explorer09@gmail.com>
Co-authored-by: Benny Baumann <BenBE@geshi.org>
@BenBE BenBE force-pushed the configure-enable-backtrace branch from 2c893ed to 349519f Compare March 25, 2026 16:10
@BenBE
Copy link
Copy Markdown
Member

BenBE commented Mar 25, 2026

Did a few minor adjustments to fix minor issues and to streamline the commit messages somewhat.

@BenBE BenBE merged commit da76ae0 into htop-dev:main Mar 25, 2026
20 checks passed
@Explorer09 Explorer09 deleted the configure-enable-backtrace branch March 25, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build system 🔧 Affects the build system rather then the user experience code quality ♻️ Code quality enhancement enhancement Extension or improvement to existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants