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 attachcommand.
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
ppidto 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 ...
随机推荐
- nginx简单的rewrite配置
假设当前已存在location /test/,希望配置一个短连接/ts/与之相同,就需要用到rewrite 直接配置到server段 rewrite ^/ts/(.*) /test/$1; 或者放到l ...
- MariaDB链接超时优化
查看mysql server超时时间: MariaDB [(none)]> use xspeeder; MariaDB [xspeeder]> show global variables ...
- LuaStudio 9.27 去10分钟退出暗桩板
http://bbs.pediy.com/showthread.php?p=1428203#post1428203
- python文件和目录操作方法大全(含实例)
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目 ...
- PHP 可变长度参数列表
In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a ...
- SQL疑难杂症【5 】大量数据查询的时候要考虑结果为空的情况
最近几天怪事儿出奇的多,同一个工单.同一个产品,在A线可以正常生产,但是在H线死活都无法生产,系统直接提示TimeOut,监控发现有一条SQL语句执行缓慢,Copy出来仔细查看,很简单的一条语句,如下 ...
- gitlab open ssl
cd /home/git/gitlab/ sudo -u git -H vi config/gitlab.yml sudo -u git -H vi /home/git/gitlab-shell/co ...
- 使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包?
Sonatype Nexus简介 登录Nexus后,点击右侧的“Repositories”,显示当前Nexus所管理的Repository: 默认情况下Nexus为我们创建了以下主要的Reposito ...
- package
1.设计package原因 理解基目录的概念,思考jre加载class的顺序,如果没有package会怎么样?有了之后又是怎么样..? 主要:确保类名的唯一性. 次要:方便组织代码 2.怎样访问\导入 ...
- Android 控件知识点,
一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...