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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if HAVE_STDIO_H
#include <stdio.h>
#endif /* HAVE_STDIO_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif /* HAVE_SIGNAL_H */
#if HAVE_ERRNO_H
#include <errno.h>
#endif /* HAVE_ERRNO_H */
#include "sys_util.h"
#include "fork_util.h"
#include "fs_util.h"
#include "pseudo.h"
#include "terminal.h"
struct pty_pair {
int masterfd;
int slavefd;
char slavename[SLAVE_SIZE];
};
pty_pair_ptr pty_pair_create(void)
{
int val;
static char local_slavename[SLAVE_SIZE];
pty_pair_ptr ptr = (pty_pair_ptr) cgdb_malloc(sizeof (struct pty_pair));
if (!ptr)
return NULL;
ptr->masterfd = -1;
ptr->slavefd = -1;
ptr->slavename[0] = 0;
val = pty_open(&(ptr->masterfd), &(ptr->slavefd), local_slavename,
SLAVE_SIZE, NULL, NULL);
if (val == -1) {
clog_error(CLOG_CGDB, "PTY open");
return NULL;
}
strncpy(ptr->slavename, local_slavename, SLAVE_SIZE);
return ptr;
}
int pty_pair_destroy(pty_pair_ptr pty_pair)
{
if (!pty_pair)
return -1;
cgdb_close(pty_pair->masterfd);
cgdb_close(pty_pair->slavefd);
if (pty_release(pty_pair->slavename) == -1) {
clog_error(CLOG_CGDB, "pty_release error");
return -1;
}
free(pty_pair);
return 0;
}
int pty_pair_get_masterfd(pty_pair_ptr pty_pair)
{
if (!pty_pair)
return -1;
return pty_pair->masterfd;
}
int pty_pair_get_slavefd(pty_pair_ptr pty_pair)
{
if (!pty_pair)
return -1;
return pty_pair->slavefd;
}
const char *pty_pair_get_slavename(pty_pair_ptr pty_pair)
{
if (!pty_pair)
return NULL;
return pty_pair->slavename;
}
int pty_free_process(int *masterfd, char *sname)
{
cgdb_close(*masterfd);
if (pty_release(sname) == -1) {
clog_error(CLOG_CGDB, "pty_release error");
return -1;
}
return 0;
}
/**
* Utility function that frees up memory.
*
* \param argc
* The number of items in argv
*
* \param argv
* free is called on each of these
*/
void free_memory(int argc, char *argv[])
{
int i;
/* Free the local list */
for (i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
/* free_memory: utility function that frees up memory.
*
* s: if NULL, left alone, otherwise pty_release is called on it
* fd: if -1, left alone, otherwise close is called on it
* argc: The number of items in argv
* argv: free is called on each of these
*
* Returns -1 if any call fails, otherwise 0
*/
static int pty_free_memory(char *s, int fd, int argc, char *argv[])
{
int error = 0, i;
if (s && pty_release(s) == -1) {
clog_error(CLOG_CGDB, "pty_release failed");
error = -1;
}
if (fd != -1 && close(fd) == -1) {
clog_error(CLOG_CGDB, "close failed");
error = -1;
}
/* Free the local list */
for (i = 0; i < argc; i++)
free(argv[i]);
free(argv);
return error;
}
int invoke_debugger(const char *path,
int argc, char *argv[], int gdb_win_rows, int gdb_win_cols,
int *in, int *out, const char *new_ui_tty)
{
pid_t pid;
const char *const GDB = "gdb";
const char *const NW = "--nw";
const char *const EX = "-ex";
char **local_argv;
int i, j = 0, extra = 5;
int malloc_size = argc + extra;
char slavename[64];
int masterfd;
struct winsize size, *winsize;
size.ws_row = gdb_win_rows;
size.ws_col = gdb_win_cols;
size.ws_xpixel = 0;
size.ws_ypixel = 0;
if (gdb_win_rows == 0 && gdb_win_cols == 0) {
winsize = 0;
} else {
winsize = &size;
}
/* Copy the argv into the local_argv, and NULL terminate it.
* sneak in the path name, the user did not type that */
local_argv = (char **) cgdb_malloc((malloc_size) * sizeof (char *));
if (path)
local_argv[j++] = cgdb_strdup(path);
else
local_argv[j++] = cgdb_strdup(GDB);
/* NOTE: These options have to come first, since if the user
* typed '--args' to GDB, everything at the end of the
* users options become parameters to the inferior.
*/
local_argv[j++] = cgdb_strdup(NW);
local_argv[j++] = cgdb_strdup(EX);
local_argv[j++] = cgdb_strdup(std::string("new-ui mi ").
append(new_ui_tty).c_str());
/* copy in all the data the user entered */
for (i = 0; i < argc; i++)
local_argv[j++] = cgdb_strdup(argv[i]);
local_argv[j] = NULL;
if (fs_util_file_exists_in_path(local_argv[0]) == -1) {
clog_error(CLOG_CGDB, "Debugger \"%s\" not found", local_argv[0]);
pty_free_memory(slavename, masterfd, argc, local_argv);
return -1;
}
/* Log the gdb invocation line */
clog_info(CLOG_GDBIO, "Invoking program:");
for (i = 0; i < j; i++) {
clog_info(CLOG_GDBIO, " argv[%d]=%s ", i, local_argv[i]);
}
/* Fork into two processes with a shared pty pipe */
pid = pty_fork(&masterfd, slavename, SLAVE_SIZE, NULL, winsize);
if (pid == -1) { /* error, free memory and return */
pty_free_memory(slavename, masterfd, argc, local_argv);
clog_error(CLOG_CGDB, "fork failed");
return -1;
} else if (pid == 0) { /* child */
execvp(local_argv[0], local_argv);
/* Will get here if exec failed. This will happen when the
* - "gdb" is not on the users path, or if
* - user specified a different program via the -d option and it was
* not able to be exec'd.
*/
exit(0);
}
*in = masterfd;
*out = masterfd;
free_memory(malloc_size, local_argv);
return pid;
}
|