
Using /proc to get a process' current stack trace - cirowrc
https://ops.tips/blog/using-procfs-to-get-process-stack-trace/
======
drewg123
FWIW, the equivalent in FreeBSD is 'procstat -kk $PID' Eg:

    
    
      % procstat -kk 5592
    
      PID    TID COMM                TDNAME              KSTACK                   
     5592 103222 less                -                   

mi_switch+0xe1 sleepq_catch_signals+0x405 sleepq_wait_sig+0xf
_cv_wait_sig+0x154 tty_wait+0x1c ttydisc_read+0x1f2 ttydev_read+0x64
devfs_read_f+0xdc dofileread+0x95 sys_read+0xc3 amd64_syscall+0x369
fast_syscall_common+0x101

procstat can also do interesting things, like show current rusage state:

    
    
      % procstat -r 5592
      PID COMM             RESOURCE                          VALUE
     5592 less             user time                    00:00:00.010805
     5592 less             system time                  00:00:00.002444
     5592 less             maximum RSS                             3172 KB
     5592 less             integral shared memory                   192 KB
     5592 less             integral unshared data                    80 KB
     5592 less             integral unshared stack                  256 KB
     5592 less             page reclaims                            199
     5592 less             page faults                                0
     5592 less             swaps                                      0
     5592 less             block reads                                4
     5592 less             block writes                               0
     5592 less             messages sent                              0
     5592 less             messages received                          0
     5592 less             signals received                           0
     5592 less             voluntary context switches                59
     5592 less             involuntary context switches               0

~~~
drewg123
BTW, I totally suck at formatting stuff in various forums. Is there a way to
post pre-formatted text here? Eg, like triple back-ticks will do in slack? ```
stuff... ```

~~~
colanderman
Prepend with four(?) spaces.

~~~
jwilk
Two spaces is enough:

[https://news.ycombinator.com/formatdoc](https://news.ycombinator.com/formatdoc)

~~~
drewg123
Thanks! I cleaned up the above based on this..

------
lixtra
If you are running java instead of a c program the proc stacktrace shows you
just the virtual machine state. You can still get a stacktrace of your java
threads[1].

How about other languages? Python, ruby?

[1] [https://stackoverflow.com/questions/4876274/kill-3-to-get-
ja...](https://stackoverflow.com/questions/4876274/kill-3-to-get-java-thread-
dump)

~~~
monocasa
This is about the kernel's stack trace during a system call, not the user
space stack trace.

~~~
scott_s
Yes, and if you want to know the user space stack, you may be able to attach
gdb to the process: [https://stackoverflow.com/questions/2308653/can-i-use-
gdb-to...](https://stackoverflow.com/questions/2308653/can-i-use-gdb-to-debug-
a-running-process)

------
indigodaddy
Nicely formatted site. Anyone know if/what static site generator/theme is
being used? Couldn't find anything on the footer, site tags, or his GitHub
that would reveal that...

~~~
curtis86
Agreed! It looks like Hugo and possibly this theme:
[https://themes.gohugo.io//theme/cocoa-eh-hugo-
theme/blog/exa...](https://themes.gohugo.io//theme/cocoa-eh-hugo-
theme/blog/example-article/)

------
ktpsns
Note, the GNU debugger (gdb) can attach to running processes. This should give
you a stack trace with readable addresses, cf.
[https://stackoverflow.com/questions/2308653/can-i-use-gdb-
to...](https://stackoverflow.com/questions/2308653/can-i-use-gdb-to-debug-a-
running-process)

~~~
drewg123
The /proc interface tells you what the process is doing in the kernel (eg,
after a system call), which is orthogonal to getting a user-space stack trace.
Both are helpful. The userspace trace is arguably more helpful. The nice thing
about the kernel trace is that you're likely to have symbols (or be able to
download them); that is unlikely to be true for userspace if you're running a
commercial binary, for example.

------
_pmf_
My favorite proc hack:
[https://news.ycombinator.com/item?id=17061499](https://news.ycombinator.com/item?id=17061499)

~~~
chaosfox
check out progress:
[https://github.com/Xfennec/progress](https://github.com/Xfennec/progress)

~~~
lathiat
That's a cool tool.. I did that manually many times and always thought I
should write a tool. Glad to see it exists already :)

------
jhallenworld
I want this capability for embedded ARM systems. I should be able to call a
function to have the current stack trace printed:

[https://communities.mentor.com/thread/16468](https://communities.mentor.com/thread/16468)

~~~
stefan_
The most widely distributed embedded ARM system software in the world,
Android, offers this. They use mini debug info (normal debug info sections
compressed, IIRC) and then you can signal a daemon in the background to do a
stacktrace on your application.

Not on production images, though.

~~~
monocasa
> The most widely distributed embedded ARM system software in the world,
> Android

[citation needed]

Also, when talking about embedded systems, people aren't normally talking
about full Linux distros.

------
AlphaWeaver
This is a very well written and formatted article... I found it easy to read!

------
Annatar
Instead of teaching people how to do this portably across all UNIX-like
systems, by sending SIGABRT to the process, the article is steeping them in
GNU/Linux only way of doing things. This feels exactly like the '90's of the
past century, where a lot of people with computer-related careers had no idea
that there were other operating systems and other ways of doing things
(better): an intel-based PC tin bucket with Windows was the one and only truth
for them. Now it's exactly the same except Windows has been replaced with
GNU/Linux. 28 years later and the only advancement some people have made is
running the proverbial sed 's/Windows/Linux/g'.

~~~
someguydave
Umm, what is the alternative O/S that has complete hardware support?

~~~
dman
What is the non alternative O/S that has complete hardware support? fwiw
openbsd / freebsd have been painless to install on every machine I have tried
in the last 5 years.

~~~
justinsaccount
How's the support for bluetooth on openbsd these days?

~~~
someguydave
Also, how many of the aforementioned bsds use drivers that were ported from
the Linux kernel tree?

~~~
oneanddone
BSD user here. Of course there is porting occurring, but this is the nature of
open source, is it not? Oftentimes, the BSD devs will take whatever hardware
specs they can get and code something from that. OpenBSD frequently writes
drivers that are orders of magnitude smaller than comparable Linux drivers,
while getting the same functionality. NVIDIA drivers are but one example. Some
hardware vendors are generous with hardware specs for OSS, most are not.
OpenBSD, as an example, will not have any GPL'd code in the base OS, as it's
not truly free. Ports are another issue.

~~~
TheDong
> will not have any GPL'd code in the base OS, as it's not truly free

That's a disingenuous way to put it. The GPL is a free software license. It is
"free" as in libre by all common definitions of "free" in regards to software.

The GPL is not compatible with the BSD dev's preferred license, which is much
more likely the reason they avoid using such code.

By all conventional definitions, the BSD and GPL licenses are "truly free
[software licenses]."

You're welcome to argue that the BSD license is better (because, for example,
it lets sony create derivative playstation OSs without providing that source
code to their users, ensuring their users have less freedom than if it were
linux) or that the GPL is better (because it would prevent the previous), but
they're both free licenses.

