Guest User

Untitled

a guest
Feb 4th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<sys/reg.h>
  3. #include<sys/types.h>
  4. #include<sys/ptrace.h>
  5. #include<sys/wait.h>
  6. #include<unistd.h>
  7. #include<sys/user.h>
  8. #include<sys/syscall.h>
  9.  
  10. void run_debugger(pid_t child_pid);
  11. void run_target(const char* programname);
  12. int main(int argc, char** argv)
  13. {
  14. pid_t child_pid;
  15.  
  16. if (argc < 2) {
  17. printf("Expected a program name as argument\n");
  18. return -1;
  19. }
  20.  
  21. child_pid = fork();
  22. if (child_pid == 0)
  23. run_target(argv[1]);
  24. else if (child_pid > 0)
  25. run_debugger(child_pid);
  26. else {
  27. printf("fork");
  28. return -1;
  29. }
  30.  
  31. return 0;
  32. }
  33. void run_target(const char* programname)
  34. {
  35. printf("target started.#include<stdio.h>
  36. #include<sys/reg.h>
  37. #include<sys/types.h>
  38. #include<sys/ptrace.h>
  39. #include<sys/wait.h>
  40. #include<unistd.h>
  41. #include<sys/user.h>
  42. #include<sys/syscall.h>
  43.  
  44. void run_debugger(pid_t child_pid);
  45. void run_target(const char* programname);
  46. int main(int argc, char** argv)
  47. {
  48. pid_t child_pid;
  49.  
  50. if (argc < 2) {
  51. printf("Expected a program name as argument\n");
  52. return -1;
  53. }
  54.  
  55. child_pid = fork();
  56. if (child_pid == 0)
  57. run_target(argv[1]);
  58. else if (child_pid > 0)
  59. run_debugger(child_pid);
  60. else {
  61. printf("fork");
  62. return -1;
  63. }
  64.  
  65. return 0;
  66. }
  67. void run_target(const char* programname)
  68. {
  69. printf("target started.#include<stdio.h>
  70. #include<sys/reg.h>
  71. #include<sys/types.h>
  72. #include<sys/ptrace.h>
  73. #include<sys/wait.h>
  74. #include<unistd.h>
  75. #include<sys/user.h>
  76. #include<sys/syscall.h>
  77.  
  78. void run_debugger(pid_t child_pid);
  79. void run_target(const char* programname);
  80. int main(int argc, char** argv)
  81. {
  82. pid_t child_pid;
  83.  
  84. if (argc < 2) {
  85. printf("Expected a program name as argument\n");
  86. return -1;
  87. }
  88.  
  89. child_pid = fork();
  90. if (child_pid == 0)
  91. run_target(argv[1]);
  92. else if (child_pid > 0)
  93. run_debugger(child_pid);
  94. else {
  95. printf("fork");
  96. return -1;
  97. }
  98.  
  99. return 0;
  100. }
  101. void run_target(const char* programname)
  102. {
  103. printf("target started.will run '%s'\n", programname);
  104.  
  105. /* Allow tracing of this process */
  106. if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
  107. printf("ptrace");
  108. return;
  109. }
  110. printf("replacing process image");
  111. /* Replace this process's image with the given program */
  112. execl(programname, programname, NULL);
  113. }
  114. void run_debugger(pid_t child_pid)
  115. {
  116. int wait_status;
  117. unsigned icounter = 0;
  118. printf("debugger started\n");
  119.  
  120. /* Wait for child to stop on its first instruction */
  121. wait(&wait_status);
  122.  
  123. while (WIFSTOPPED(wait_status)) {
  124. icounter++;
  125. struct user_regs_struct regs;
  126. ptrace(PTRACE_GETREGS, child_pid, 0, &regs);
  127. unsigned instr = ptrace(PTRACE_PEEKTEXT, child_pid, regs.rip, 0);
  128.  
  129. /*printf("icounter = %u. EIP = 0x%08lld. instr = 0x%08x\n",
  130. icounter, regs.rip, instr);*/
  131.  
  132. /* Make the child execute another instruction */
  133. if (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) {
  134. printf("ptrace");
  135. return;
  136. }
  137.  
  138. /* Wait for child to stop on its next instruction */
  139. wait(&wait_status);
  140. }
  141.  
  142. printf("the child executed %u instructions\n", icounter);
  143. }
Add Comment
Please, Sign In to add comment