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
/ execname == $$1 /
{
stop();
system(
"echo attach %d > /tmp/WaitAttach.gdb ; gdb -x /tmp/WaitAttach.gdb",
pid
);
exit(0);
}

Listing 3  Using WaitAttach.d

$ # Make the script executable
$ chmod ugo+x WaitAttach.d
$ # Run it
$ sudo ./WaitAttach.d TextEdit
GNU gdb 6.3.50-20050815 [...]
[... now launch TextEdit ...]
Attaching to process 3723.
Reading symbols for shared libraries . done
0x8fe21a25 in __dyld_getpid ()
(gdb) bt
#0 0x8fe21a25 in __dyld_getpid ()
#1 0x8fe07139 in __dyld__ZN4dyld5_mainEPK11mach_headermiPPKcS5_S5_ ()
#2 0x8fe01872 in __dyld__ZN13dyldbootstrap5startEPK11mach_headeriPPKcl ()
#3 0x8fe01037 in __dyld__dyld_start ()

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的更多相关文章

  1. Debugging Chromium on Windows

    转自:https://www.chromium.org/developers/how-tos/debugging-on-windows For Developers‎ > ‎How-Tos‎ & ...

  2. 转:Remote debugging with Visual Studio 2010

    Original URL http://www.codeproject.com/Articles/146838/Remote-debugging-with-Visual-Studio-2010 you ...

  3. error——Fusion log——Debugging Assembly Loading Failures

    原文 So...you're seeing a FileNotFoundException, FileLoadException, BadImageFormatException or you sus ...

  4. [中英对照]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 ...

  5. Process Monitor

    https://en.wikipedia.org/wiki/Process_Monitor Process Monitor is a free tool from Windows Sysinterna ...

  6. 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 ...

  7. oracle_hc.sql

    select event,count(1) from gv$session group by event order by 2;exec dbms_workload_repository.create ...

  8. 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, ...

  9. Oracle12c版本中未归档隐藏参数

    In this post, I will give a list of all undocumented parameters in Oracle 12.1.0.1c. Here is a query ...

随机推荐

  1. [问题2014A02] 解答二(求和法+拆分法,由张诚纯同学提供)

    [问题2014A02] 解答二(求和法+拆分法,由张诚纯同学提供) 将行列式 \(|A|\) 的第二列,\(\cdots\),第 \(n\) 列全部加到第一列,可得 \[ |A|=\begin{vma ...

  2. HTML学习开篇

    最近开的博客,一切都是从零开始,昨天刚写了java的开篇,今天写一写HTML开篇. 很多初学者都不太看重前端的学习,甚至鄙视前端,我刚开始时就这样.其实,要想成为一个真正的程序员,前端和后端都必须了解 ...

  3. 利用backtrace和objdump进行分析挂掉的程序

    转自:http://blog.csdn.net/hanchaoman/article/details/5583457 汇编不懂,先把方法记下来. glibc为我们提供了此类能够dump栈内容的函数簇, ...

  4. 剑指offer一:二维数组中的查找

    题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路: 这是一个顺序二维 ...

  5. Pycharm使用问题# 快捷键设置

    Pycharm内部设置也已经变得非常复杂,此处说明我改变字体大小的快捷键设置. 在Settings中找到Keymap一项,默认显示会展开Editor Actions一项: 在里面分别找到Decreas ...

  6. MTF(Move-to-front transform)数据转换

    1.什么是MTF MTF(move-to-front)是一种数据编码方式,用于提高数据压缩技术效果. 在数据压缩算法中,MTF可以作为一个额外的步骤.也就是说 ,可以先进行MTF编码,在进行数据压缩. ...

  7. 数据库 基础篇3(mysql语法)

    4 数据库管理(接上篇) 4.1 查询所有数据库 mysql> show databases; +--------------------+ | Database           | +-- ...

  8. 怎么写jq插件?

    1.概述 先看看html代码 <ul id="catagory"> <li><a href="#">jQuery</a ...

  9. python 安装模块步骤

    1.下载 pyocr-0.4.1.tar.gz   tar.gz文件  解压  放到 c:/python27 文件夹下面 C:\Python27\pyocr-0.4.1  直接 cmd 命令 进入   ...

  10. 并发编程 19—— 显式的Conditon 对象

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...