Windows下父进程监视子进程状态
最近研究自动化测试,需要获取程序的运行状态及结果,下面是些参考资料。
原文地址:http://blog.csdn.net/ariesjzj/article/details/7226443
Linux下有功能强大ptrace,用于让父进程监视/修改/控制子进程的状态。Windows也提供了类似的接口,那就是Debuging API,用它可以编写用户级的调试器。
下面是一个例子,用以实现父进程创建并监视子进程运行状态。
- #include <stddef.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <assert.h>
- #include <windows.h>
- #define MAX_PARAM_LEN 4096
- int main( int argc, char ** argv )
- {
- int i, j = 0, len;
- char command_buf[MAX_PARAM_LEN];
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- DEBUG_EVENT de;
- BOOL stop = FALSE;
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- if (argc<2) {
- printf("Usage: %s <app_name> [arguments ...]\n", argv[0]);
- return 0;
- }
- // Combine the module name and params into one string.
- for (i = 1; i < argc; ++i) {
- len = strlen(argv[i]);
- if (len >= MAX_PARAM_LEN - j - 1) {
- printf("buffer overflow\n");
- exit(-1);
- }
- j += _snprintf(command_buf + j, MAX_PARAM_LEN - j, "%s ", argv[i]);
- command_buf[j] = '\0'; // just for sure
- }
- if( !CreateProcess(NULL, command_buf, NULL, NULL, FALSE,
- DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi ) ) {
- printf( "CreateProcess failed (%d).\n", GetLastError() );
- exit(-1);
- }
- while (TRUE) {
- WaitForDebugEvent (&de, INFINITE);
- switch (de.dwDebugEventCode) {
- case EXCEPTION_DEBUG_EVENT: /* exception */
- switch (de.u.Exception.ExceptionRecord.ExceptionCode) {
- case EXCEPTION_INT_DIVIDE_BY_ZERO: /* #DE */
- // Do what the parent process want to do when the child process gets #DE interrupt.
- TerminateProcess(pi.hProcess,1);
- break;
- case EXCEPTION_BREAKPOINT: /* #BP */
- // Do what the parent process want to do when the child process gets #BP interrupt.
- break;
- default:
- printf("Unknown Exception\n");
- break;
- }
- ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_EXCEPTION_HANDLED);
- continue;
- case CREATE_PROCESS_DEBUG_EVENT: /* child process created */
- // Do what the parent process want to do when the child process was created.
- break;
- case EXIT_PROCESS_DEBUG_EVENT: /* child process exits */
- stop = TRUE;
- // Do what the parent process want to do when the child process exits.
- break;
- default:
- printf("Unknown Event!\n");
- break;
- }
- if (TRUE == stop) {
- //printf("Process exit\n");
- break;
- }
- ContinueDebugEvent (de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
- } // end of loop
- assert(stop);
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- return 0;
- }
程序参数为要监视的子进程及子进程的参数。注意一个正常的进程被创建出来后会先后收到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下父进程监视子进程状态的更多相关文章
- Linux 进程--父进程查询子进程的退出状态
僵尸进程 当一个子进程先于父进程结束运行时,它与其父进程之间的关联还会保持到父进程也正常地结束运行,或者父进程调用了wait才告终止. 子进程退出时,内核将子进程置为僵尸状态,这个进程称为僵尸进程,它 ...
- Linux下利用fork()创建子进程并使父进程等待子进程结束
int status; pid_t t = fork(); if(t){ waitpid(t, &status, 0); }else{ system("vi temp ...
- Windows下的进程【一】
什么是进程?进程就是一个正在运行的程序的实例,由两部分组成: 内核对象.操作系统用内核对象对进程进行管理,内核对象是操作系统保存进程统计信息的地方. 地址空间.其中包含所有可执行文件或DLL模块的代码 ...
- [转]Windows 下的进程间通讯及数据共享
http://blog.codingnow.com/2005/10/interprocess_communications.html Windows 下有很多方法实现进程间通讯,比如用 socket, ...
- 父进程等待子进程结束 waitpid wait
我们一直在强调一个概念就是进程是一个程序执行的实例,是内核在虚拟概念下创建的实体,它实例化的体现在用户态就是程序代码和代码使用的变量(存储空间),在内核态就是内核为我们每个进程所保存的数据结构(状态信 ...
- linux系统编程之进程(六):父进程查询子进程的退出,wait,waitpid
本节目标: 僵进程 SIGCHLD wait waitpid 一,僵尸进程 当一个子进程先于父进程结束运行时,它与其父进程之间的关联还会保持到父进程也正常地结束运行,或者父进程调用了wait才告终止. ...
- PHP多进程学习(三)__代码案例来了解父进程与子进程的执行顺序
pcntl_fork创建子进程成功的话,系统就有了2个进程,一个为父进程,一个为子进程,父进程和子进程都继续向下执行,子进程的id号为$pid(父进程会获取子进程的$pid也就是$pid不为0,而子进 ...
- Windows下tomcat进程监控批处理程序
在Windows下tomcat进程监控批处理程序脚本如下: @echo off ::tomcat安装目录 set _tomcatDir=E:\myFiles\apache-tomcat-8.5.31 ...
- Windows下查看进程及结束进程命令[转]
Windows下查看进程及结束进程命令 1)查看占用8080端口的进程号 >netstat –aon | findstr “8080” 结果:TCP 0.0.0.0:8080 ...
随机推荐
- MVP快速开发框架
所谓MVP(Model-View-Presenter)模式.是将APP的结构分为三层: view - UI显示层 view 层主要负责: 提供UI交互 在presenter的控制下修改UI. 将业务事 ...
- .net下载文件方法
1.以文件流下载 byte[] fileStr=new byte[5]; MemoryStream btMs = new MemoryStream(fileStr); //以字符流的形式下载文件 by ...
- 快速消除IOS 版本升级带来的警告
开发中我们经常会遇到这样的情况,我们在IOS 6.0开发的程序,当出现IOS 7.0 或者IOS8.0的时候,我们代码中得某些方法苹果已经不推荐使用了,建议我们改用新的方法.如果我们不更新方法,则会出 ...
- linux命令行抓取网页快照-(xvfb+CutyCapt)
目的: 在一台没有安装X-server的Debian服务器上实现命令行抓取网页快照 软件: xvfb(在命令行下实现对X-server的模拟,渲染图形进行缓存)-在没有安装X-Server的环境下提供 ...
- 一个在线的C++帮助文档网站 转载
http://www.cplusplus.com/ //C++参考 http://www.cppreference.com/wiki/start //C++参考 http ...
- (转)Android开发:性能最佳实践-管理应用内存
翻自:http://developer.android.com/training/articles/memory.html 在任何软件开发环境中,RAM都是宝贵的资源,但在移动操作系统中更加珍贵.尽管 ...
- [Neural Networks] Momentum
一.目的 加快参数的收敛速度. 二.做法 另第t次的权重更新对第t+1次的权重更新造成影响. 从上式可看出,加入momentum后能够保持权重的更新方向,同时加快收敛.通常alpha的取值为[0.7, ...
- 关于abbr 的一些用法
<span style="margin: 20px;">位 000置:</span><abbr title='四川省绵阳市梓潼县文昌镇金龙乡一大队二组 ...
- 使用jQuery验证用户名是否存在,达到局部刷新的效果
<%@ page pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- Jquery 和 Js
jQuery [一] jQuery 1: javaScript和 javaScript库: (1) javaScript自身存在的3个弊端:复杂的文档对象模型(DOM),不 ...