blob: 85276d732f77def0ef0c9519bbe03c77445e58a3 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#define _GNU_SOURCE 1
Juan Cespedes1cd999a2001-07-03 00:46:04 +02006#include <stdlib.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01007#include <sys/types.h>
8#include <sys/wait.h>
9#include <errno.h>
10#include <signal.h>
11#include <string.h>
Juan Cespedes1e583132009-04-07 18:17:11 +020012#include <sys/ptrace.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013
14#include "ltrace.h"
15#include "options.h"
Juan Cespedescde58262009-05-07 11:09:00 +020016#include "output.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010017#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Juan Cespedes393f1d02009-05-07 11:13:54 +020019static Event event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010020
Juan Cespedes393f1d02009-05-07 11:13:54 +020021Event *
Juan Cespedese2023f72009-04-07 12:12:08 +020022next_event(void) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010023 pid_t pid;
24 int status;
25 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +020026 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +010027
Juan Cespedes28f60191998-04-12 00:04:39 +020028 if (!list_of_processes) {
Juan Cespedescac15c32003-01-31 18:58:58 +010029 debug(1, "No more children");
Juan Cespedes28f60191998-04-12 00:04:39 +020030 exit(0);
31 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010032 pid = wait(&status);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 if (pid == -1) {
34 if (errno == ECHILD) {
Juan Cespedescac15c32003-01-31 18:58:58 +010035 debug(1, "No more children");
Juan Cespedes5e01f651998-03-08 22:31:44 +010036 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010037 } else if (errno == EINTR) {
Juan Cespedescac15c32003-01-31 18:58:58 +010038 debug(1, "wait received EINTR ?");
Juan Cespedes138d41c2009-04-07 00:49:12 +020039 event.thing = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +020040 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010041 }
42 perror("wait");
43 exit(1);
44 }
45 event.proc = pid2proc(pid);
46 if (!event.proc) {
Juan Cespedesa8909f72009-04-28 20:02:41 +020047 output_line(NULL, "event from wrong pid %u ?!?\n", pid);
48// exit(1);
49 event.thing = EVENT_NONE;
50 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010051 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +020052 get_arch_dep(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010053 event.proc->instruction_pointer = NULL;
Juan Cespedes1e583132009-04-07 18:17:11 +020054 debug(3, "event from pid %u", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010055 if (event.proc->breakpoints_enabled == -1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010056 enable_all_breakpoints(event.proc);
Juan Cespedes138d41c2009-04-07 00:49:12 +020057 event.thing = EVENT_NONE;
Ian Wienand9a2ad352006-02-20 22:44:45 +010058 trace_set_options(event.proc, event.proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010059 continue_process(event.proc->pid);
60 return &event;
61 }
Juan Cespedes35d70631998-03-15 14:05:40 +010062 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 event.proc->instruction_pointer =
64 get_instruction_pointer(event.proc);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010065 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +010067 case 1:
Juan Cespedes138d41c2009-04-07 00:49:12 +020068 event.thing = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +010069 event.e_un.sysnum = tmp;
70 return &event;
71 case 2:
Juan Cespedes138d41c2009-04-07 00:49:12 +020072 event.thing = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +010073 event.e_un.sysnum = tmp;
74 return &event;
75 case 3:
Juan Cespedes138d41c2009-04-07 00:49:12 +020076 event.thing = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +010077 event.e_un.sysnum = tmp;
78 return &event;
79 case 4:
Juan Cespedes138d41c2009-04-07 00:49:12 +020080 event.thing = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +010081 event.e_un.sysnum = tmp;
82 return &event;
83 case -1:
Juan Cespedes138d41c2009-04-07 00:49:12 +020084 event.thing = EVENT_NONE;
Juan Cespedes63184be2008-12-10 13:30:12 +010085 continue_process(event.proc->pid);
86 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010087 }
Juan Cespedes1e583132009-04-07 18:17:11 +020088 if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) {
89 unsigned long data;
90 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
91 event.thing = EVENT_FORK;
92 event.e_un.newpid = data;
93 return &event;
94 }
95 /* TODO: check for EVENT_CLONE */
96 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
97 event.thing = EVENT_EXEC;
98 return &event;
99 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100100 if (WIFEXITED(status)) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200101 event.thing = EVENT_EXIT;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100102 event.e_un.ret_val = WEXITSTATUS(status);
103 return &event;
104 }
105 if (WIFSIGNALED(status)) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200106 event.thing = EVENT_EXIT_SIGNAL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100107 event.e_un.signum = WTERMSIG(status);
108 return &event;
109 }
110 if (!WIFSTOPPED(status)) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200111 event.thing = EVENT_NONE;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100112 return &event;
113 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200114
115 stop_signal = WSTOPSIG(status);
116
117 /* On some targets, breakpoints are signalled not using
118 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. Check
119 for these. */
120 if (stop_signal == SIGSEGV
121 || stop_signal == SIGILL
122#ifdef SIGEMT
123 || stop_signal == SIGEMT
124#endif
125 ) {
126 // If we didn't need to know IP so far, get it now.
127 void * addr = opt_i
128 ? event.proc->instruction_pointer
129 : (event.proc->instruction_pointer = get_instruction_pointer (event.proc));
130
131 if (address2bpstruct(event.proc, addr))
132 stop_signal = SIGTRAP;
133 }
134
135 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
136 && stop_signal != SIGTRAP) {
Juan Cespedes138d41c2009-04-07 00:49:12 +0200137 event.thing = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200138 event.e_un.signum = stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100139 return &event;
140 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200141
142 if (was_exec(event.proc, status)) {
143 pid_t saved_pid;
144
Juan Cespedes138d41c2009-04-07 00:49:12 +0200145 event.thing = EVENT_NONE;
Petr Machata55ed83b2007-05-17 16:24:15 +0200146 event.e_un.signum = WSTOPSIG(status);
147 debug(1, "Placing breakpoints for the new program");
148 event.proc->mask_32bit = 0;
149 event.proc->personality = 0;
150 event.proc->arch_ptr = NULL;
151 event.proc->filename = pid2name(event.proc->pid);
152 saved_pid = event.proc->pid;
153 event.proc->pid = 0;
154 breakpoints_init(event.proc);
155 event.proc->pid = saved_pid;
156 continue_process(event.proc->pid);
157 return &event;
158 }
159
Juan Cespedes138d41c2009-04-07 00:49:12 +0200160 event.thing = EVENT_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +0100161 if (!event.proc->instruction_pointer) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100162 event.proc->instruction_pointer =
163 get_instruction_pointer(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +0100164 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100165 event.e_un.brk_addr =
166 event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100167 return &event;
168}