简体   繁体   English

如何知道正在运行的进程正在执行哪个语句

[英]how to know which statement the running process is executing

I have a process which suddenly hanged and is not giving any core dump and is also not killed.i can see it still running using the ps command. 我有一个突然挂起的进程,并没有给出任何核心转储,也没有被杀死。我可以看到它仍然使用ps命令运行。

how can i know which statement it is currently executing inside the code. 我怎么知道它当前在代码中执行的语句。

basically i want to know where exactly it got hanged. 基本上我想知道它被绞死的地方。

language is c++ and platform is solaris unix. 语言是c ++,平台是solaris unix。

demos.283> cat test3.cc
#include<stdio.h>
#include<unistd.h>

int main()
{

sleep(100);
return 0;

}
demos.284> CC test3.cc 
demos.285> ./a.out &
[1] 2231
demos.286> ps -o "pid,wchan,comm"
  PID            WCHAN COMMAND
23420 fffffe86e9a5aff6 -tcsh
 2345                - ps
 2231 ffffffffb8ca3376 ./a.out
demos.290> ps
   PID TTY         TIME CMD
  3823 pts/36      0:00 ps
 23420 pts/36      0:00 tcsh
  3822 pts/36      0:00 a.out
demos.291> pstack 3822
3822:   ./a.out
 fed1a215 nanosleep (80478c0, 80478c8)
 080508ff main     (1, 8047920, 8047928, fed93ec0) + f
 0805085d _start   (1, 8047a4c, 0, 8047a54, 8047a67, 8047c05) + 7d
demos.292> 

You have several options: the easiest is to check the WCHAN wait channel that the process is sleeping on: 您有几种选择:最简单的方法是检查进程正在睡眠的WCHAN等待通道:

$ ps -o "pid,wchan,comm"
  PID WCHAN  COMMAND
 2350 wait   bash
20639 hrtime i3status
20640 poll_s dzen2
28821 -      ps

This can give you a good indication of what the process is doing and is very easy to get. 这可以很好地指示流程正在做什么,并且很容易获得。

You can use ktruss and ktrace or DTrace to trace your process. 您可以使用ktrussktraceDTrace来跟踪您的流程。 (Sorry, no Solaris here, so no examples.) (对不起,这里没有Solaris,所以没有例子。)

You can also attach gdb(1) to your process: 您还可以将gdb(1)附加到您的流程:

# gdb -p 20640
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
...
(gdb) bt
#0  0x00007fd1a99fd123 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:82
#1  0x0000000000405533 in ?? ()
#2  0x00007fd1a993deff in __libc_start_main (main=0x4043e3, argc=13, ubp_av=0x7fff25e7b478, 
...

The backtrace is often the single most useful error report you can get from a process, so it is worth installing gdb(1) if it isn't already installed. 回溯通常是您可以从进程获得的最有用的错误报告,因此如果尚未安装gdb(1) ,则值得安装。 gdb(1) can do a lot more than just show you backtraces, but a full tutorial is well outside the scope of Stack Overflow. gdb(1)可以做很多事情不仅仅是告诉你回溯,而是一个完整的教程以及堆栈溢出的范围之内。

you can try with pstack passing pid as parameter. 你可以尝试使用pstack传递pid作为参数。 You can use ps to get the process id (pid) 您可以使用ps来获取进程ID(pid)

For example: pstack 1267 例如: pstack 1267

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM