1 /**[txh]********************************************************************
3 Copyright (c) 2004-2009 by Salvador E. Tropea.
4 Covered by the GPL license.
8 This module handles the dialog with gdb, including starting and stopping
11 GDB Bug workaround for "file -readnow": I tried to workaround a bug using
12 it but looks like this option also have bugs!!!! so I have to use the
13 command line option --readnow.
14 It also have a bug!!!! when the binary is changed and gdb must reload it
15 this option is ignored. So it looks like we have no solution but 3 gdb bugs
18 ***************************************************************************/
21 #include <sys/types.h>
40 #ifndef TEMP_FAILURE_RETRY
41 #define TEMP_FAILURE_RETRY(a) (a)
45 char *mi_error_from_gdb=NULL;
46 static char *gdb_exe=NULL;
47 static char *xterm_exe=NULL;
48 static char *gdb_start=NULL;
49 static char *gdb_conn=NULL;
50 static char *main_func=NULL;
51 static char disable_psym_search_workaround=0;
55 mi_h *h=(mi_h *)calloc(1,sizeof(mi_h));
58 mi_error=MI_OUT_OF_MEMORY;
61 h->to_gdb[0]=h->to_gdb[1]=h->from_gdb[0]=h->from_gdb[1]=-1;
66 int mi_check_running_pid(pid_t pid)
72 /* If waitpid returns the number of our child means it communicated
73 to as a termination status. */
74 if (waitpid(pid,&status,WNOHANG)==pid)
82 int mi_check_running(mi_h *h)
84 return !h->died && mi_check_running_pid(h->pid);
87 void mi_kill_child(pid_t pid)
91 if (mi_check_running_pid(pid))
95 waitpid(pid,&status,0);
99 void mi_free_h(mi_h **handle)
106 else if (h->to_gdb[1]>=0)
110 else if (h->from_gdb[0]>=0)
111 close(h->from_gdb[0]);
112 if (h->from_gdb[1]>=0)
113 close(h->from_gdb[1]);
114 if (mi_check_running(h))
115 {/* GDB is running! */
116 mi_kill_child(h->pid);
120 mi_free_output(h->po);
121 free(h->catched_console);
126 void mi_set_nonblk(int h)
129 flf=fcntl(h,F_GETFL,0);
130 flf=flf | O_NONBLOCK;
131 fcntl(h,F_SETFL,flf);
134 int mi_getline(mi_h *h)
138 while (read(h->from_gdb[0],&c,1)==1)
140 if (h->lread>=h->llen)
142 h->llen=h->lread+128;
143 h->line=(char *)realloc(h->line,h->llen);
164 char *get_cstr(mi_output *o)
166 if (!o->c || o->c->type!=t_const)
171 int mi_get_response(mi_h *h)
177 if (h->from_gdb_echo)
178 h->from_gdb_echo(h->line,h->from_gdb_echo_data);
179 if (strncmp(h->line,"(gdb)",5)==0)
180 {/* End of response. */
184 {/* Add to the response. */
186 int add=1, is_exit=0;
187 o=mi_parse_gdb_output(h->line);
191 /* Tunneled streams callbacks. */
192 if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_STREAM)
201 h->console(aux,h->console_data);
202 if (h->catch_console && aux)
205 if (!h->catch_console)
207 free(h->catched_console);
208 h->catched_console=strdup(aux);
213 /* This one seems to be useless. */
215 h->target(get_cstr(o),h->target_data);
219 h->log(get_cstr(o),h->log_data);
223 else if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_ASYNC)
226 h->async(o,h->async_data);
228 else if (o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_ERROR)
229 {/* Error from gdb, record it. */
230 mi_error=MI_FROM_GDB;
231 free(mi_error_from_gdb);
232 mi_error_from_gdb=NULL;
233 if (o->c && strcmp(o->c->var,"msg")==0 && o->c->type==t_const)
234 mi_error_from_gdb=strdup(o->c->v.cstr);
236 is_exit=(o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_EXIT);
237 /* Add to the list of responses. */
248 /* Exit RR means gdb exited, we won't get a new prompt ;-) */
256 mi_output *mi_retire_response(mi_h *h)
258 mi_output *ret=h->po;
263 mi_output *mi_get_response_blk(mi_h *h)
266 /* Sometimes gdb dies. */
267 if (!mi_check_running(h))
270 mi_error=MI_GDB_DIED;
278 That's a must. If we just keep trying to read and failing things
279 become really sloooowwww. Instead we try and if it fails we wait
280 until something is available.
281 TODO: Implement something with the time out, a callback to ask the
282 application is we have to wait or not could be a good thing.
285 struct timeval timeout;
288 r=mi_get_response(h);
290 return mi_retire_response(h);
293 FD_SET(h->from_gdb[0],&set);
294 timeout.tv_sec=h->time_out;
296 ret=TEMP_FAILURE_RETRY(select(FD_SETSIZE,&set,NULL,NULL,&timeout));
299 if (!mi_check_running(h))
302 mi_error=MI_GDB_DIED;
306 ret=h->time_out_cb(h->time_out_cb_data);
309 mi_error=MI_GDB_TIME_OUT;
316 r=mi_get_response(h);
318 return mi_retire_response(h);
328 void mi_send_commands(mi_h *h, const char *file)
333 //printf("File: %s\n",file);
341 if (fgets(b,PATH_MAX,f))
343 //printf("Send: %s\n",b);
345 mi_res_simple_done(h);
351 void mi_send_target_commands(mi_h *h)
353 mi_send_commands(h,gdb_conn);
356 /**[txh]********************************************************************
359 Connect to a local copy of gdb. Note that the mi_h structure is something
360 similar to a "FILE *" for stdio.
362 Return: A new mi_h structure or NULL on error.
364 ***************************************************************************/
366 mi_h *mi_connect_local()
369 const char *gdb=mi_get_gdb_exe();
371 /* Start without error. */
373 /* Verify we have a GDB binary. */
374 if (access(gdb,X_OK))
376 mi_error=MI_MISSING_GDB;
379 /* Alloc the handle structure. */
383 h->time_out=MI_DEFAULT_TIME_OUT;
384 /* Create the pipes to connect with the child. */
385 if (pipe(h->to_gdb) || pipe(h->from_gdb))
387 mi_error=MI_PIPE_CREATE;
391 mi_set_nonblk(h->to_gdb[1]);
392 mi_set_nonblk(h->from_gdb[0]);
393 /* Associate streams to the file handles. */
394 h->to=fdopen(h->to_gdb[1],"w");
395 h->from=fdopen(h->from_gdb[0],"r");
396 if (!h->to || !h->from)
398 mi_error=MI_PIPE_CREATE;
402 /* Create the child. */
405 {/* We are the child. */
407 /* Connect stdin/out to the pipes. */
408 dup2(h->to_gdb[0],STDIN_FILENO);
409 dup2(h->from_gdb[1],STDOUT_FILENO);
410 /* Pass the control to gdb. */
411 argv[0]=(char *)gdb; /* Is that OK? */
412 argv[1]="--interpreter=mi";
414 argv[3]=disable_psym_search_workaround ? 0 : "--readnow";
416 execvp(argv[0],argv);
417 /* We get here only if exec failed. */
420 /* We are the parent. */
427 if (!mi_check_running(h))
429 mi_error=MI_DEBUGGER_RUN;
433 /* Wait for the prompt. */
434 mi_get_response_blk(h);
435 /* Send the start-up commands */
436 mi_send_commands(h,gdb_start);
441 /**[txh]********************************************************************
444 Close connection. You should ask gdb to quit first @x{gmi_gdb_exit}.
446 ***************************************************************************/
448 void mi_disconnect(mi_h *h)
451 free(mi_error_from_gdb);
452 mi_error_from_gdb=NULL;
455 void mi_set_console_cb(mi_h *h, stream_cb cb, void *data)
458 h->console_data=data;
461 void mi_set_target_cb(mi_h *h, stream_cb cb, void *data)
467 void mi_set_log_cb(mi_h *h, stream_cb cb, void *data)
473 stream_cb mi_get_console_cb(mi_h *h, void **data)
476 *data=h->console_data;
480 stream_cb mi_get_target_cb(mi_h *h, void **data)
483 *data=h->target_data;
487 stream_cb mi_get_log_cb(mi_h *h, void **data)
494 void mi_set_async_cb(mi_h *h, async_cb cb, void *data)
500 async_cb mi_get_async_cb(mi_h *h, void **data)
507 void mi_set_to_gdb_cb(mi_h *h, stream_cb cb, void *data)
510 h->to_gdb_echo_data=data;
513 void mi_set_from_gdb_cb(mi_h *h, stream_cb cb, void *data)
516 h->from_gdb_echo_data=data;
519 stream_cb mi_get_to_gdb_cb(mi_h *h, void **data)
522 *data=h->to_gdb_echo_data;
523 return h->to_gdb_echo;
526 stream_cb mi_get_from_gdb_cb(mi_h *h, void **data)
529 *data=h->from_gdb_echo_data;
530 return h->from_gdb_echo;
533 void mi_set_time_out_cb(mi_h *h, tm_cb cb, void *data)
536 h->time_out_cb_data=data;
539 tm_cb mi_get_time_out_cb(mi_h *h, void **data)
542 *data=h->time_out_cb_data;
543 return h->time_out_cb;
546 void mi_set_time_out(mi_h *h, int to)
551 int mi_get_time_out(mi_h *h)
556 int mi_send(mi_h *h, const char *format, ...)
565 va_start(argptr,format);
566 ret=vasprintf(&str,format,argptr);
571 h->to_gdb_echo(str,h->to_gdb_echo_data);
577 void mi_clean_up_globals()
591 void mi_register_exit()
593 static int registered=0;
597 atexit(mi_clean_up_globals);
601 void mi_set_gdb_exe(const char *name)
604 gdb_exe=name ? strdup(name) : NULL;
608 void mi_set_gdb_start(const char *name)
611 gdb_start=name ? strdup(name) : NULL;
615 void mi_set_gdb_conn(const char *name)
618 gdb_conn=name ? strdup(name) : NULL;
623 char *mi_search_in_path(const char *file)
633 r=strtok(pt,PATH_SEPARATOR_STR);
639 if (stat(test,&st)==0 && S_ISREG(st.st_mode))
644 r=strtok(NULL,PATH_SEPARATOR_STR);
650 const char *mi_get_gdb_exe()
653 {/* Look for gdb in path */
654 gdb_exe=mi_search_in_path("gdb");
656 return "/usr/bin/gdb";
661 const char *mi_get_gdb_start()
666 const char *mi_get_gdb_conn()
671 void mi_set_xterm_exe(const char *name)
674 xterm_exe=name ? strdup(name) : NULL;
678 const char *mi_get_xterm_exe()
681 {/* Look for xterm in path */
682 xterm_exe=mi_search_in_path("xterm");
684 return "/usr/bin/X11/xterm";
689 void mi_set_main_func(const char *name)
692 main_func=name ? strdup(name) : NULL;
696 const char *mi_get_main_func()
703 /**[txh]********************************************************************
706 Opens a new xterm to be used by the child process to debug.
708 Return: A new mi_aux_term structure, you can use @x{gmi_end_aux_term} to
711 ***************************************************************************/
713 mi_aux_term *gmi_start_xterm()
715 char nsh[14]="/tmp/shXXXXXX";
716 char ntt[14]="/tmp/ttXXXXXX";
720 mi_aux_term *res=NULL;
725 /* Verify we have an X terminal. */
726 xterm=mi_get_xterm_exe();
727 if (access(xterm,X_OK))
729 mi_error=MI_MISSING_XTERM;
733 /* Create 2 temporals. */
737 mi_error=MI_CREATE_TEMPORAL;
745 mi_error=MI_CREATE_TEMPORAL;
749 /* Create the script. */
756 mi_error=MI_CREATE_TEMPORAL;
759 fprintf(f,"#!/bin/sh\n");
760 fprintf(f,"tty > %s\n",ntt);
761 fprintf(f,"rm %s\n",nsh);
762 fprintf(f,"sleep 365d\n");
765 /* Create the child. */
768 {/* We are the child. */
770 /* Pass the control to gdb. */
771 argv[0]=(char *)mi_get_xterm_exe(); /* Is that ok? */
776 execvp(argv[0],argv);
777 /* We get here only if exec failed. */
782 /* We are the parent. */
790 /* Wait until the shell is deleted. */
791 while (stat(nsh,&st)==0)
793 /* Try to read the tty name. */
797 if (fgets(buf,PATH_MAX,f))
799 char *s; /* Strip the \n. */
800 for (s=buf; *s && *s!='\n'; s++);
802 res=(mi_aux_term *)malloc(sizeof(mi_aux_term));
806 res->tty=strdup(buf);
815 void mi_free_aux_term(mi_aux_term *t)
823 /**[txh]********************************************************************
826 Closes the auxiliar terminal and releases the allocated memory.
828 ***************************************************************************/
830 void gmi_end_aux_term(mi_aux_term *t)
834 if (t->pid!=-1 && mi_check_running_pid(t->pid))
835 mi_kill_child(t->pid);
839 /**[txh]********************************************************************
842 Forces the MI version. Currently the library can't detect it so you must
843 force it manually. GDB 5.x implemented MI v1 and 6.x v2.
845 ***************************************************************************/
847 void mi_force_version(mi_h *h, unsigned vMajor, unsigned vMiddle,
850 h->version=MI_VERSION2U(vMajor,vMiddle,vMinor);
853 /**[txh]********************************************************************
856 Dis/Enables the @var{wa} workaround for a bug in gdb.
858 ***************************************************************************/
860 void mi_set_workaround(unsigned wa, int enable)
865 disable_psym_search_workaround=enable ? 0 : 1;
870 /**[txh]********************************************************************
873 Finds if the @var{wa} workaround for a bug in gdb is enabled.
875 Return: !=0 if enabled.
877 ***************************************************************************/
879 int mi_get_workaround(unsigned wa)
884 return disable_psym_search_workaround==0;