最近研究自动化测试,需要获取程序的运行状态及结果,下面是些参考资料。

原文地址:http://blog.csdn.net/ariesjzj/article/details/7226443

Linux下有功能强大ptrace,用于让父进程监视/修改/控制子进程的状态。Windows也提供了类似的接口,那就是Debuging API,用它可以编写用户级的调试器。

下面是一个例子,用以实现父进程创建并监视子进程运行状态。

  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <assert.h>
  7. #include <windows.h>
  8. #define MAX_PARAM_LEN       4096
  9. int main( int argc, char ** argv )
  10. {
  11. int i, j = 0, len;
  12. char command_buf[MAX_PARAM_LEN];
  13. STARTUPINFO si;
  14. PROCESS_INFORMATION pi;
  15. DEBUG_EVENT de;
  16. BOOL stop = FALSE;
  17. ZeroMemory( &si, sizeof(si) );
  18. si.cb = sizeof(si);
  19. ZeroMemory( &pi, sizeof(pi) );
  20. if (argc<2) {
  21. printf("Usage: %s <app_name> [arguments ...]\n", argv[0]);
  22. return 0;
  23. }
  24. // Combine the module name and params into one string.
  25. for (i = 1; i < argc; ++i) {
  26. len = strlen(argv[i]);
  27. if (len >= MAX_PARAM_LEN - j - 1) {
  28. printf("buffer overflow\n");
  29. exit(-1);
  30. }
  31. j += _snprintf(command_buf + j, MAX_PARAM_LEN - j, "%s ", argv[i]);
  32. command_buf[j] = '\0';  // just for sure
  33. }
  34. if( !CreateProcess(NULL, command_buf, NULL, NULL, FALSE,
  35. DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi ) ) {
  36. printf( "CreateProcess failed (%d).\n", GetLastError() );
  37. exit(-1);
  38. }
  39. while (TRUE) {
  40. WaitForDebugEvent (&de, INFINITE);
  41. switch (de.dwDebugEventCode) {
  42. case EXCEPTION_DEBUG_EVENT:         /* exception */
  43. switch (de.u.Exception.ExceptionRecord.ExceptionCode) {
  44. case   EXCEPTION_INT_DIVIDE_BY_ZERO:    /* #DE */
  45. // Do what the parent process want to do when the child process gets #DE interrupt.
  46. TerminateProcess(pi.hProcess,1);
  47. break;
  48. case   EXCEPTION_BREAKPOINT:            /* #BP */
  49. // Do what the parent process want to do when the child process gets #BP interrupt.
  50. break;
  51. default:
  52. printf("Unknown Exception\n");
  53. break;
  54. }
  55. ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_EXCEPTION_HANDLED);
  56. continue;
  57. case CREATE_PROCESS_DEBUG_EVENT:        /* child process created */
  58. // Do what the parent process want to do when the child process was created.
  59. break;
  60. case EXIT_PROCESS_DEBUG_EVENT:          /* child process exits */
  61. stop = TRUE;
  62. // Do what the parent process want to do when the child process exits.
  63. break;
  64. default:
  65. printf("Unknown Event!\n");
  66. break;
  67. }
  68. if (TRUE == stop) {
  69. //printf("Process exit\n");
  70. break;
  71. }
  72. ContinueDebugEvent (de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
  73. } // end of loop
  74. assert(stop);
  75. CloseHandle( pi.hProcess );
  76. CloseHandle( pi.hThread );
  77. return 0;
  78. }

程序参数为要监视的子进程及子进程的参数。注意一个正常的进程被创建出来后会先后收到CREATE_PROCESS_DEBUG_EVENT, EXCEPTION_DEBUG_EVENT中的EXCEPTION_BREAKPOINT和EXIT_PROCESS_DEBUG_EVENT。所以如果你不想子进程创建起来就出错,那就让处理断点的分支跳去执行ContinueDebugEvent(..., DBG_EXCEPTION_HANDLED)。

例子仅含框架,如要attach到已有进程请参见DebugActiveProcess,要修改子进程状态请参见RriteProcessMemory和WriteProcessMemory等函数。

一些参考资料:

Debugging API examples: http://www.debuginfo.com/examples/dbgexamples.html

Writing the Debugger's Main Loop: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681675(v=vs.85).aspx

Using the Windows Debugging API: http://www.howzatt.demon.co.uk/articles/SimpleDebugger.html

Debugging Functions: http://msdn.microsoft.com/en-us/library/ms679303

Win32调试API:http://hi.baidu.com/combojiang/blog/item/efb56e8ff0ebbfebf11f3654.html

利用Win32 Debug API打造自己的Debugger: http://hi.baidu.com/olhack/blog/item/c1e896508250e86284352407.html

The Debugging Application Programming Interface: http://msdn.microsoft.com/en-us/library/ms809754.aspx

在主进程中捕获子进程的异常:http://blog.csdn.net/simbi/article/details/3705719

