blob: 363782b9d989a9cbcf443f60b80263c0d63646ee [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 Cespedes5c3fe062004-06-14 18:08:37 +020051 get_arch_dep(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010052 event.proc->instruction_pointer = NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +010053 debug(3, "signal from pid %u", pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010054 if (event.proc->breakpoints_enabled == -1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010055 enable_all_breakpoints(event.proc);
56 event.thing = LT_EV_NONE;
57 continue_process(event.proc->pid);
58 return &event;
59 }
Juan Cespedes35d70631998-03-15 14:05:40 +010060 if (opt_i) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020061 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010062 }
Juan Cespedes35d70631998-03-15 14:05:40 +010063 switch(syscall_p(event.proc, status, &tmp)) {
64 case 1: event.thing = LT_EV_SYSCALL;
65 event.e_un.sysnum = tmp;
66 return &event;
67 case 2: event.thing = LT_EV_SYSRET;
68 event.e_un.sysnum = tmp;
69 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010070 }
71 if (WIFEXITED(status)) {
72 event.thing = LT_EV_EXIT;
73 event.e_un.ret_val = WEXITSTATUS(status);
74 return &event;
75 }
76 if (WIFSIGNALED(status)) {
77 event.thing = LT_EV_EXIT_SIGNAL;
78 event.e_un.signum = WTERMSIG(status);
79 return &event;
80 }
81 if (!WIFSTOPPED(status)) {
82 event.thing = LT_EV_UNKNOWN;
83 return &event;
84 }
85 if (WSTOPSIG(status) != SIGTRAP) {
86 event.thing = LT_EV_SIGNAL;
87 event.e_un.signum = WSTOPSIG(status);
88 return &event;
89 }
90 event.thing = LT_EV_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +010091 if (!event.proc->instruction_pointer) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020092 event.proc->instruction_pointer = get_instruction_pointer(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010093 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010094 event.e_un.brk_addr = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
95 return &event;
96}
97
Juan Cespedes21c63a12001-07-07 20:56:56 +020098static struct process *
99pid2proc(pid_t pid) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100100 struct process * tmp;
101
102 tmp = list_of_processes;
103 while(tmp) {
104 if (pid == tmp->pid) {
105 return tmp;
106 }
107 tmp = tmp->next;
108 }
109 return NULL;
110}