Debugging Process Startup
Debugging Process Startup
Q: How do I debug a process's startup code?
A: This depends on how the process is launched. For a typical application-level process, you can debug the startup code by launching it from within the debugger in Xcode. However, there may be circumstances where that's not an option. For example:
if the problem is masked by starting the application in the debugger
if the program isn't a typical application (for example it might be a CUPS filter)
The following sections describe various ways to debug a process's startup code.
Write Code To Stop
If you can build the program from source, it's trivial to add code to stop the program at the first line of main. Listing 1 shows an example of this.
Listing 1 Code to stop at startup
#include <signal.h> #include <unistd.h> int main(int argc, char **argv) { (void) raise(SIGSTOP); /// rest of your code here } |
This sends a SIGSTOP
to the process, which stops its execution so that you can attach using GDB. Alternatively, you can resume execution by sending the process a SIGCONT
using kill.
launchd
If it's not convenient to build the program from source, you can use a variety of other techniques. If your program is managed by launchd
, you can add the WaitForDebugger
property to your property list file to have launchd
stop your program before it it executes a single instruction. See the man page for details.
Important: Support for this property was introduced in Mac OS X 10.5.
GDB
If your program is not managed by launchd
, you can use GDB's --waitfor
option. GDB will poll the process list waiting for a matching process to be launched. You can supply the option either on the command line or as an argument to GDB's attach
command.
The fact that GDB polls the process list has two drawbacks. Firstly, it consumes a lot of CPU while waiting for the process to be launched. Secondly, the program stops in an indeterminate state. If you're running on Mac OS X 10.5 or later, it's probably better to use DTrace instead.
Important: This option is supported by the GDB that's included in Xcode 2.5 and later.
DTrace
Listing 2 shows an example DTrace script that sets a probe on a commonly used system call (getpid
) and, when that probe is hit, stops the process and invokes GDB on it. Listing 3 shows an example of its use.
Listing 2 WaitAttach.d
#! /usr/sbin/dtrace -w -q -s syscall::getpid:entry |
Listing 3 Using WaitAttach.d
$ # Make the script executable |
You can modify this script to meet your particular needs. For example:
The script sets the probe on
getpid
, which is currently the first system call made by a process. You can change this to any other system call (by changing "getpid" to something else), or to match all system calls (by deleting "getpid" entirely).The script currently matches the process by its executable name. This is an exact string match. You can use a fuzzy match by invoking DTrace functions like
strstr
.You can also extend the match to look for other criteria. For example, you can use the DTrace built in variable
ppid
to filter on the parent process ID.As it stands the script runs GDB as root. If that's a problem, you can change the script to invoke the chroot command to set the user and group ID of GDB to whatever you desire.
Important: DTrace was introduced in Mac OS X 10.5.
Debugging Process Startup的更多相关文章
- Debugging Chromium on Windows
转自:https://www.chromium.org/developers/how-tos/debugging-on-windows For Developers > How-Tos & ...
- 转:Remote debugging with Visual Studio 2010
Original URL http://www.codeproject.com/Articles/146838/Remote-debugging-with-Visual-Studio-2010 you ...
- error——Fusion log——Debugging Assembly Loading Failures
原文 So...you're seeing a FileNotFoundException, FileLoadException, BadImageFormatException or you sus ...
- [中英对照]Booting Process in Linux RHEL 7 | Linux RHEL 7启动过程
Booting Process in Linux RHEL 7 | Linux RHEL 7启动过程 In this post, I will guide you booting process in ...
- Process Monitor
https://en.wikipedia.org/wiki/Process_Monitor Process Monitor is a free tool from Windows Sysinterna ...
- SLES 12: Database Startup Error with ORA-27300 ORA-27301 ORA-27303 While Starting using Srvctl (Doc ID 2340986.1)
SLES 12: Database Startup Error with ORA-27300 ORA-27301 ORA-27303 While Starting using Srvctl (Doc ...
- oracle_hc.sql
select event,count(1) from gv$session group by event order by 2;exec dbms_workload_repository.create ...
- What are some good books/papers for learning deep learning?
What's the most effective way to get started with deep learning? 29 Answers Yoshua Bengio, ...
- Oracle12c版本中未归档隐藏参数
In this post, I will give a list of all undocumented parameters in Oracle 12.1.0.1c. Here is a query ...
随机推荐
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- hdu 4283 You Are the One 区间dp
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 使用nodewebx进行前后端开发环境分离
下载nodewebx(windows环境) npm install nodewebx npm install inherits 为什么要下载inherits,因为nodewebx依赖它... 构建目录 ...
- Java精确计算小数
Java在计算浮点数的时候,由于二进制无法精确表示0.1的值(就好比十进制无法精确表示1/3一样),所以一般会对小数格式化处理. 但是如果涉及到金钱的项目,一点点误差都不能有,必须使用精确运算的时候, ...
- CentOS6.5 安装 tomcat
1.首先下载tomcat.tar.gz包 http://tomcat.apache.org/download-70.cgi 2.移动到目标文件夹 mv tomcat.tar.gz /usr/tomca ...
- 深入浅出设计模式——命令模式(Command Pattern)
模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使得请 ...
- CSS分页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Makefile-入门与进阶【转】
from:here 一.入门 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的 ...
- Java防盗链机制
对于防盗链技术,网上提供了很多很多的相关技术,但是不是特别复杂就是效果不好. 这里在网上找到一种思路,就是关于HTTP协议响应头中包含的Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可 ...
- t检验
例子:以往通过大规模调查已知某地新生儿出生体重为3.30kg.从该地难产儿中随机抽取35名新生儿作为研究样本,平均出生体重为3.42kg,标准差为0.40kg. 问该地难产儿出生体重是否与一般新生儿体 ...