blob: aa1372f77f0927c09a945e4176eaf8d33fe9ebd9 [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>
12
13#include "ltrace.h"
14#include "options.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010015#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010016
17static struct event event;
18
19/* This should also update `current_process' */
20
21static struct process * pid2proc(int pid);
22
Juan Cespedes21c63a12001-07-07 20:56:56 +020023struct event *
24wait_for_something(void) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010025 pid_t pid;
26 int status;
27 int tmp;
28
Juan Cespedes28f60191998-04-12 00:04:39 +020029 if (!list_of_processes) {
Juan Cespedescac15c32003-01-31 18:58:58 +010030 debug(1, "No more children");
Juan Cespedes28f60191998-04-12 00:04:39 +020031 exit(0);
32 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010033 pid = wait(&status);
34 if (pid==-1) {
35 if (errno==ECHILD) {
Juan Cespedescac15c32003-01-31 18:58:58 +010036 debug(1, "No more children");
Juan Cespedes5e01f651998-03-08 22:31:44 +010037 exit(0);
Juan Cespedes28f60191998-04-12 00:04:39 +020038 } else if (errno==EINTR) {
Juan Cespedescac15c32003-01-31 18:58:58 +010039 debug(1, "wait received EINTR ?");
Juan Cespedes28f60191998-04-12 00:04:39 +020040 event.thing = LT_EV_NONE;
41 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010042 }
43 perror("wait");
44 exit(1);
45 }
46 event.proc = pid2proc(pid);
47 if (!event.proc) {
48 fprintf(stderr, "signal from wrong pid %u ?!?\n", pid);
49 exit(1);
50 }
Juan Cespedes35d70631998-03-15 14:05:40 +010051 event.proc->instruction_pointer = NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +010052 debug(3, "signal 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);
55 event.thing = LT_EV_NONE;
56 continue_process(event.proc->pid);
57 return &event;
58 }
Juan Cespedes35d70631998-03-15 14:05:40 +010059 if (opt_i) {
60 event.proc->instruction_pointer = get_instruction_pointer(pid);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010061 }
Juan Cespedes35d70631998-03-15 14:05:40 +010062 switch(syscall_p(event.proc, status, &tmp)) {
63 case 1: event.thing = LT_EV_SYSCALL;
64 event.e_un.sysnum = tmp;
65 return &event;
66 case 2: event.thing = LT_EV_SYSRET;
67 event.e_un.sysnum = tmp;
68 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010069 }
70 if (WIFEXITED(status)) {
71 event.thing = LT_EV_EXIT;
72 event.e_un.ret_val = WEXITSTATUS(status);
73 return &event;
74 }
75 if (WIFSIGNALED(status)) {
76 event.thing = LT_EV_EXIT_SIGNAL;
77 event.e_un.signum = WTERMSIG(status);
78 return &event;
79 }
80 if (!WIFSTOPPED(status)) {
81 event.thing = LT_EV_UNKNOWN;
82 return &event;
83 }
84 if (WSTOPSIG(status) != SIGTRAP) {
85 event.thing = LT_EV_SIGNAL;
86 event.e_un.signum = WSTOPSIG(status);
87 return &event;
88 }
89 event.thing = LT_EV_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +010090 if (!event.proc->instruction_pointer) {
91 event.proc->instruction_pointer = get_instruction_pointer(pid);
92 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010093 event.e_un.brk_addr = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
94 return &event;
95}
96
Juan Cespedes21c63a12001-07-07 20:56:56 +020097static struct process *
98pid2proc(pid_t pid) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010099 struct process * tmp;
100
101 tmp = list_of_processes;
102 while(tmp) {
103 if (pid == tmp->pid) {
104 return tmp;
105 }
106 tmp = tmp->next;
107 }
108 return NULL;
109}