summaryrefslogtreecommitdiff
path: root/src/ps/stacktrace.c
blob: fdd2aa924341722cf7e54a3e2924d0868d28afc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * stacktrace.c - ps debugging additions
 *
 * Gnu debugger stack trace code provided by Peter Mattis
 * <petm@CSUA.Berkeley.EDU> on Thu, 2 Nov 1995
 * Modified for easy use by Albert Cahalan.
 *
 * Copyright © 2004-2023 Craig Small <csmall@dropbear.xyz>
 * Copyright © 2011-2016 Jim Warner <james.warner@comcast.net
 * Copyright © 1998-2004 Albert Cahalan
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "common.h"

#define INTERACTIVE 0
#define STACK_TRACE 1

char *stored_prog_name = "you forgot to set \"program\"";
static int stack_trace_done;

/***********/
static void debug_stop(char **args){
  execvp (args[0], args);
  perror ("exec failed");
  _exit (0);
}

/***********/
static void stack_trace_sigchld(int signum){
  (void)signum;
  stack_trace_done = 1;
}

/************/
static void stack_trace(char **args){
  pid_t pid;
  int in_fd[2];
  int out_fd[2];
  fd_set fdset;
  fd_set readset;
  struct timeval tv;
  int sel, index, state;
  char buffer[256];
  char c;

  stack_trace_done = 0;
  signal(SIGCHLD, stack_trace_sigchld);

  if((pipe (in_fd) == -1) || (pipe (out_fd) == -1)){
    perror ("could open pipe");
    _exit (0);
  }

  pid = fork ();
  if (pid == 0){
    close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
    close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
    close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
    execvp (args[0], args);      /* exec gdb */
    perror ("exec failed");
    _exit (0);
  } else {
    if(pid == (pid_t) -1){
      perror ("could not fork");
      _exit (0);
    }
  }

  FD_ZERO (&fdset);
  FD_SET (out_fd[0], &fdset);

  write (in_fd[1], "backtrace\n", 10);
  write (in_fd[1], "p x = 0\n", 8);
  write (in_fd[1], "quit\n", 5);

  index = 0;
  state = 0;

  for(;;){
    readset = fdset;
    tv.tv_sec = 1;
    tv.tv_usec = 0;

    sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
    if (sel == -1) break;

    if((sel > 0) && (FD_ISSET (out_fd[0], &readset))){
      if(read (out_fd[0], &c, 1)){
        switch(state){
        case 0:
          if(c == '#'){
            state = 1;
            index = 0;
            buffer[index++] = c;
          }
          break;
        case 1:
          buffer[index++] = c;
          if((c == '\n') || (c == '\r')){
            buffer[index] = 0;
            fprintf (stderr, "%s", buffer);
            state = 0;
            index = 0;
          }
          break;
        default:
          break;
        }
      }
    }
    else if(stack_trace_done) break;
  }

  close (in_fd[0]);
  close (in_fd[1]);
  close (out_fd[0]);
  close (out_fd[1]);
  _exit (0);
}

/************/
void debug(int method, char *prog_name){
  pid_t pid;
  char buf[16];
  char *args[4] = { "gdb", NULL, NULL, NULL };
  int x;

  snprintf (buf, sizeof(buf), "%d", getpid ());

  args[1] = prog_name;
  args[2] = buf;

  pid = fork ();
  if(pid == 0){
    switch (method){
    case INTERACTIVE:
      fprintf (stderr, "debug_stop\n");
      debug_stop(args);
      break;
    case STACK_TRACE:
      fprintf (stderr, "stack_trace\n");
      stack_trace(args);
      break;
    }
    _exit(0);
  } else if(pid == (pid_t) -1){
    perror ("could not fork");
    return;
  }

  x = 1;
  while(x);  /* wait for debugger? */
}

#ifdef DEBUG
/************/
static void stack_trace_sigsegv(int signum){
  (void)signum;
  debug(STACK_TRACE, stored_prog_name);
}

/************/
void init_stack_trace(char *prog_name){
  stored_prog_name = prog_name;
  signal(SIGSEGV, stack_trace_sigsegv);
}
#endif