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>
36 #ifndef TEMP_FAILURE_RETRY
37 #define TEMP_FAILURE_RETRY(a) (a)
41 char *mi_error_from_gdb=NULL;
42 static char *gdb_exe=NULL;
43 static char *xterm_exe=NULL;
44 static char *gdb_start=NULL;
45 static char *gdb_conn=NULL;
46 static char *main_func=NULL;
47 static char disable_psym_search_workaround=0;
51 mi_h *h=(mi_h *)calloc(1,sizeof(mi_h));
54 mi_error=MI_OUT_OF_MEMORY;
57 h->to_gdb[0]=h->to_gdb[1]=h->from_gdb[0]=h->from_gdb[1]=-1;
62 int mi_check_running_pid(pid_t pid)
68 /* If waitpid returns the number of our child means it communicated
69 to as a termination status. */
70 if (waitpid(pid,&status,WNOHANG)==pid)
78 int mi_check_running(mi_h *h)
80 return !h->died && mi_check_running_pid(h->pid);
83 void mi_kill_child(pid_t pid)
87 if (mi_check_running_pid(pid))
91 waitpid(pid,&status,0);
95 void mi_free_h(mi_h **handle)
102 else if (h->to_gdb[1]>=0)
106 else if (h->from_gdb[0]>=0)
107 close(h->from_gdb[0]);
108 if (h->from_gdb[1]>=0)
109 close(h->from_gdb[1]);
110 if (mi_check_running(h))
111 {/* GDB is running! */
112 mi_kill_child(h->pid);
116 mi_free_output(h->po);
117 free(h->catched_console);
122 void mi_set_nonblk(int h)
125 flf=fcntl(h,F_GETFL,0);
126 flf=flf | O_NONBLOCK;
127 fcntl(h,F_SETFL,flf);
130 int mi_getline(mi_h *h)
134 while (read(h->from_gdb[0],&c,1)==1)
136 if (h->lread>=h->llen)
138 h->llen=h->lread+128;
139 h->line=(char *)realloc(h->line,h->llen);
160 char *get_cstr(mi_output *o)
162 if (!o->c || o->c->type!=t_const)
167 int mi_get_response(mi_h *h)
173 if (h->from_gdb_echo)
174 h->from_gdb_echo(h->line,h->from_gdb_echo_data);
175 if (strncmp(h->line,"(gdb)",5)==0)
176 {/* End of response. */
180 {/* Add to the response. */
182 int add=1, is_exit=0;
183 o=mi_parse_gdb_output(h->line);
187 /* Tunneled streams callbacks. */
188 if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_STREAM)
197 h->console(aux,h->console_data);
198 if (h->catch_console && aux)
201 if (!h->catch_console)
203 free(h->catched_console);
204 h->catched_console=strdup(aux);
209 /* This one seems to be useless. */
211 h->target(get_cstr(o),h->target_data);
215 h->log(get_cstr(o),h->log_data);
219 else if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_ASYNC)
222 h->async(o,h->async_data);
224 else if (o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_ERROR)
225 {/* Error from gdb, record it. */
226 mi_error=MI_FROM_GDB;
227 free(mi_error_from_gdb);
228 mi_error_from_gdb=NULL;
229 if (o->c && strcmp(o->c->var,"msg")==0 && o->c->type==t_const)
230 mi_error_from_gdb=strdup(o->c->v.cstr);
232 is_exit=(o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_EXIT);
233 /* Add to the list of responses. */
244 /* Exit RR means gdb exited, we won't get a new prompt ;-) */
252 mi_output *mi_retire_response(mi_h *h)
254 mi_output *ret=h->po;
259 mi_output *mi_get_response_blk(mi_h *h)
262 /* Sometimes gdb dies. */
263 if (!mi_check_running(h))
266 mi_error=MI_GDB_DIED;
274 That's a must. If we just keep trying to read and failing things
275 become really sloooowwww. Instead we try and if it fails we wait
276 until something is available.
277 TODO: Implement something with the time out, a callback to ask the
278 application is we have to wait or not could be a good thing.
281 struct timeval timeout;
284 r=mi_get_response(h);
286 return mi_retire_response(h);
289 FD_SET(h->from_gdb[0],&set);
290 timeout.tv_sec=h->time_out;
292 ret=TEMP_FAILURE_RETRY(select(FD_SETSIZE,&set,NULL,NULL,&timeout));
295 if (!mi_check_running(h))
298 mi_error=MI_GDB_DIED;
302 ret=h->time_out_cb(h->time_out_cb_data);
305 mi_error=MI_GDB_TIME_OUT;
312 r=mi_get_response(h);
314 return mi_retire_response(h);
324 void mi_send_commands(mi_h *h, const char *file)
329 //printf("File: %s\n",file);
337 if (fgets(b,PATH_MAX,f))
339 //printf("Send: %s\n",b);
341 mi_res_simple_done(h);
347 void mi_send_target_commands(mi_h *h)
349 mi_send_commands(h,gdb_conn);
352 /**[txh]********************************************************************
355 Connect to a local copy of gdb. Note that the mi_h structure is something
356 similar to a "FILE *" for stdio.
358 Return: A new mi_h structure or NULL on error.
360 ***************************************************************************/
362 mi_h *mi_connect_local()
365 const char *gdb=mi_get_gdb_exe();
367 /* Start without error. */
369 /* Verify we have a GDB binary. */
370 if (access(gdb,X_OK))
372 mi_error=MI_MISSING_GDB;
375 /* Alloc the handle structure. */
379 h->time_out=MI_DEFAULT_TIME_OUT;
380 /* Create the pipes to connect with the child. */
381 if (pipe(h->to_gdb) || pipe(h->from_gdb))
383 mi_error=MI_PIPE_CREATE;
387 mi_set_nonblk(h->to_gdb[1]);
388 mi_set_nonblk(h->from_gdb[0]);
389 /* Associate streams to the file handles. */
390 h->to=fdopen(h->to_gdb[1],"w");
391 h->from=fdopen(h->from_gdb[0],"r");
392 if (!h->to || !h->from)
394 mi_error=MI_PIPE_CREATE;
398 /* Create the child. */
401 {/* We are the child. */
403 /* Connect stdin/out to the pipes. */
404 dup2(h->to_gdb[0],STDIN_FILENO);
405 dup2(h->from_gdb[1],STDOUT_FILENO);
406 /* Pass the control to gdb. */
407 argv[0]=(char *)gdb; /* Is that OK? */
408 argv[1]="--interpreter=mi";
410 argv[3]=disable_psym_search_workaround ? 0 : "--readnow";
412 execvp(argv[0],argv);
413 /* We get here only if exec failed. */
416 /* We are the parent. */
423 if (!mi_check_running(h))
425 mi_error=MI_DEBUGGER_RUN;
429 /* Wait for the prompt. */
430 mi_get_response_blk(h);
431 /* Send the start-up commands */
432 mi_send_commands(h,gdb_start);
437 /**[txh]********************************************************************
440 Close connection. You should ask gdb to quit first @x{gmi_gdb_exit}.
442 ***************************************************************************/
444 void mi_disconnect(mi_h *h)
447 free(mi_error_from_gdb);
448 mi_error_from_gdb=NULL;
451 void mi_set_console_cb(mi_h *h, stream_cb cb, void *data)
454 h->console_data=data;
457 void mi_set_target_cb(mi_h *h, stream_cb cb, void *data)
463 void mi_set_log_cb(mi_h *h, stream_cb cb, void *data)
469 stream_cb mi_get_console_cb(mi_h *h, void **data)
472 *data=h->console_data;
476 stream_cb mi_get_target_cb(mi_h *h, void **data)
479 *data=h->target_data;
483 stream_cb mi_get_log_cb(mi_h *h, void **data)
490 void mi_set_async_cb(mi_h *h, async_cb cb, void *data)
496 async_cb mi_get_async_cb(mi_h *h, void **data)
503 void mi_set_to_gdb_cb(mi_h *h, stream_cb cb, void *data)
506 h->to_gdb_echo_data=data;
509 void mi_set_from_gdb_cb(mi_h *h, stream_cb cb, void *data)
512 h->from_gdb_echo_data=data;
515 stream_cb mi_get_to_gdb_cb(mi_h *h, void **data)
518 *data=h->to_gdb_echo_data;
519 return h->to_gdb_echo;
522 stream_cb mi_get_from_gdb_cb(mi_h *h, void **data)
525 *data=h->from_gdb_echo_data;
526 return h->from_gdb_echo;
529 void mi_set_time_out_cb(mi_h *h, tm_cb cb, void *data)
532 h->time_out_cb_data=data;
535 tm_cb mi_get_time_out_cb(mi_h *h, void **data)
538 *data=h->time_out_cb_data;
539 return h->time_out_cb;
542 void mi_set_time_out(mi_h *h, int to)
547 int mi_get_time_out(mi_h *h)
552 int mi_send(mi_h *h, const char *format, ...)
561 va_start(argptr,format);
562 ret=vasprintf(&str,format,argptr);
567 h->to_gdb_echo(str,h->to_gdb_echo_data);
573 void mi_clean_up_globals()
587 void mi_register_exit()
589 static int registered=0;
593 atexit(mi_clean_up_globals);
597 void mi_set_gdb_exe(const char *name)
600 gdb_exe=name ? strdup(name) : NULL;
604 void mi_set_gdb_start(const char *name)
607 gdb_start=name ? strdup(name) : NULL;
611 void mi_set_gdb_conn(const char *name)
614 gdb_conn=name ? strdup(name) : NULL;
619 char *mi_search_in_path(const char *file)
629 r=strtok(pt,PATH_SEPARATOR_STR);
635 if (stat(test,&st)==0 && S_ISREG(st.st_mode))
640 r=strtok(NULL,PATH_SEPARATOR_STR);
646 const char *mi_get_gdb_exe()
649 {/* Look for gdb in path */
650 gdb_exe=mi_search_in_path("gdb");
652 return "/usr/bin/gdb";
657 const char *mi_get_gdb_start()
662 const char *mi_get_gdb_conn()
667 void mi_set_xterm_exe(const char *name)
670 xterm_exe=name ? strdup(name) : NULL;
674 const char *mi_get_xterm_exe()
677 {/* Look for xterm in path */
678 xterm_exe=mi_search_in_path("xterm");
680 return "/usr/bin/X11/xterm";
685 void mi_set_main_func(const char *name)
688 main_func=name ? strdup(name) : NULL;
692 const char *mi_get_main_func()
699 /**[txh]********************************************************************
702 Opens a new xterm to be used by the child process to debug.
704 Return: A new mi_aux_term structure, you can use @x{gmi_end_aux_term} to
707 ***************************************************************************/
709 mi_aux_term *gmi_start_xterm()
711 char nsh[14]="/tmp/shXXXXXX";
712 char ntt[14]="/tmp/ttXXXXXX";
716 mi_aux_term *res=NULL;
721 /* Verify we have an X terminal. */
722 xterm=mi_get_xterm_exe();
723 if (access(xterm,X_OK))
725 mi_error=MI_MISSING_XTERM;
729 /* Create 2 temporals. */
733 mi_error=MI_CREATE_TEMPORAL;
741 mi_error=MI_CREATE_TEMPORAL;
745 /* Create the script. */
752 mi_error=MI_CREATE_TEMPORAL;
755 fprintf(f,"#!/bin/sh\n");
756 fprintf(f,"tty > %s\n",ntt);
757 fprintf(f,"rm %s\n",nsh);
758 fprintf(f,"sleep 365d\n");
761 /* Create the child. */
764 {/* We are the child. */
766 /* Pass the control to gdb. */
767 argv[0]=(char *)mi_get_xterm_exe(); /* Is that ok? */
772 execvp(argv[0],argv);
773 /* We get here only if exec failed. */
778 /* We are the parent. */
786 /* Wait until the shell is deleted. */
787 while (stat(nsh,&st)==0)
789 /* Try to read the tty name. */
793 if (fgets(buf,PATH_MAX,f))
795 char *s; /* Strip the \n. */
796 for (s=buf; *s && *s!='\n'; s++);
798 res=(mi_aux_term *)malloc(sizeof(mi_aux_term));
802 res->tty=strdup(buf);
811 void mi_free_aux_term(mi_aux_term *t)
819 /**[txh]********************************************************************
822 Closes the auxiliar terminal and releases the allocated memory.
824 ***************************************************************************/
826 void gmi_end_aux_term(mi_aux_term *t)
830 if (t->pid!=-1 && mi_check_running_pid(t->pid))
831 mi_kill_child(t->pid);
835 /**[txh]********************************************************************
838 Forces the MI version. Currently the library can't detect it so you must
839 force it manually. GDB 5.x implemented MI v1 and 6.x v2.
841 ***************************************************************************/
843 void mi_force_version(mi_h *h, unsigned vMajor, unsigned vMiddle,
846 h->version=MI_VERSION2U(vMajor,vMiddle,vMinor);
849 /**[txh]********************************************************************
852 Dis/Enables the @var{wa} workaround for a bug in gdb.
854 ***************************************************************************/
856 void mi_set_workaround(unsigned wa, int enable)
861 disable_psym_search_workaround=enable ? 0 : 1;
866 /**[txh]********************************************************************
869 Finds if the @var{wa} workaround for a bug in gdb is enabled.
871 Return: !=0 if enabled.
873 ***************************************************************************/
875 int mi_get_workaround(unsigned wa)
880 return disable_psym_search_workaround==0;