trace.c

来自「一个简单的操作系统minix的核心代码」· C语言 代码 · 共 114 行

C
114
字号
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				src/mm/trace.c	 	 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

18600	/* This file handles the memory manager's part of debugging, using the 
18601	 * ptrace system call. Most of the commands are passed on to the system
18602	 * task for completion.
18603	 *
18604	 * The debugging commands available are:
18605	 * T_STOP       stop the process 
18606	 * T_OK         enable tracing by parent for this process
18607	 * T_GETINS     return value from instruction space 
18608	 * T_GETDATA    return value from data space 
18609	 * T_GETUSER    return value from user process table
18610	 * T_SETINS     set value in instruction space
18611	 * T_SETDATA    set value in data space
18612	 * T_SETUSER    set value in user process table 
18613	 * T_RESUME     resume execution 
18614	 * T_EXIT       exit
18615	 * T_STEP       set trace bit 
18616	 * 
18617	 * The T_OK and T_EXIT commands are handled here, and the T_RESUME and
18618	 * T_STEP commands are partially handled here and completed by the system
18619	 * task. The rest are handled entirely by the system task. 
18620	 */
18621	
18622	#include "mm.h"
18623	#include <sys/ptrace.h>
18624	#include <signal.h>
18625	#include "mproc.h"
18626	#include "param.h"
18627	
18628	#define NIL_MPROC       ((struct mproc *) 0)
18629	
18630	FORWARD _PROTOTYPE( struct mproc *findproc, (pid_t lpid) );
18631	
18632	/*===========================================================================*
18633	 *                              do_trace                                     *
18634	 *===========================================================================*/
18635	PUBLIC int do_trace()
18636	{
18637	  register struct mproc *child;
18638	
18639	  /* the T_OK call is made by the child fork of the debugger before it execs  
18640	   * the process to be traced
18641	   */
18642	  if (request == T_OK) {/* enable tracing by parent for this process */
18643	        mp->mp_flags |= TRACED;
18644	        mm_out.m2_l2 = 0;
18645	        return(OK);
18646	  }
18647	  if ((child = findproc(pid)) == NIL_MPROC || !(child->mp_flags & STOPPED)) {
18648	        return(ESRCH);
18649	  }
18650	  /* all the other calls are made by the parent fork of the debugger to 
18651	   * control execution of the child
18652	   */
18653	  switch (request) {
18654	  case T_EXIT:          /* exit */
18655	        mm_exit(child, (int)data);
18656	        mm_out.m2_l2 = 0;
18657	        return(OK);
18658	  case T_RESUME: 
18659	  case T_STEP:          /* resume execution */
18660	        if (data < 0 || data > _NSIG) return(EIO);
18661	        if (data > 0) {         /* issue signal */
18662	                child->mp_flags &= ~TRACED;  /* so signal is not diverted */
18663	                sig_proc(child, (int) data);
18664	                child->mp_flags |= TRACED;
18665	        }
18666	        child->mp_flags &= ~STOPPED;
18667	        break;
18668	  }
18669	  if (sys_trace(request, (int) (child - mproc), taddr, &data) != OK)
18670	        return(-errno);
18671	  mm_out.m2_l2 = data;
18672	  return(OK);
18673	}
	
18675	/*===========================================================================*
18676	 *                              findproc                                     *
18677	 *===========================================================================*/
18678	PRIVATE struct mproc *findproc(lpid)
18679	pid_t lpid;
18680	{
18681	  register struct mproc *rmp;
18682	
18683	  for (rmp = &mproc[INIT_PROC_NR + 1]; rmp < &mproc[NR_PROCS]; rmp++)
18684	        if (rmp->mp_flags & IN_USE && rmp->mp_pid == lpid) return(rmp);
18685	  return(NIL_MPROC);
18686	}
	
18688	/*===========================================================================*
18689	 *                              stop_proc                                    *
18690	 *===========================================================================*/
18691	PUBLIC void stop_proc(rmp, signo)
18692	register struct mproc *rmp;
18693	int signo;
18694	{
18695	/* A traced process got a signal so stop it. */
18696	
18697	  register struct mproc *rpmp = mproc + rmp->mp_parent;
18698	
18699	  if (sys_trace(-1, (int) (rmp - mproc), 0L, (long *) 0) != OK) return;
18700	  rmp->mp_flags |= STOPPED;
18701	  if (rpmp->mp_flags & WAITING) {
18702	        rpmp->mp_flags &= ~WAITING;     /* parent is no longer waiting */
18703	        reply(rmp->mp_parent, rmp->mp_pid, 0177 | (signo << 8), NIL_PTR);
18704	  } else {
18705	        rmp->mp_sigstatus = signo;
18706	  }
18707	  return;
18708	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?