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;
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;
318 void mi_send_commands(mi_h *h, const char *file)
323 //printf("File: %s\n",file);
331 if (fgets(b,PATH_MAX,f))
333 //printf("Send: %s\n",b);
334 mi_send (h, "%s", b);
335 mi_res_simple_done(h);
341 void mi_send_target_commands(mi_h *h)
343 mi_send_commands(h,gdb_conn);
346 /**[txh]********************************************************************
349 Connect to a local copy of gdb. Note that the mi_h structure is something
350 similar to a "FILE *" for stdio.
352 Return: A new mi_h structure or NULL on error.
354 ***************************************************************************/
356 mi_h *mi_connect_local()
359 const char *gdb=mi_get_gdb_exe();
361 /* Start without error. */
363 /* Verify we have a GDB binary. */
364 if (access(gdb,X_OK))
366 mi_error=MI_MISSING_GDB;
369 /* Alloc the handle structure. */
373 h->time_out=MI_DEFAULT_TIME_OUT;
374 /* Create the pipes to connect with the child. */
375 if (pipe(h->to_gdb) || pipe(h->from_gdb))
377 mi_error=MI_PIPE_CREATE;
381 mi_set_nonblk(h->to_gdb[1]);
382 mi_set_nonblk(h->from_gdb[0]);
383 /* Associate streams to the file handles. */
384 h->to=fdopen(h->to_gdb[1],"w");
385 h->from=fdopen(h->from_gdb[0],"r");
386 if (!h->to || !h->from)
388 mi_error=MI_PIPE_CREATE;
392 /* Create the child. */
395 {/* We are the child. */
397 /* Connect stdin/out to the pipes. */
398 dup2(h->to_gdb[0],STDIN_FILENO);
399 dup2(h->from_gdb[1],STDOUT_FILENO);
400 /* Pass the control to gdb. */
401 argv[0]=(char *)gdb; /* Is that OK? */
402 argv[1]="--interpreter=mi";
404 argv[3]=disable_psym_search_workaround ? 0 : "--readnow";
406 execvp(argv[0],argv);
407 /* We get here only if exec failed. */
410 /* We are the parent. */
417 if (!mi_check_running(h))
419 mi_error=MI_DEBUGGER_RUN;
423 /* Wait for the prompt. */
424 mi_get_response_blk(h);
425 /* Send the start-up commands */
426 mi_send_commands(h,gdb_start);
431 /**[txh]********************************************************************
434 Close connection. You should ask gdb to quit first gmi_gdb_exit.
436 ***************************************************************************/
438 void mi_disconnect(mi_h *h)
441 free(mi_error_from_gdb);
442 mi_error_from_gdb=NULL;
445 void mi_set_console_cb(mi_h *h, stream_cb cb, void *data)
448 h->console_data=data;
451 void mi_set_target_cb(mi_h *h, stream_cb cb, void *data)
457 void mi_set_log_cb(mi_h *h, stream_cb cb, void *data)
463 stream_cb mi_get_console_cb(mi_h *h, void **data)
466 *data=h->console_data;
470 stream_cb mi_get_target_cb(mi_h *h, void **data)
473 *data=h->target_data;
477 stream_cb mi_get_log_cb(mi_h *h, void **data)
484 void mi_set_async_cb(mi_h *h, async_cb cb, void *data)
490 async_cb mi_get_async_cb(mi_h *h, void **data)
497 void mi_set_to_gdb_cb(mi_h *h, stream_cb cb, void *data)
500 h->to_gdb_echo_data=data;
503 void mi_set_from_gdb_cb(mi_h *h, stream_cb cb, void *data)
506 h->from_gdb_echo_data=data;
509 stream_cb mi_get_to_gdb_cb(mi_h *h, void **data)
512 *data=h->to_gdb_echo_data;
513 return h->to_gdb_echo;
516 stream_cb mi_get_from_gdb_cb(mi_h *h, void **data)
519 *data=h->from_gdb_echo_data;
520 return h->from_gdb_echo;
523 void mi_set_time_out_cb(mi_h *h, tm_cb cb, void *data)
526 h->time_out_cb_data=data;
529 tm_cb mi_get_time_out_cb(mi_h *h, void **data)
532 *data=h->time_out_cb_data;
533 return h->time_out_cb;
536 void mi_set_time_out(mi_h *h, int to)
541 int mi_get_time_out(mi_h *h)
546 int mi_send(mi_h *h, const char *format, ...)
555 va_start(argptr,format);
556 ret=vasprintf(&str,format,argptr);
561 h->to_gdb_echo(str,h->to_gdb_echo_data);
567 void mi_clean_up_globals()
581 void mi_register_exit()
583 static int registered=0;
587 atexit(mi_clean_up_globals);
591 void mi_set_gdb_exe(const char *name)
594 gdb_exe=name ? strdup(name) : NULL;
598 void mi_set_gdb_start(const char *name)
601 gdb_start=name ? strdup(name) : NULL;
605 void mi_set_gdb_conn(const char *name)
608 gdb_conn=name ? strdup(name) : NULL;
613 char *mi_search_in_path(const char *file)
623 r=strtok(pt,PATH_SEPARATOR_STR);
629 if (stat(test,&st)==0 && S_ISREG(st.st_mode))
634 r=strtok(NULL,PATH_SEPARATOR_STR);
640 const char *mi_get_gdb_exe()
643 {/* Look for gdb in path */
644 gdb_exe=mi_search_in_path("gdb");
646 return "/usr/bin/gdb";
651 const char *mi_get_gdb_start()
656 const char *mi_get_gdb_conn()
661 void mi_set_xterm_exe(const char *name)
664 xterm_exe=name ? strdup(name) : NULL;
668 const char *mi_get_xterm_exe()
671 {/* Look for xterm in path */
672 xterm_exe=mi_search_in_path("xterm");
674 return "/usr/bin/X11/xterm";
679 void mi_set_main_func(const char *name)
682 main_func=name ? strdup(name) : NULL;
686 const char *mi_get_main_func()
693 /**[txh]********************************************************************
696 Opens a new xterm to be used by the child process to debug.
698 Return: A new mi_aux_term structure, you can use gmi_end_aux_term to
701 ***************************************************************************/
703 mi_aux_term *gmi_start_xterm()
705 char nsh[14]="/tmp/shXXXXXX";
706 char ntt[14]="/tmp/ttXXXXXX";
710 mi_aux_term *res=NULL;
715 /* Verify we have an X terminal. */
716 xterm=mi_get_xterm_exe();
717 if (access(xterm,X_OK))
719 mi_error=MI_MISSING_XTERM;
723 /* Create 2 temporals. */
727 mi_error=MI_CREATE_TEMPORAL;
735 mi_error=MI_CREATE_TEMPORAL;
739 /* Create the script. */
746 mi_error=MI_CREATE_TEMPORAL;
749 fprintf(f,"#!/bin/sh\n");
750 fprintf(f,"tty > %s\n",ntt);
751 fprintf(f,"rm %s\n",nsh);
752 fprintf(f,"sleep 365d\n");
755 /* Create the child. */
758 {/* We are the child. */
760 /* Pass the control to gdb. */
761 argv[0]=(char *)mi_get_xterm_exe(); /* Is that ok? */
766 execvp(argv[0],argv);
767 /* We get here only if exec failed. */
772 /* We are the parent. */
780 /* Wait until the shell is deleted. */
781 while (stat(nsh,&st)==0)
783 /* Try to read the tty name. */
787 if (fgets(buf,PATH_MAX,f))
789 char *s; /* Strip the \n. */
790 for (s=buf; *s && *s!='\n'; s++);
792 res=(mi_aux_term *)malloc(sizeof(mi_aux_term));
796 res->tty=strdup(buf);
805 void mi_free_aux_term(mi_aux_term *t)
813 /**[txh]********************************************************************
816 Closes the auxiliar terminal and releases the allocated memory.
818 ***************************************************************************/
820 void gmi_end_aux_term(mi_aux_term *t)
824 if (t->pid!=-1 && mi_check_running_pid(t->pid))
825 mi_kill_child(t->pid);
829 /**[txh]********************************************************************
832 Forces the MI version. Currently the library can't detect it so you must
833 force it manually. GDB 5.x implemented MI v1 and 6.x v2.
835 ***************************************************************************/
837 void mi_force_version(mi_h *h, unsigned vMajor, unsigned vMiddle,
840 h->version=MI_VERSION2U(vMajor,vMiddle,vMinor);
843 /**[txh]********************************************************************
846 Dis/Enables the workaround for a bug in gdb.
848 ***************************************************************************/
850 void mi_set_workaround(unsigned wa, int enable)
855 disable_psym_search_workaround=enable ? 0 : 1;
860 /**[txh]********************************************************************
863 Finds if the workaround for a bug in gdb is enabled.
865 Return: !=0 if enabled.
867 ***************************************************************************/
869 int mi_get_workaround(unsigned wa)
874 return disable_psym_search_workaround==0;