blob: d49f6a53a2c4cfb3b3fb8666b06a413b7680c5b2 [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 Cespedescac15c32003-01-31 18:58:58 +010016#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010017
18static struct event event;
19
Juan Cespedesf1350522008-12-16 18:19:58 +010020struct event *
Juan Cespedese2023f72009-04-07 12:12:08 +020021next_event(void) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010022 pid_t pid;
23 int status;
24 int tmp;
Petr Machataef46b3e2007-05-09 19:21:42 +020025 int stop_signal;
Juan Cespedes5e01f651998-03-08 22:31:44 +010026
Juan Cespedes28f60191998-04-12 00:04:39 +020027 if (!list_of_processes) {
Juan Cespedescac15c32003-01-31 18:58:58 +010028 debug(1, "No more children");
Juan Cespedes28f60191998-04-12 00:04:39 +020029 exit(0);
30 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010031 pid = wait(&status);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032 if (pid == -1) {
33 if (errno == ECHILD) {
Juan Cespedescac15c32003-01-31 18:58:58 +010034 debug(1, "No more children");
Juan Cespedes5e01f651998-03-08 22:31:44 +010035 exit(0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036 } else if (errno == EINTR) {
Juan Cespedescac15c32003-01-31 18:58:58 +010037 debug(1, "wait received EINTR ?");
Juan Cespedes138d41c2009-04-07 00:49:12 +020038 event.thing = EVENT_NONE;
Juan Cespedes28f60191998-04-12 00:04:39 +020039 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010040 }
41 perror("wait");
42 exit(1);
43 }
44 event.proc = pid2proc(pid);
45 if (!event.proc) {
Juan Cespedes1e583132009-04-07 18:17:11 +020046 fprintf(stderr, "event from wrong pid %u ?!?\n", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010047 exit(1);
48 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +020049 get_arch_dep(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010050 event.proc->instruction_pointer = NULL;
Juan Cespedes1e583132009-04-07 18:17:11 +020051 debug(3, "event from pid %u", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010052 if (event.proc->breakpoints_enabled == -1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010053 enable_all_breakpoints(event.proc);
Juan Cespedes138d41c2009-04-07 00:49:12 +020054 event.thing = EVENT_NONE;
Ian Wienand9a2ad352006-02-20 22:44:45 +010055 trace_set_options(event.proc, event.proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010056 continue_process(event.proc->pid);
57 return &event;
58 }
Juan Cespedes35d70631998-03-15 14:05:40 +010059 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010060 event.proc->instruction_pointer =
61 get_instruction_pointer(event.proc);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010062 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 switch (syscall_p(event.proc, status, &tmp)) {
Juan Cespedes63184be2008-12-10 13:30:12 +010064 case 1:
Juan Cespedes138d41c2009-04-07 00:49:12 +020065 event.thing = EVENT_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +010066 event.e_un.sysnum = tmp;
67 return &event;
68 case 2:
Juan Cespedes138d41c2009-04-07 00:49:12 +020069 event.thing = EVENT_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +010070 event.e_un.sysnum = tmp;
71 return &event;
72 case 3:
Juan Cespedes138d41c2009-04-07 00:49:12 +020073 event.thing = EVENT_ARCH_SYSCALL;
Juan Cespedes63184be2008-12-10 13:30:12 +010074 event.e_un.sysnum = tmp;
75 return &event;
76 case 4:
Juan Cespedes138d41c2009-04-07 00:49:12 +020077 event.thing = EVENT_ARCH_SYSRET;
Juan Cespedes63184be2008-12-10 13:30:12 +010078 event.e_un.sysnum = tmp;
79 return &event;
80 case -1:
Juan Cespedes138d41c2009-04-07 00:49:12 +020081 event.thing = EVENT_NONE;
Juan Cespedes63184be2008-12-10 13:30:12 +010082 continue_process(event.proc->pid);
83 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010084 }
Juan Cespedes1e583132009-04-07 18:17:11 +020085 if (WIFSTOPPED(status) && ((status>>16 == PTRACE_EVENT_FORK) || (status>>16 == PTRACE_EVENT_VFORK) || (status>>16 == PTRACE_EVENT_CLONE))) {
86 unsigned long data;
87 ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data);
88 event.thing = EVENT_FORK;
89 event.e_un.newpid = data;
90 return &event;
91 }
92 /* TODO: check for EVENT_CLONE */
93 if (WIFSTOPPED(status) && (status>>16 == PTRACE_EVENT_EXEC)) {
94 event.thing = EVENT_EXEC;
95 return &event;
96 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010097 if (WIFEXITED(status)) {
Juan Cespedes138d41c2009-04-07 00:49:12 +020098 event.thing = 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 Cespedes138d41c2009-04-07 00:49:12 +0200103 event.thing = 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 Cespedes138d41c2009-04-07 00:49:12 +0200108 event.thing = 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 Cespedes138d41c2009-04-07 00:49:12 +0200134 event.thing = 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 Cespedes138d41c2009-04-07 00:49:12 +0200142 event.thing = 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 Cespedes138d41c2009-04-07 00:49:12 +0200157 event.thing = 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}