Stack walk on linux using ptrace

The following is my requirement.

when process A. is running

  • attach process A from B using PTRACE_ATTACH.
  • Start cycle
  • Stop Process A
  • reading registers
  • Renewal Process A
  • sleep (1)
  • end of cycle
  • disconnect A

I ran into a problem starting and resuming process A from a loop. I tried a combination of kill (pid, SIGSTOP), kill (pid, SIGCONT), PTRACE_CONT. but does not work.

any other solutions please?

Thanks in advance. Sandeep

+3
source share
3 answers

The following code works for me and seems to meet your requirements -

Ac

#include<stdio.h>
int main()
{
   int i=0;
   printf("My PID is - %ld\n",getpid());
   while(i>=0)
   {
   }
   return 0;
}

Bc - Tracing Process

int main()
{
   int pid;
   int status;
   struct user_regs_struct regs;
   unsigned int eip;

   printf("Enter pid to trace : \n");
   scanf("%d",&pid);
   printf("PID to be traced - %ld\n",pid);

   ptrace(PTRACE_ATTACH,pid,0,0);
   if(errno)
   {
        perror("attach");
        return -1;
   }

   waitpid(pid,&status,WUNTRACED);

   printf("Process Stopped\n");
   while(1)
   {
      ptrace(PTRACE_GETREGS,pid,0,&regs);
      eip=ptrace(PTRACE_PEEKTEXT,pid,regs.eip,0);

      printf("EIP - 0x%08x, instruction executed - 0x%08x\n",regs.eip,eip);

      ptrace(PTRACE_CONT,pid,0,0);
      waitpid(pid,&status,WUNTRACED);
   }

   return 0;

}

The signal has passed -

kill -STOP 17779 kill -STOP 17779

Output A -

xxxxx!xxxxx:~/myPer/stack_overflow [135]$ ./A
My PID is - 17779

Output B -

XXXXX!xxxxx:~/myPer/stack_overflow [121]$ ./B
Enter pid to trace :
17779
PID to be traced - 17779
Process Stopped
EIP - 0x080483e1, instruction executed - 0x00f87d83
EIP - 0x080483e5, instruction executed - 0x00b8fa79
EIP - 0x080483e5, instruction executed - 0x00b8fa79

, B EIP . A, B EIP, . , .

, . - , , .

+1

, . - GNU? , libgdb2, , .

0

You can try using scripts / pairing with gdb in the same way as many IDEs do. See also http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gdb/gdb-mi.html

0
source

Source: https://habr.com/ru/post/1753161/


All Articles