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 ...
随机推荐
- 调用discuz编辑器再也不是问题了
前面讲了如何开发一个discuz的特殊主题插件,详情可在此查看discuz特殊主题插件开发步骤和犯的愚蠢错误.上一篇文章讲解的是一些简单的开发步骤,不涉及到具体的编码.网页编辑器之类的都是系统默认带过 ...
- ucenter的单点登录
所谓单点登录,无非就是几个站点共用一个用户中心,实现同步登陆,同步退出. 服务器端:Loog SSO . 客服端: ucenter,说实话dz商业化确实让php发展了不少. ucenter 基本原理: ...
- 调度系统任务创建---Git部署项目(一)
有任务要上线时,需要将任务所在的项目代码从git上取出,在调度系统中编译打包,分发大任务执行服务器上. 具体的任务可以参考:http://192.168.53.100:8090/jenkins/vie ...
- linux详细redis安装和php中redis扩展
第一部分:安装redis 希望将redis安装到此目录 1 /usr/local/redis 希望将安装包下载到此目录 1 /usr/local/src 那么安装过程指令如下: 1 2 3 4 5 6 ...
- div 一段时间后自动隐藏
一.div弹出后自动消失 这里并没有删除 setTimeout(function(){$(".alert").hide();},2000); 直接在js文件中需要的地方添加执行这段 ...
- JSP 中EL表达式用法详解
EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以${ ...
- Skyfree的毕业论文 《系统封装与部署的深入研究》
Skyfree的毕业论文 <系统封装与部署的深入研究> https://www.itsk.com/thread-197-1-4.html Skyfree 发表于 2007-9-13 07: ...
- js高级程序设计(六)面向对象
ECMA-262 把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.”严格来讲,这就相当于说对象是一组没有特定顺序的值.对象的每个属性或方法都有一个名字,而每个名字都映射到一个值.正 ...
- WPF:换肤
看了一篇博客,觉得样式很好看,就自己动手做了一下,做个总结. 效果: 选择不同的图片背景就会改变: 直接上代码: 每个Theme对应一张图,除了图的名称不同之外,Theme?.xaml中的内容相 ...
- C#窗体 WinForm 对话框,流
一.对话框 ColorDialog:颜色选择控件 private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDia ...