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
12 GDB Bug workaround for "file -readnow": I tried to workaround a bug using
13 it but looks like this option also have bugs!!!! so I have to use the
14 command line option --readnow.
15 It also have a bug!!!! when the binary is changed and gdb must reload it
16 this option is ignored. So it looks like we have no solution but 3 gdb bugs
19 ***************************************************************************/
22 #include <sys/types.h>
41 #ifndef TEMP_FAILURE_RETRY
42 #define TEMP_FAILURE_RETRY(a) (a)
46 char *mi_error_from_gdb=NULL;
47 static char *gdb_exe=NULL;
48 static char *xterm_exe=NULL;
49 static char *gdb_start=NULL;
50 static char *gdb_conn=NULL;
51 static char *main_func=NULL;
52 static char disable_psym_search_workaround=0;
56 mi_h *h=(mi_h *)calloc(1,sizeof(mi_h));
59 mi_error=MI_OUT_OF_MEMORY;
62 h->to_gdb[0]=h->to_gdb[1]=h->from_gdb[0]=h->from_gdb[1]=-1;
67 int mi_check_running_pid(pid_t pid)
73 /* If waitpid returns the number of our child means it communicated
74 to as a termination status. */
75 if (waitpid(pid,&status,WNOHANG)==pid)
83 int mi_check_running(mi_h *h)
85 return !h->died && mi_check_running_pid(h->pid);
88 void mi_kill_child(pid_t pid)
92 if (mi_check_running_pid(pid))
96 waitpid(pid,&status,0);
100 void mi_free_h(mi_h **handle)
107 else if (h->to_gdb[1]>=0)
111 else if (h->from_gdb[0]>=0)
112 close(h->from_gdb[0]);
113 if (h->from_gdb[1]>=0)
114 close(h->from_gdb[1]);
115 if (mi_check_running(h))
116 {/* GDB is running! */
117 mi_kill_child(h->pid);
121 mi_free_output(h->po);
122 free(h->catched_console);
127 void mi_set_nonblk(int h)
130 flf=fcntl(h,F_GETFL,0);
131 flf=flf | O_NONBLOCK;
132 fcntl(h,F_SETFL,flf);
135 int mi_getline(mi_h *h)
139 while (read(h->from_gdb[0],&c,1)==1)
141 if (h->lread>=h->llen)
143 h->llen=h->lread+128;
144 h->line=(char *)realloc(h->line,h->llen);
165 char *get_cstr(mi_output *o)
167 if (!o->c || o->c->type!=t_const)
172 int mi_get_response(mi_h *h)
178 if (h->from_gdb_echo)
179 h->from_gdb_echo(h->line,h->from_gdb_echo_data);
180 if (strncmp(h->line,"(gdb)",5)==0)
181 {/* End of response. */
185 {/* Add to the response. */
187 int add=1, is_exit=0;
188 o=mi_parse_gdb_output(h->line);
192 /* Tunneled streams callbacks. */
193 if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_STREAM)
202 h->console(aux,h->console_data);
203 if (h->catch_console && aux)
206 if (!h->catch_console)
208 free(h->catched_console);
209 h->catched_console=strdup(aux);
214 /* This one seems to be useless. */
216 h->target(get_cstr(o),h->target_data);
220 h->log(get_cstr(o),h->log_data);
224 else if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_ASYNC)
227 h->async(o,h->async_data);
229 else if (o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_ERROR)
230 {/* Error from gdb, record it. */
231 mi_error=MI_FROM_GDB;
232 free(mi_error_from_gdb);
233 mi_error_from_gdb=NULL;
234 if (o->c && strcmp(o->c->var,"msg")==0 && o->c->type==t_const)
235 mi_error_from_gdb=strdup(o->c->v.cstr);
237 is_exit=(o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_EXIT);
238 /* Add to the list of responses. */
249 /* Exit RR means gdb exited, we won't get a new prompt ;-) */
257 mi_output *mi_retire_response(mi_h *h)
259 mi_output *ret=h->po;
264 mi_output *mi_get_response_blk(mi_h *h)
267 /* Sometimes gdb dies. */
268 if (!mi_check_running(h))
271 mi_error=MI_GDB_DIED;
279 That's a must. If we just keep trying to read and failing things
280 become really sloooowwww. Instead we try and if it fails we wait
281 until something is available.
282 TODO: Implement something with the time out, a callback to ask the
283 application is we have to wait or not could be a good thing.
286 struct timeval timeout;
289 r=mi_get_response(h);
291 return mi_retire_response(h);
294 FD_SET(h->from_gdb[0],&set);
295 timeout.tv_sec=h->time_out;
297 ret=TEMP_FAILURE_RETRY(select(FD_SETSIZE,&set,NULL,NULL,&timeout));
300 if (!mi_check_running(h))
303 mi_error=MI_GDB_DIED;
307 ret=h->time_out_cb(h->time_out_cb_data);
310 mi_error=MI_GDB_TIME_OUT;
317 r=mi_get_response(h);
319 return mi_retire_response(h);
329 void mi_send_commands(mi_h *h, const char *file)
334 //printf("File: %s\n",file);
342 if (fgets(b,PATH_MAX,f))
344 //printf("Send: %s\n",b);
346 mi_res_simple_done(h);
352 void mi_send_target_commands(mi_h *h)
354 mi_send_commands(h,gdb_conn);
357 /**[txh]********************************************************************
360 Connect to a local copy of gdb. Note that the mi_h structure is something
361 similar to a "FILE *" for stdio.
363 Return: A new mi_h structure or NULL on error.
365 ***************************************************************************/
367 mi_h *mi_connect_local()
370 const char *gdb=mi_get_gdb_exe();
372 /* Start without error. */
374 /* Verify we have a GDB binary. */
375 if (access(gdb,X_OK))
377 mi_error=MI_MISSING_GDB;
380 /* Alloc the handle structure. */
384 h->time_out=MI_DEFAULT_TIME_OUT;
385 /* Create the pipes to connect with the child. */
386 if (pipe(h->to_gdb) || pipe(h->from_gdb))
388 mi_error=MI_PIPE_CREATE;
392 mi_set_nonblk(h->to_gdb[1]);
393 mi_set_nonblk(h->from_gdb[0]);
394 /* Associate streams to the file handles. */
395 h->to=fdopen(h->to_gdb[1],"w");
396 h->from=fdopen(h->from_gdb[0],"r");
397 if (!h->to || !h->from)
399 mi_error=MI_PIPE_CREATE;
403 /* Create the child. */
406 {/* We are the child. */
408 /* Connect stdin/out to the pipes. */
409 dup2(h->to_gdb[0],STDIN_FILENO);
410 dup2(h->from_gdb[1],STDOUT_FILENO);
411 /* Pass the control to gdb. */
412 argv[0]=(char *)gdb; /* Is that OK? */
413 argv[1]="--interpreter=mi";
415 argv[3]=disable_psym_search_workaround ? 0 : "--readnow";
417 execvp(argv[0],argv);
418 /* We get here only if exec failed. */
421 /* We are the parent. */
428 if (!mi_check_running(h))
430 mi_error=MI_DEBUGGER_RUN;
434 /* Wait for the prompt. */
435 mi_get_response_blk(h);
436 /* Send the start-up commands */
437 mi_send_commands(h,gdb_start);
442 /**[txh]********************************************************************
445 Close connection. You should ask gdb to quit first gmi_gdb_exit.
447 ***************************************************************************/
449 void mi_disconnect(mi_h *h)
452 free(mi_error_from_gdb);
453 mi_error_from_gdb=NULL;
456 void mi_set_console_cb(mi_h *h, stream_cb cb, void *data)
459 h->console_data=data;
462 void mi_set_target_cb(mi_h *h, stream_cb cb, void *data)
468 void mi_set_log_cb(mi_h *h, stream_cb cb, void *data)
474 stream_cb mi_get_console_cb(mi_h *h, void **data)
477 *data=h->console_data;
481 stream_cb mi_get_target_cb(mi_h *h, void **data)
484 *data=h->target_data;
488 stream_cb mi_get_log_cb(mi_h *h, void **data)
495 void mi_set_async_cb(mi_h *h, async_cb cb, void *data)
501 async_cb mi_get_async_cb(mi_h *h, void **data)
508 void mi_set_to_gdb_cb(mi_h *h, stream_cb cb, void *data)
511 h->to_gdb_echo_data=data;
514 void mi_set_from_gdb_cb(mi_h *h, stream_cb cb, void *data)
517 h->from_gdb_echo_data=data;
520 stream_cb mi_get_to_gdb_cb(mi_h *h, void **data)
523 *data=h->to_gdb_echo_data;
524 return h->to_gdb_echo;
527 stream_cb mi_get_from_gdb_cb(mi_h *h, void **data)
530 *data=h->from_gdb_echo_data;
531 return h->from_gdb_echo;
534 void mi_set_time_out_cb(mi_h *h, tm_cb cb, void *data)
537 h->time_out_cb_data=data;
540 tm_cb mi_get_time_out_cb(mi_h *h, void **data)
543 *data=h->time_out_cb_data;
544 return h->time_out_cb;
547 void mi_set_time_out(mi_h *h, int to)
552 int mi_get_time_out(mi_h *h)
557 int mi_send(mi_h *h, const char *format, ...)
566 va_start(argptr,format);
567 ret=vasprintf(&str,format,argptr);
572 h->to_gdb_echo(str,h->to_gdb_echo_data);
578 void mi_clean_up_globals()
592 void mi_register_exit()
594 static int registered=0;
598 atexit(mi_clean_up_globals);
602 void mi_set_gdb_exe(const char *name)
605 gdb_exe=name ? strdup(name) : NULL;
609 void mi_set_gdb_start(const char *name)
612 gdb_start=name ? strdup(name) : NULL;
616 void mi_set_gdb_conn(const char *name)
619 gdb_conn=name ? strdup(name) : NULL;
624 char *mi_search_in_path(const char *file)
634 r=strtok(pt,PATH_SEPARATOR_STR);
640 if (stat(test,&st)==0 && S_ISREG(st.st_mode))
645 r=strtok(NULL,PATH_SEPARATOR_STR);
651 const char *mi_get_gdb_exe()
654 {/* Look for gdb in path */
655 gdb_exe=mi_search_in_path("gdb");
657 return "/usr/bin/gdb";
662 const char *mi_get_gdb_start()
667 const char *mi_get_gdb_conn()
672 void mi_set_xterm_exe(const char *name)
675 xterm_exe=name ? strdup(name) : NULL;
679 const char *mi_get_xterm_exe()
682 {/* Look for xterm in path */
683 xterm_exe=mi_search_in_path("xterm");
685 return "/usr/bin/X11/xterm";
690 void mi_set_main_func(const char *name)
693 main_func=name ? strdup(name) : NULL;
697 const char *mi_get_main_func()
704 /**[txh]********************************************************************
707 Opens a new xterm to be used by the child process to debug.
709 Return: A new mi_aux_term structure, you can use gmi_end_aux_term to
712 ***************************************************************************/
714 mi_aux_term *gmi_start_xterm()
716 char nsh[14]="/tmp/shXXXXXX";
717 char ntt[14]="/tmp/ttXXXXXX";
721 mi_aux_term *res=NULL;
726 /* Verify we have an X terminal. */
727 xterm=mi_get_xterm_exe();
728 if (access(xterm,X_OK))
730 mi_error=MI_MISSING_XTERM;
734 /* Create 2 temporals. */
738 mi_error=MI_CREATE_TEMPORAL;
746 mi_error=MI_CREATE_TEMPORAL;
750 /* Create the script. */
757 mi_error=MI_CREATE_TEMPORAL;
760 fprintf(f,"#!/bin/sh\n");
761 fprintf(f,"tty > %s\n",ntt);
762 fprintf(f,"rm %s\n",nsh);
763 fprintf(f,"sleep 365d\n");
766 /* Create the child. */
769 {/* We are the child. */
771 /* Pass the control to gdb. */
772 argv[0]=(char *)mi_get_xterm_exe(); /* Is that ok? */
777 execvp(argv[0],argv);
778 /* We get here only if exec failed. */
783 /* We are the parent. */
791 /* Wait until the shell is deleted. */
792 while (stat(nsh,&st)==0)
794 /* Try to read the tty name. */
798 if (fgets(buf,PATH_MAX,f))
800 char *s; /* Strip the \n. */
801 for (s=buf; *s && *s!='\n'; s++);
803 res=(mi_aux_term *)malloc(sizeof(mi_aux_term));
807 res->tty=strdup(buf);
816 void mi_free_aux_term(mi_aux_term *t)
824 /**[txh]********************************************************************
827 Closes the auxiliar terminal and releases the allocated memory.
829 ***************************************************************************/
831 void gmi_end_aux_term(mi_aux_term *t)
835 if (t->pid!=-1 && mi_check_running_pid(t->pid))
836 mi_kill_child(t->pid);
840 /**[txh]********************************************************************
843 Forces the MI version. Currently the library can't detect it so you must
844 force it manually. GDB 5.x implemented MI v1 and 6.x v2.
846 ***************************************************************************/
848 void mi_force_version(mi_h *h, unsigned vMajor, unsigned vMiddle,
851 h->version=MI_VERSION2U(vMajor,vMiddle,vMinor);
854 /**[txh]********************************************************************
857 Dis/Enables the workaround for a bug in gdb.
859 ***************************************************************************/
861 void mi_set_workaround(unsigned wa, int enable)
866 disable_psym_search_workaround=enable ? 0 : 1;
871 /**[txh]********************************************************************
874 Finds if the workaround for a bug in gdb is enabled.
876 Return: !=0 if enabled.
878 ***************************************************************************/
880 int mi_get_workaround(unsigned wa)
885 return disable_psym_search_workaround==0;