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);
163 char *get_cstr(mi_output *o)
165 if (!o->c || o->c->type!=t_const)
170 int mi_get_response(mi_h *h)
176 if (h->from_gdb_echo)
177 h->from_gdb_echo(h->line,h->from_gdb_echo_data);
178 if (strncmp(h->line,"(gdb)",5)==0)
179 {/* End of response. */
183 {/* Add to the response. */
185 int add=1, is_exit=0;
186 o=mi_parse_gdb_output(h->line);
190 /* Tunneled streams callbacks. */
191 if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_STREAM)
200 h->console(aux,h->console_data);
201 if (h->catch_console && aux)
204 if (!h->catch_console)
206 free(h->catched_console);
207 h->catched_console=strdup(aux);
212 /* This one seems to be useless. */
214 h->target(get_cstr(o),h->target_data);
218 h->log(get_cstr(o),h->log_data);
222 else if (o->type==MI_T_OUT_OF_BAND && o->stype==MI_ST_ASYNC)
225 h->async(o,h->async_data);
227 else if (o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_ERROR)
228 {/* Error from gdb, record it. */
229 mi_error=MI_FROM_GDB;
230 free(mi_error_from_gdb);
231 mi_error_from_gdb=NULL;
232 if (o->c && strcmp(o->c->var,"msg")==0 && o->c->type==t_const)
233 mi_error_from_gdb=strdup(o->c->v.cstr);
235 is_exit=(o->type==MI_T_RESULT_RECORD && o->tclass==MI_CL_EXIT);
236 /* Add to the list of responses. */
247 /* Exit RR means gdb exited, we won't get a new prompt ;-) */
255 mi_output *mi_retire_response(mi_h *h)
257 mi_output *ret=h->po;
262 mi_output *mi_get_response_blk(mi_h *h)
265 /* Sometimes gdb dies. */
266 if (!mi_check_running(h))
269 mi_error=MI_GDB_DIED;
277 That's a must. If we just keep trying to read and failing things
278 become really sloooowwww. Instead we try and if it fails we wait
279 until something is available.
280 TODO: Implement something with the time out, a callback to ask the
281 application is we have to wait or not could be a good thing.
284 struct timeval timeout;
287 r=mi_get_response(h);
289 return mi_retire_response(h);
292 FD_SET(h->from_gdb[0],&set);
293 timeout.tv_sec=h->time_out;
295 ret=TEMP_FAILURE_RETRY(select(FD_SETSIZE,&set,NULL,NULL,&timeout));
298 if (!mi_check_running(h))
301 mi_error=MI_GDB_DIED;
305 ret=h->time_out_cb(h->time_out_cb_data);
308 mi_error=MI_GDB_TIME_OUT;
315 r=mi_get_response(h);
317 return mi_retire_response(h);
327 void mi_send_commands(mi_h *h, const char *file)
332 //printf("File: %s\n",file);
340 if (fgets(b,PATH_MAX,f))
342 //printf("Send: %s\n",b);
344 mi_res_simple_done(h);
350 void mi_send_target_commands(mi_h *h)
352 mi_send_commands(h,gdb_conn);
355 /**[txh]********************************************************************
358 Connect to a local copy of gdb. Note that the mi_h structure is something
359 similar to a "FILE *" for stdio.
361 Return: A new mi_h structure or NULL on error.
363 ***************************************************************************/
365 mi_h *mi_connect_local()
368 const char *gdb=mi_get_gdb_exe();
370 /* Start without error. */
372 /* Verify we have a GDB binary. */
373 if (access(gdb,X_OK))
375 mi_error=MI_MISSING_GDB;
378 /* Alloc the handle structure. */
382 h->time_out=MI_DEFAULT_TIME_OUT;
383 /* Create the pipes to connect with the child. */
384 if (pipe(h->to_gdb) || pipe(h->from_gdb))
386 mi_error=MI_PIPE_CREATE;
390 mi_set_nonblk(h->to_gdb[1]);
391 mi_set_nonblk(h->from_gdb[0]);
392 /* Associate streams to the file handles. */
393 h->to=fdopen(h->to_gdb[1],"w");
394 h->from=fdopen(h->from_gdb[0],"r");
395 if (!h->to || !h->from)
397 mi_error=MI_PIPE_CREATE;
401 /* Create the child. */
404 {/* We are the child. */
406 /* Connect stdin/out to the pipes. */
407 dup2(h->to_gdb[0],STDIN_FILENO);
408 dup2(h->from_gdb[1],STDOUT_FILENO);
409 /* Pass the control to gdb. */
410 argv[0]=(char *)gdb; /* Is that OK? */
411 argv[1]="--interpreter=mi";
413 argv[3]=disable_psym_search_workaround ? 0 : "--readnow";
415 execvp(argv[0],argv);
416 /* We get here only if exec failed. */
419 /* We are the parent. */
426 if (!mi_check_running(h))
428 mi_error=MI_DEBUGGER_RUN;
432 /* Wait for the prompt. */
433 mi_get_response_blk(h);
434 /* Send the start-up commands */
435 mi_send_commands(h,gdb_start);
440 /**[txh]********************************************************************
443 Close connection. You should ask gdb to quit first @x{gmi_gdb_exit}.
445 ***************************************************************************/
447 void mi_disconnect(mi_h *h)
450 free(mi_error_from_gdb);
451 mi_error_from_gdb=NULL;
454 void mi_set_console_cb(mi_h *h, stream_cb cb, void *data)
457 h->console_data=data;
460 void mi_set_target_cb(mi_h *h, stream_cb cb, void *data)
466 void mi_set_log_cb(mi_h *h, stream_cb cb, void *data)
472 stream_cb mi_get_console_cb(mi_h *h, void **data)
475 *data=h->console_data;
479 stream_cb mi_get_target_cb(mi_h *h, void **data)
482 *data=h->target_data;
486 stream_cb mi_get_log_cb(mi_h *h, void **data)
493 void mi_set_async_cb(mi_h *h, async_cb cb, void *data)
499 async_cb mi_get_async_cb(mi_h *h, void **data)
506 void mi_set_to_gdb_cb(mi_h *h, stream_cb cb, void *data)
509 h->to_gdb_echo_data=data;
512 void mi_set_from_gdb_cb(mi_h *h, stream_cb cb, void *data)
515 h->from_gdb_echo_data=data;
518 stream_cb mi_get_to_gdb_cb(mi_h *h, void **data)
521 *data=h->to_gdb_echo_data;
522 return h->to_gdb_echo;
525 stream_cb mi_get_from_gdb_cb(mi_h *h, void **data)
528 *data=h->from_gdb_echo_data;
529 return h->from_gdb_echo;
532 void mi_set_time_out_cb(mi_h *h, tm_cb cb, void *data)
535 h->time_out_cb_data=data;
538 tm_cb mi_get_time_out_cb(mi_h *h, void **data)
541 *data=h->time_out_cb_data;
542 return h->time_out_cb;
545 void mi_set_time_out(mi_h *h, int to)
550 int mi_get_time_out(mi_h *h)
555 int mi_send(mi_h *h, const char *format, ...)
564 va_start(argptr,format);
565 ret=vasprintf(&str,format,argptr);
570 h->to_gdb_echo(str,h->to_gdb_echo_data);
576 void mi_clean_up_globals()
590 void mi_register_exit()
592 static int registered=0;
596 atexit(mi_clean_up_globals);
600 void mi_set_gdb_exe(const char *name)
603 gdb_exe=name ? strdup(name) : NULL;
607 void mi_set_gdb_start(const char *name)
610 gdb_start=name ? strdup(name) : NULL;
614 void mi_set_gdb_conn(const char *name)
617 gdb_conn=name ? strdup(name) : NULL;
622 char *mi_search_in_path(const char *file)
638 if (stat(test,&st)==0 && S_ISREG(st.st_mode))
649 const char *mi_get_gdb_exe()
652 {/* Look for gdb in path */
653 gdb_exe=mi_search_in_path("gdb");
655 return "/usr/bin/gdb";
660 const char *mi_get_gdb_start()
665 const char *mi_get_gdb_conn()
670 void mi_set_xterm_exe(const char *name)
673 xterm_exe=name ? strdup(name) : NULL;
677 const char *mi_get_xterm_exe()
680 {/* Look for xterm in path */
681 xterm_exe=mi_search_in_path("xterm");
683 return "/usr/bin/X11/xterm";
688 void mi_set_main_func(const char *name)
691 main_func=name ? strdup(name) : NULL;
695 const char *mi_get_main_func()
702 /**[txh]********************************************************************
705 Opens a new xterm to be used by the child process to debug.
707 Return: A new mi_aux_term structure, you can use @x{gmi_end_aux_term} to
710 ***************************************************************************/
712 mi_aux_term *gmi_start_xterm()
714 char nsh[14]="/tmp/shXXXXXX";
715 char ntt[14]="/tmp/ttXXXXXX";
719 mi_aux_term *res=NULL;
724 /* Verify we have an X terminal. */
725 xterm=mi_get_xterm_exe();
726 if (access(xterm,X_OK))
728 mi_error=MI_MISSING_XTERM;
732 /* Create 2 temporals. */
736 mi_error=MI_CREATE_TEMPORAL;
744 mi_error=MI_CREATE_TEMPORAL;
748 /* Create the script. */
755 mi_error=MI_CREATE_TEMPORAL;
758 fprintf(f,"#!/bin/sh\n");
759 fprintf(f,"tty > %s\n",ntt);
760 fprintf(f,"rm %s\n",nsh);
761 fprintf(f,"sleep 365d\n");
764 /* Create the child. */
767 {/* We are the child. */
769 /* Pass the control to gdb. */
770 argv[0]=(char *)mi_get_xterm_exe(); /* Is that ok? */
775 execvp(argv[0],argv);
776 /* We get here only if exec failed. */
781 /* We are the parent. */
789 /* Wait until the shell is deleted. */
790 while (stat(nsh,&st)==0)
792 /* Try to read the tty name. */
796 if (fgets(buf,PATH_MAX,f))
798 char *s; /* Strip the \n. */
799 for (s=buf; *s && *s!='\n'; s++);
801 res=(mi_aux_term *)malloc(sizeof(mi_aux_term));
805 res->tty=strdup(buf);
814 void mi_free_aux_term(mi_aux_term *t)
822 /**[txh]********************************************************************
825 Closes the auxiliar terminal and releases the allocated memory.
827 ***************************************************************************/
829 void gmi_end_aux_term(mi_aux_term *t)
833 if (t->pid!=-1 && mi_check_running_pid(t->pid))
834 mi_kill_child(t->pid);
838 /**[txh]********************************************************************
841 Forces the MI version. Currently the library can't detect it so you must
842 force it manually. GDB 5.x implemented MI v1 and 6.x v2.
844 ***************************************************************************/
846 void mi_force_version(mi_h *h, unsigned vMajor, unsigned vMiddle,
849 h->version=MI_VERSION2U(vMajor,vMiddle,vMinor);
852 /**[txh]********************************************************************
855 Dis/Enables the @var{wa} workaround for a bug in gdb.
857 ***************************************************************************/
859 void mi_set_workaround(unsigned wa, int enable)
864 disable_psym_search_workaround=enable ? 0 : 1;
869 /**[txh]********************************************************************
872 Finds if the @var{wa} workaround for a bug in gdb is enabled.
874 Return: !=0 if enabled.
876 ***************************************************************************/
878 int mi_get_workaround(unsigned wa)
883 return disable_psym_search_workaround==0;