blob: dfb8284f64c3ccb7908bc846572776ea1f600683 [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 Cespedes8f6d1ec2009-05-07 17:50:34 +020039 event.type = 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 Cespedes8f6d1ec2009-05-07 17:50:34 +020047 event.type = EVENT_NEW;
Juan Cespedesa8909f72009-04-28 20:02:41 +020048 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010049 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +020050 get_arch_dep(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010051 event.proc->instruction_pointer = NULL;
Juan Cespedes1e583132009-04-07 18:17:11 +020052 debug(3, "event from pid %u", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010053 if (event.proc->breakpoints_enabled == -1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010054 enable_all_breakpoints(event.proc);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020055 event.type = EVENT_NONE;
Ian Wienand9a2ad352006-02-20 22:44:45 +010056 trace_set_options(event.proc, event.proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010057 continue_process(event.proc->pid);
58 return &event;
59 }
Juan Cespedes35d70631998-03-15 14:05:40 +010060 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 event.proc->instruction_pointer =
62 get_instruction_pointer(event.proc);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010063 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +010065 case 1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020066 event.type = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +010067 event.e_un.sysnum = tmp;
68 return &event;
69 case 2:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020070 event.type = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +010071 event.e_un.sysnum = tmp;
72 return &event;
73 case 3:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020074 event.type = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +010075 event.e_un.sysnum = tmp;
76 return &event;
77 case 4:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020078 event.type = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +010079 event.e_un.sysnum = tmp;
80 return &event;
81 case -1:
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020082 event.type = EVENT_NONE;
Juan Cespedes63184be2008-12-10 13:30:12 +010083 continue_process(event.proc->pid);
84 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010085 }
Juan Cespedes1e583132009-04-07 18:17:11 +020086 if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) {
87 unsigned long data;
88 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020089 event.type = EVENT_CLONE;
Juan Cespedes1e583132009-04-07 18:17:11 +020090 event.e_un.newpid = data;
91 return &event;
92 }
Juan Cespedes1e583132009-04-07 18:17:11 +020093 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020094 event.type = EVENT_EXEC;
Juan Cespedes1e583132009-04-07 18:17:11 +020095 return &event;
96 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010097 if (WIFEXITED(status)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +020098 event.type = EVENT_EXIT;
Juan Cespedes5e01f651998-03-08 22:31:44 +010099 event.e_un.ret_val = WEXITSTATUS(status);
100 return &event;
101 }
102 if (WIFSIGNALED(status)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200103 event.type = EVENT_EXIT_SIGNAL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100104 event.e_un.signum = WTERMSIG(status);
105 return &event;
106 }
107 if (!WIFSTOPPED(status)) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200108 event.type = EVENT_NONE;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100109 return &event;
110 }
Petr Machataef46b3e2007-05-09 19:21:42 +0200111
112 stop_signal = WSTOPSIG(status);
113
114 /* On some targets, breakpoints are signalled not using
115 SIGTRAP, but also with SIGILL, SIGSEGV or SIGEMT. Check
116 for these. */
117 if (stop_signal == SIGSEGV
118 || stop_signal == SIGILL
119#ifdef SIGEMT
120 || stop_signal == SIGEMT
121#endif
122 ) {
123 // If we didn't need to know IP so far, get it now.
124 void * addr = opt_i
125 ? event.proc->instruction_pointer
126 : (event.proc->instruction_pointer = get_instruction_pointer (event.proc));
127
128 if (address2bpstruct(event.proc, addr))
129 stop_signal = SIGTRAP;
130 }
131
132 if (stop_signal != (SIGTRAP | event.proc->tracesysgood)
133 && stop_signal != SIGTRAP) {
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200134 event.type = EVENT_SIGNAL;
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200135 event.e_un.signum = stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 return &event;
137 }
Petr Machata55ed83b2007-05-17 16:24:15 +0200138
139 if (was_exec(event.proc, status)) {
140 pid_t saved_pid;
141
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200142 event.type = EVENT_NONE;
Petr Machata55ed83b2007-05-17 16:24:15 +0200143 event.e_un.signum = WSTOPSIG(status);
144 debug(1, "Placing breakpoints for the new program");
145 event.proc->mask_32bit = 0;
146 event.proc->personality = 0;
147 event.proc->arch_ptr = NULL;
148 event.proc->filename = pid2name(event.proc->pid);
149 saved_pid = event.proc->pid;
150 event.proc->pid = 0;
151 breakpoints_init(event.proc);
152 event.proc->pid = saved_pid;
153 continue_process(event.proc->pid);
154 return &event;
155 }
156
Juan Cespedes8f6d1ec2009-05-07 17:50:34 +0200157 event.type = EVENT_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +0100158 if (!event.proc->instruction_pointer) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100159 event.proc->instruction_pointer =
160 get_instruction_pointer(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +0100161 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100162 event.e_un.brk_addr =
163 event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100164 return &event;
165}