blob: e24ee954e5baa28be1b5e2712c108cf35808e16f [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
Ian Wienand3219f322006-02-16 06:00:00 +010021static struct process *pid2proc(int pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010022
Ian Wienand3219f322006-02-16 06:00:00 +010023struct event *wait_for_something(void)
24{
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);
Ian Wienand3219f322006-02-16 06:00:00 +010034 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);
Ian Wienand3219f322006-02-16 06:00:00 +010038 } 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) {
Ian Wienand3219f322006-02-16 06:00:00 +010061 event.proc->instruction_pointer =
62 get_instruction_pointer(event.proc);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010063 }
Ian Wienand3219f322006-02-16 06:00:00 +010064 switch (syscall_p(event.proc, status, &tmp)) {
65 case 1:
66 event.thing = LT_EV_SYSCALL;
67 event.e_un.sysnum = tmp;
68 return &event;
69 case 2:
70 event.thing = LT_EV_SYSRET;
71 event.e_un.sysnum = tmp;
72 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010073 }
74 if (WIFEXITED(status)) {
75 event.thing = LT_EV_EXIT;
76 event.e_un.ret_val = WEXITSTATUS(status);
77 return &event;
78 }
79 if (WIFSIGNALED(status)) {
80 event.thing = LT_EV_EXIT_SIGNAL;
81 event.e_un.signum = WTERMSIG(status);
82 return &event;
83 }
84 if (!WIFSTOPPED(status)) {
85 event.thing = LT_EV_UNKNOWN;
86 return &event;
87 }
88 if (WSTOPSIG(status) != SIGTRAP) {
89 event.thing = LT_EV_SIGNAL;
90 event.e_un.signum = WSTOPSIG(status);
91 return &event;
92 }
93 event.thing = LT_EV_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +010094 if (!event.proc->instruction_pointer) {
Ian Wienand3219f322006-02-16 06:00:00 +010095 event.proc->instruction_pointer =
96 get_instruction_pointer(event.proc);
Juan Cespedes35d70631998-03-15 14:05:40 +010097 }
Ian Wienand3219f322006-02-16 06:00:00 +010098 event.e_un.brk_addr =
99 event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100100 return &event;
101}
102
Ian Wienand3219f322006-02-16 06:00:00 +0100103static struct process *pid2proc(pid_t pid)
104{
105 struct process *tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100106
107 tmp = list_of_processes;
Ian Wienand3219f322006-02-16 06:00:00 +0100108 while (tmp) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100109 if (pid == tmp->pid) {
110 return tmp;
111 }
112 tmp = tmp->next;
113 }
114 return NULL;
115}