blob: 84847a970b923d7e54bf6d8102839ec75ddeed69 [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
6#include <sys/types.h>
7#include <sys/wait.h>
8#include <errno.h>
9#include <signal.h>
10#include <string.h>
11
12#include "ltrace.h"
13#include "options.h"
14#include "output.h"
15
16static struct event event;
17
18/* This should also update `current_process' */
19
20static struct process * pid2proc(int pid);
21
22struct event * wait_for_something(void)
23{
24 pid_t pid;
25 int status;
26 int tmp;
27
Juan Cespedes28f60191998-04-12 00:04:39 +020028 if (!list_of_processes) {
29 if (opt_d) {
30 output_line(0, "No more children");
31 }
32 exit(0);
33 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010034 pid = wait(&status);
35 if (pid==-1) {
36 if (errno==ECHILD) {
37 if (opt_d) {
38 output_line(0, "No more children");
39 }
40 exit(0);
Juan Cespedes28f60191998-04-12 00:04:39 +020041 } else if (errno==EINTR) {
42 if (opt_d) {
43 output_line(0, "wait received EINTR ?");
44 }
45 event.thing = LT_EV_NONE;
46 return &event;
Juan Cespedes5e01f651998-03-08 22:31:44 +010047 }
48 perror("wait");
49 exit(1);
50 }
51 event.proc = pid2proc(pid);
52 if (!event.proc) {
53 fprintf(stderr, "signal from wrong pid %u ?!?\n", pid);
54 exit(1);
55 }
Juan Cespedes35d70631998-03-15 14:05:40 +010056 event.proc->instruction_pointer = NULL;
Juan Cespedesf0fdae91998-03-11 00:03:00 +010057 if (opt_d>2) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010058 output_line(0,"signal from pid %u", pid);
59 }
60 if (event.proc->breakpoints_enabled == -1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010061 enable_all_breakpoints(event.proc);
62 event.thing = LT_EV_NONE;
63 continue_process(event.proc->pid);
64 return &event;
65 }
Juan Cespedes35d70631998-03-15 14:05:40 +010066 if (opt_i) {
67 event.proc->instruction_pointer = get_instruction_pointer(pid);
Juan Cespedesf0fdae91998-03-11 00:03:00 +010068 }
Juan Cespedes35d70631998-03-15 14:05:40 +010069 switch(syscall_p(event.proc, status, &tmp)) {
70 case 1: event.thing = LT_EV_SYSCALL;
71 event.e_un.sysnum = tmp;
72 return &event;
73 case 2: event.thing = LT_EV_SYSRET;
74 event.e_un.sysnum = tmp;
75 return &event;
76 default:
Juan Cespedes5e01f651998-03-08 22:31:44 +010077 }
78 if (WIFEXITED(status)) {
79 event.thing = LT_EV_EXIT;
80 event.e_un.ret_val = WEXITSTATUS(status);
81 return &event;
82 }
83 if (WIFSIGNALED(status)) {
84 event.thing = LT_EV_EXIT_SIGNAL;
85 event.e_un.signum = WTERMSIG(status);
86 return &event;
87 }
88 if (!WIFSTOPPED(status)) {
89 event.thing = LT_EV_UNKNOWN;
90 return &event;
91 }
92 if (WSTOPSIG(status) != SIGTRAP) {
93 event.thing = LT_EV_SIGNAL;
94 event.e_un.signum = WSTOPSIG(status);
95 return &event;
96 }
97 event.thing = LT_EV_BREAKPOINT;
Juan Cespedes35d70631998-03-15 14:05:40 +010098 if (!event.proc->instruction_pointer) {
99 event.proc->instruction_pointer = get_instruction_pointer(pid);
100 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100101 event.e_un.brk_addr = event.proc->instruction_pointer - DECR_PC_AFTER_BREAK;
102 return &event;
103}
104
Juan Cespedesd44c6b81998-09-25 14:48:42 +0200105static struct process * pid2proc(pid_t pid)
Juan Cespedes5e01f651998-03-08 22:31:44 +0100106{
107 struct process * tmp;
108
109 tmp = list_of_processes;
110 while(tmp) {
111 if (pid == tmp->pid) {
112 return tmp;
113 }
114 tmp = tmp->next;
115 }
116 return NULL;
117}
118