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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#if HAVE_STDIO_H
#include <stdio.h>
#endif /* HAVE_STDIO_H */
#if HAVE_STDARG_H
#include <stdarg.h>
#endif /* HAVE_STDARG_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
/* Library includes */
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if HAVE_ERRNO_H
#include <errno.h>
#endif /* HAVE_ERRNO_H */
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
/* Local includes */
#include "tgdb.h"
#include "io.h"
#include "terminal.h"
#include "fork_util.h"
#include "cgdb_clog.h"
#define MAXLINE 4096
struct tgdb *tgdb;
/* Readline interface */
static int gdb_quit = 0;
/* Original terminal attributes */
static struct termios term_attributes;
static void start_logging()
{
/* Open our cgdb and tgdb io logfiles */
clog_open(CLOG_CGDB_ID, "%s/cgdb_log%d.txt", ".");
clog_open(CLOG_GDBIO_ID, "%s/cgdb_gdb_console_io_log%d.txt", ".");
clog_open(CLOG_GDBMIIO_ID, "%s/cgdb_gdb_mi_io_log%d.txt", ".");
clog_set_level(CLOG_GDBMIIO_ID, CLOG_DEBUG);
clog_set_fmt(CLOG_GDBMIIO_ID, CGDB_CLOG_FORMAT);
/* Puts cgdb in a mode where it writes a debug log of everything
* that is read from gdb. That is basically the entire session.
* This info is useful in determining what is going on under tgdb
* since the gui is good at hiding that info from the user.
*
* Change level to CLOG_ERROR to write only error messages.
* clog_set_level(CLOG_GDBIO, CLOG_ERROR);
*/
clog_set_level(CLOG_GDBIO_ID, CLOG_DEBUG);
clog_set_fmt(CLOG_GDBIO_ID, CGDB_CLOG_FORMAT);
/* General cgdb logging. Only logging warnings and debug messages
by default. */
clog_set_level(CLOG_CGDB_ID, CLOG_DEBUG);
clog_set_fmt(CLOG_CGDB_ID, CGDB_CLOG_FORMAT);
}
static void signal_handler(int signo)
{
if (signo == SIGINT || signo == SIGTERM || signo == SIGQUIT)
tgdb_signal_notification(tgdb, signo);
}
/* Sets up the signal handler for SIGWINCH
* Returns -1 on error. Or 0 on success */
static int set_up_signal(void)
{
struct sigaction action;
action.sa_handler = signal_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGINT, &action, NULL) < 0) {
clog_error(CLOG_CGDB, "sigaction failed ");
return -1;
}
if (sigaction(SIGTERM, &action, NULL) < 0) {
clog_error(CLOG_CGDB, "sigaction failed ");
return -1;
}
if (sigaction(SIGQUIT, &action, NULL) < 0) {
clog_error(CLOG_CGDB, "sigaction failed ");
return -1;
}
return 0;
}
static int gdb_input(int fd)
{
int result = tgdb_process(tgdb, fd);
if (result == -1) {
clog_error(CLOG_CGDB, "file descriptor closed");
return -1;
}
return 0;
}
static int stdin_input()
{
char buf[MAXLINE];
size_t size;
size_t i;
size = read(STDIN_FILENO, buf, MAXLINE - 1);
if (size == -1) {
clog_error(CLOG_CGDB, "read error");
return -1;
}
buf[size] = 0;
/* Display GDB output
* The strlen check is here so that if_print does not get called
* when displaying the filedlg. If it does get called, then the
* gdb window gets displayed when the filedlg is up
*/
for (i = 0; i < size; ++i) {
tgdb_send_char(tgdb, buf[i]);
}
return 0;
}
int main_loop(int gdbfd, int mifd)
{
int max;
fd_set rfds;
int result;
while (!gdb_quit) {
max = (gdbfd > STDIN_FILENO) ? gdbfd : STDIN_FILENO;
max = (max > mifd) ? max : mifd;
/* Clear the set and
*
* READ FROM:
* stdin (user or gui)
* master (gdb's stdout)
* gui_stdout (gui's stdout sending new info)
*
*/
FD_ZERO(&rfds);
/* Let the terminal emulate the char's when TGDB is busy */
FD_SET(STDIN_FILENO, &rfds);
FD_SET(gdbfd, &rfds);
FD_SET(mifd, &rfds);
result = select(max + 1, &rfds, NULL, NULL, NULL);
/* if the signal interrupted system call keep going */
if (result == -1 && errno == EINTR)
continue;
else if (result == -1) /* on error ... must die -> stupid OS */
clog_error(CLOG_CGDB, "select failed");
/* stdin -> readline input */
if (FD_ISSET(STDIN_FILENO, &rfds)) {
stdin_input();
}
/* gdb's output -> stdout */
if (FD_ISSET(gdbfd, &rfds))
if (gdb_input(gdbfd) == -1)
return -1;
/* gdb's mi output -> tgdb */
if (FD_ISSET(mifd, &rfds)) {
if (gdb_input(mifd) == -1)
return -1;
}
}
return 0;
}
void console_output(void *context, const std::string &str) {
std::string::const_iterator iter = str.begin();
for (; iter != str.end(); ++iter) {
if (write(STDOUT_FILENO, &*iter, 1) != 1) {
clog_error(CLOG_CGDB, "could not write byte");
}
}
}
void command_response(void *context, struct tgdb_response *response)
{
if (response->header == TGDB_QUIT) {
fprintf(stderr, "%s:%d TGDB_QUIT\n", __FILE__, __LINE__);
gdb_quit = 1;
}
}
tgdb_callbacks callbacks = {
NULL,
console_output,
command_response
};
int main(int argc, char **argv)
{
int gdb_console_fd, gdb_mi_fd;
#if 0
int c;
read(0, &c, 1);
#endif
start_logging();
/* Set all clog levels to debug */
clog_set_level(CLOG_CGDB_ID, CLOG_DEBUG);
clog_set_level(CLOG_GDBIO_ID, CLOG_DEBUG);
clog_set_level(CLOG_GDBMIIO_ID, CLOG_DEBUG);
if (tty_cbreak(STDIN_FILENO, &term_attributes) == -1)
clog_error(CLOG_CGDB, "tty_cbreak error");
if ((tgdb = tgdb_initialize(callbacks)) == NULL) {
clog_error(CLOG_CGDB, "tgdb_start error");
goto driver_end;
}
if (tgdb_start_gdb(tgdb, NULL, argc - 1, argv + 1, 0, 0,
&gdb_console_fd, &gdb_mi_fd ) == -1) {
clog_error(CLOG_CGDB, "tgdb_start error");
goto driver_end;
}
set_up_signal();
main_loop(gdb_console_fd, gdb_mi_fd);
if (tgdb_shutdown(tgdb) == -1)
clog_error(CLOG_CGDB, "could not shutdown");
driver_end:
if (tty_set_attributes(STDIN_FILENO, &term_attributes) == -1)
clog_error(CLOG_CGDB, "tty_reset error");
tgdb_close_logfiles();
return 0;
}
|