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. 用JavaBean实现数据库的连接和关闭,在jsp页面输出数据库中student表中学生的信息

    package com.hanqi.test; import java.sql.*; public class XveSheng { Connection conn; Statement st; Re ...

  2. IE6兼容性问题及IE6常见bug详细汇总

    转载地址:http://www.jb51.net/css/76894.html 1.IE6怪异解析之padding与border算入宽高 原因:未加文档声明造成非盒模型解析 解决方法:加入文档声明&l ...

  3. 启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration.

    启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration. 出现此异常是因为,str ...

  4. bridge

    1 意图:将抽象部分与实现部分分离,使得它们都可以独立地变化 2 别名:Handle/Body 3 动机:客户在创建窗口时应该不涉及到具体实现部分.仅仅是窗口的实现部分依赖于应用运行的平台. 客户代码 ...

  5. spring boot初探

    又被领导鄙视了,说让先把程序跑起来,再去研究深层次的东西.. 我又一次没有学会走就要开始跑了..说干就干 eclipse mars下载 新建maven project 加依赖 <dependen ...

  6. Centos6下rpm安装MySQL5.6

    Centos6在rpm安装 rpm -ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm yum install ...

  7. MySQL SQL

    SQL语句错误: Column count doesn't match value count at row 1 列计数与第1行的值计数不匹配 You have an error in your SQ ...

  8. Linux运行级详解

    对于那些在DOS/Win9x/NT平台下的高级用户而言,Linux似乎是一个怪物.没有config.sys,没有 autoexec.bat,具有个人特色的机器配置不知道从何开始. 需要说明的是,很多人 ...

  9. 运用js解决java selenium元素定位问题

    一.解决定位并操作uneditable元素 尝试了通过id,xpath等等定位元素后点击都提示Element is not clickable at point 再看了下可以click的元素发现上面有 ...

  10. (Python)导出指定文件夹中as文件的完全限定类名

    AS3程序在编译的过程中,有一个特点是这样的,不管是项目中的类,还是标准库或者第三方库的类,编译的时候只会把用到的那些类文件编译进去,也就是说,某一些类,只要没有被主程序引用到,那这个文件是不会被编译 ...