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 ...
随机推荐
- 【Unity3D游戏开发】之游戏目录结构之最佳实践和优化 (十一)
游戏目录结构之最佳实践 前置条件 1.多人协作开发,git管理 2.游戏不大,所有Scene合并到一起Scene中,eg.RoleScene.MapScene.StoreScene 3.Master一 ...
- 【树莓派】树莓派网络配置:静态IP、无线网络、服务等
一.网络配置之静态IP: 树莓派的默认网络为: haochuang@raspberrypi:~ $ vi /etc/network/interfaces # interfaces() file use ...
- LTE Module User Documentation(翻译12)——X2切换(X2-based handover)
LTE用户文档 (如有不当的地方,欢迎指正!) 18 X2-based handover 正如 3GPP 定义的,切换是改变用户服务小区的连接方式的过程.这一过程中涉及的两个基站通常称为源基站和目 ...
- 描述Linux运行级别0-6的各自含义
中文: 0: 关机模式 1:单用户模式<==破解root密码 2:无网络支持的多用户模式 3:有网络支持的多用户模式(文本模式,工作中最长使用的模式) 4:保留,未使用 5:有网络支持又x-wi ...
- 使用新版Android Studio检测内存泄露和性能
内存泄露,是Android开发者最头疼的事.可能一处小小的内存泄露,都可能是毁于千里之堤的蚁穴. 怎么才能检测内存泄露呢?网上教程非常多,不过很多都是使用Eclipse检测的, 其实1.3版本以后的 ...
- Spring + Mybatis 使用 PageHelper 插件分页
原文:http://www.cnblogs.com/yucongblog/p/5330886.html 先增加maven依赖: <dependency> <groupId>co ...
- Oracle创建定时器
--创建日志信息表(测试表) create table T_LOG( id number, datetime DATE); --插入测试数据insert into t_log values (1,'2 ...
- Visual Studio 如何恢复默认设置
我们在使用 Visual Studio 的时候,常常有一些窗口不见了又弄不回来的情况,如何恢复默认设置呢? 1.点击“开始-->程序-->Microsoft Visual Studio20 ...
- 如何根据不同的浏览器写不同的css样式达到兼容
做前端静态页面的时候总是发现,ie的兼容性很差,总会出点问题.然后就去改代码 ,改完以后 又发现 火狐 谷歌又挂了,这可咋整. 后来发现做个判断吧 哪里有问题哪里就做个判断呗 ,咋判断呢,这么 ...
- Jquery easyui-combobox 的一个BUG
通过easyui-combobox的loadData方法,easyui-combobox可以实现一个带字符搜索的下拉框,如下图: 但是这个下拉框中的字符串如果包含一些特殊字符的时候,就会出现BUG,通 ...