Windows Debugging API: http://my.safaribooksonline.com/book/networking/intrusion-detection/9780321446114/in-memory-fuzzing-automation/ch20lev1sec3

Windows下父进程监视子进程状态的更多相关文章

  1. Linux 进程--父进程查询子进程的退出状态

    僵尸进程 当一个子进程先于父进程结束运行时,它与其父进程之间的关联还会保持到父进程也正常地结束运行,或者父进程调用了wait才告终止. 子进程退出时,内核将子进程置为僵尸状态,这个进程称为僵尸进程,它 ...

  2. Linux下利用fork()创建子进程并使父进程等待子进程结束

    int status; pid_t t = fork(); if(t){     waitpid(t, &status, 0); }else{     system("vi temp ...

  3. Windows下的进程【一】

    什么是进程?进程就是一个正在运行的程序的实例,由两部分组成: 内核对象.操作系统用内核对象对进程进行管理,内核对象是操作系统保存进程统计信息的地方. 地址空间.其中包含所有可执行文件或DLL模块的代码 ...

  4. [转]Windows 下的进程间通讯及数据共享

    http://blog.codingnow.com/2005/10/interprocess_communications.html Windows 下有很多方法实现进程间通讯,比如用 socket, ...

  5. 父进程等待子进程结束 waitpid wait

    我们一直在强调一个概念就是进程是一个程序执行的实例,是内核在虚拟概念下创建的实体,它实例化的体现在用户态就是程序代码和代码使用的变量(存储空间),在内核态就是内核为我们每个进程所保存的数据结构(状态信 ...

  6. linux系统编程之进程(六):父进程查询子进程的退出,wait,waitpid

    本节目标: 僵进程 SIGCHLD wait waitpid 一,僵尸进程 当一个子进程先于父进程结束运行时,它与其父进程之间的关联还会保持到父进程也正常地结束运行,或者父进程调用了wait才告终止. ...

  7. PHP多进程学习(三)__代码案例来了解父进程与子进程的执行顺序

    pcntl_fork创建子进程成功的话,系统就有了2个进程,一个为父进程,一个为子进程,父进程和子进程都继续向下执行,子进程的id号为$pid(父进程会获取子进程的$pid也就是$pid不为0,而子进 ...

  8. Windows下tomcat进程监控批处理程序

    在Windows下tomcat进程监控批处理程序脚本如下: @echo off ::tomcat安装目录 set _tomcatDir=E:\myFiles\apache-tomcat-8.5.31 ...

  9. Windows下查看进程及结束进程命令[转]

    Windows下查看进程及结束进程命令 1)查看占用8080端口的进程号 >netstat –aon | findstr “8080” 结果:TCP    0.0.0.0:8080        ...

随机推荐

  1. Local IIS 7.0 - CS0016: Could not write to output file / Microsoft.Net > Framework > v4.0.30319 > Temporary ASP.NET Files

    This week I went nuts over my local IIS. I have never swore to a machine that much in my whole life. ...

  2. oracle编译 失效对象方式

    如果procedure 所使用的表结构发生了改变等其它情况,在相应的xxx_objects表的status字段会变为invalid状态,但是如果在调用时procedure会自动编译,grant失效对象 ...

  3. power shell upload file to azure storage

    # Azure subscription-specific variables. $storageAccountName = "storage-account-name" $con ...

  4. XhProf安装教程–详细教程

    wget去这里找最新的包下载,我现在最新的是xhprof-0.9.4.tgz cd /tmp mkdir xhprof && cd xhprof wget http://pecl.ph ...

  5. React初探

    经过几天根据官方文档和博园中一些大牛的文章,在了解过基础的语法和组件后,总结一下: 1.第一件事就是分析界面,理想状态下是让每个组件只做一件事情,也就是单一职责,相互嵌套.我认为: 构建组件树,整体的 ...

  6. Bresenham画直线,任意斜率

    function DrawLineBresenham(x1,y1,x2,y2) %sort by x,sure x1<x2. if x1>x2 tmp=x1; x1=x2; x2=tmp; ...

  7. 关于boost::function与boost::bind函数的使用心得

    最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...

  8. JavaScript学习心得(二)

    一选择DOCTYPE DOCTYPE是一种标准通用标记语言的文档类型声明,目的是告诉标准通用标记语言解析器使用什么样的文档类型定义(DTD)来解析文档. 网页从DOCTYPE开始,即<!DOCT ...

  9. Android新建项目 默认布局改为 LinearLayout

    目前此方法仅适用于eclipse 需要修改SDK 目录 android-sdk/tools/templates/activities/BlankActivity/root/res/layout 文件: ...

  10. Mac Pro的HDMI接口与WI-FI可能存在冲突的解决方法

    当我将Mac Pro通过HDMI->DVI转接头接上一台显示器时,正在使用的WI-FI网络立马不能使用,重新连接网络也不行,但断开HDMI连接后,WI-FI立马恢复正常. 在网上查,在apple ...