1 /**[txh]********************************************************************
3 Copyright (c) 2004-2007 by Salvador E. Tropea.
4 Covered by the GPL license.
8 Implements a very simple (naive ;-) C++ wrapper.@p
10 ***************************************************************************/
16 /**[txh]********************************************************************
19 Initializes a debugger object. It starts in the "disconnected" state.
20 Use @x{::Connect} after it.
22 ***************************************************************************/
24 MIDebugger::MIDebugger()
30 targetEndian=enUnknown;
34 /**[txh]********************************************************************
37 This is the destructor for the class. It tries to change the state to
38 "disconnected" doing the needed actions.
40 ***************************************************************************/
42 MIDebugger::~MIDebugger()
48 // TODO: Some kind of time-out
56 state=target_specified;
58 if (state==target_specified)
65 // Here state==disconnected
68 /**[txh]********************************************************************
71 Connects to gdb. Currently only local connections are supported, that's
72 a gdb limitation. Call it when in "unconnected" state, on success it will
73 change to the "connected" state. After it you should call one of the
74 SelectTarget members. @x{::SelectTargetX11}, @x{::SelectTargetLinux} or
75 @x{::SelectTargetRemote}. To finish the connection use @x{::Disconnect}.
79 ***************************************************************************/
81 int MIDebugger::Connect(bool )
83 if (state==disconnected)
95 /**[txh]********************************************************************
98 Finishes the connection to gdb. Call when in "connected" state, on success
99 it will change to "disconnected" state. This function first tries to exit
100 from gdb and then close the connection. But if gdb fails to exit it will be
105 ***************************************************************************/
107 int MIDebugger::Disconnect()
109 if (state==connected)
119 /**[txh]********************************************************************
122 Protected member that implements @x{::SelectTargetX11} and
123 @x{::SelectTargetLinux}.
127 ***************************************************************************/
129 int MIDebugger::SelectTargetTTY(const char *exec, const char *args,
130 const char *auxtty, dMode m)
132 if (state!=connected)
135 targetEndian=enUnknown;
136 targetArch=arUnknown;
138 if (!gmi_set_exec(h,exec,args))
141 const char *tty_name;
145 aux_tty=m==dmLinux ? gmi_look_for_free_vt() : gmi_start_xterm();
148 tty_name=aux_tty->tty;
154 if (!gmi_target_terminal(h,tty_name))
158 if (!gmi_gdb_set(h,"new-console","on"))
162 state=target_specified;
167 /**[txh]********************************************************************
170 Starts a debug session for X11. It opens an xterm console for the program
171 to debug and tells gdb which executable to debug and the command line
172 options to pass. You can specify an already existing tty console to be used.
173 Can be called when the state is "connected". On success will change to the
174 "target_specified" state. After it you can use @x{::Run} or use the members
175 to define breakpoints and similar stuff. To finish it use
176 @x{::TargetUnselect}.
180 ***************************************************************************/
182 int MIDebugger::SelectTargetX11(const char *exec, const char *args,
185 return SelectTargetTTY(exec,args,auxtty,dmX11);
189 /**[txh]********************************************************************
192 Starts a debug session for Linux console. It selects an empty VT for the
193 program to debug and tells gdb which executable to debug and the command line
194 options to pass. You can specify an already existing tty console to be used.
195 Can be called when the state is "connected". On success will change to the
196 "target_specified" state. After it you can use @x{::Run} or use the members
197 to define breakpoints and similar stuff. To finish it use
198 @x{::TargetUnselect}.
202 ***************************************************************************/
204 int MIDebugger::SelectTargetLinux(const char *exec, const char *args,
207 return SelectTargetTTY(exec,args,auxtty,dmLinux);
210 /**[txh]********************************************************************
213 Starts a remote session. The other end should be running gdbserver. You
214 must specify a local copy of the program to debug with debug info. The remote
215 copy can be stripped. The @var{rtype} and @var{rparams} selects the protocol
216 and the remote machine. Read gdb docs to know more about the available
217 options. If @var{rtype} is omitted "extended-remote" protocol is used.
218 Can be called when the state is "connected". On success will change to the
219 "target_specified" state. After it you can use @x{::Run} or use the members
220 to define breakpoints and similar stuff. To finish it use
221 @x{::TargetUnselect}. Note that when gdb uses remote debugging the remote
222 program starts running. The @x{::Run} member knows about it.
226 o->SelectTargetRemote("./exec_file","192.168.1.65:5000");
228 ***************************************************************************/
230 int MIDebugger::SelectTargetRemote(const char *exec, const char *rparams,
231 const char *rtype, bool download)
233 if (state!=connected)
238 targetEndian=enUnknown;
239 targetArch=arUnknown;
241 rtype="extended-remote";
243 /* Tell gdb to load symbols from the local copy. */
244 int res=download ? gmi_set_exec(h,exec,NULL) : gmi_file_symbol_file(h,exec);
247 /* Select the target */
248 if (!gmi_target_select(h,rtype,rparams))
250 /* Download the binary */
253 if (!gmi_target_download(h))
257 state=target_specified;
261 /**[txh]********************************************************************
264 Starts a local session using an already running process.
268 ***************************************************************************/
270 mi_frames *MIDebugger::SelectTargetPID(const char *exec, int pid)
272 if (state!=connected)
277 targetEndian=enUnknown;
278 targetArch=arUnknown;
280 mi_frames *res=gmi_target_attach(h,pid);
285 /* Tell gdb to load symbols from the local copy. */
286 if (!gmi_file_symbol_file(h,exec))
296 /**[txh]********************************************************************
299 Used to unselect the current target. When X11 mode it closes the auxiliar
300 terminal. For remote debugging it uses "detach". Can be called when in
301 "target_specified" state. On success it changes to "connected" state.
305 ***************************************************************************/
307 int MIDebugger::TargetUnselect()
313 if (state!=target_specified)
317 gmi_end_aux_term(aux_tty);
323 if (state!=target_specified)
325 if (state!=stopped || !gmi_target_detach(h))
334 /**[txh]********************************************************************
337 Starts running the program. You should set breakpoint before it. Can be
338 called when state is "target_specified". On success will change to "running"
339 state. After it you should poll for async responses using @x{::Poll}. The
340 program can stop for many reasons asynchronously and also exit. This
341 information is known using Poll. You can stop the program using @x{::Stop}.
345 ***************************************************************************/
347 int MIDebugger::Run()
349 if (state!=target_specified)
354 res=gmi_exec_continue(h);
363 /**[txh]********************************************************************
366 Stops the program execution. GDB sends an interrupt signal to the program.
367 Can be called when the state is "running". It won't switch to "stopped"
368 state automatically. Instead you must poll for async events and wait for a
369 stopped notification. After it you can call @x{::Continue} to resume
375 ***************************************************************************/
377 int MIDebugger::Stop()
381 return gmi_exec_interrupt(h);
384 /**[txh]********************************************************************
387 Polls gdb looking for async responses. Currently it just looks for
388 "stopped" messages. You must call it when the state is "running". But the
389 function will poll gdb even if the state isn't "running". When a stopped
390 message is received the state changes to stopped or target_specified (the
391 last is when we get some exit).
393 Return: !=0 if we got a response. The @var{rs} pointer will point to an
394 mi_stop structure if we got it or will be NULL if we didn't.
396 ***************************************************************************/
398 int MIDebugger::Poll(mi_stop *&rs)
400 if (state==disconnected || !mi_get_response(h))
403 mi_stop *res=mi_res_stop(h);
406 if (res->reason==sr_exited_signalled ||
407 res->reason==sr_exited ||
408 res->reason==sr_exited_normally)
409 // When we use a PID the exit makes it invalid, so we don't have a
410 // valid target to re-run.
411 state=mode==dmPID ? connected : target_specified;
414 if (res->reason==sr_unknown && waitingTempBkpt)
417 res->reason=sr_bkpt_hit;
421 {// We got an error. It looks like most async commands returns running even
422 // before they are sure the process is running. Latter we get the real
423 // error. So I'm assuming the program is stopped.
424 // Lamentably -target-exec-status isn't implemented and even in this case
425 // if the program is really running as real async isn't implemented it
426 // will fail anyways.
434 /**[txh]********************************************************************
437 Resumes execution after the program "stopped". Can be called when the state
438 is stopped. On success will change to "running" state.
442 ***************************************************************************/
444 int MIDebugger::Continue()
448 int res=gmi_exec_continue(h);
454 /**[txh]********************************************************************
457 Starts program execution or resumes it. When the state is target_specified
458 it calls @x{::Run} otherwise it uses @x{::Continue}. Can be called when the
459 state is "target_specified" or "stopped". On success will change to
464 ***************************************************************************/
466 int MIDebugger::RunOrContinue()
468 if (state==target_specified)
473 /**[txh]********************************************************************
476 Kills the program you are debugging. Can be called when the state is
477 "stopped" or "running". On success changes the state to "target_specified".
478 Note that if you want to restart the program you can just call @x{::Run} and
479 if you want to just stop the program call @x{::Stop}.
483 ***************************************************************************/
485 int MIDebugger::Kill()
487 if (state!=stopped && state!=running)
489 /* GDB/MI doesn't implement it (yet), so we use the regular kill. */
490 /* Ensure confirm is off. */
491 char *prev=gmi_gdb_show(h,"confirm");
494 if (strcmp(prev,"off"))
496 if (!gmi_gdb_set(h,"confirm","off"))
508 int res=gmi_exec_kill(h);
509 /* Revert confirm option if needed. */
512 gmi_gdb_set(h,"confirm",prev);
517 state=target_specified;
522 /**[txh]********************************************************************
525 Inserts a breakpoint at @var{file} and @var{line}. Can be called when the
526 state is "stopped" or "target_specified".
528 Return: An mi_bkpt structure or NULL if error.
530 ***************************************************************************/
532 mi_bkpt *MIDebugger::Breakpoint(const char *file, int line)
534 if (state!=stopped && state!=target_specified)
536 return gmi_break_insert(h,file,line);
539 /**[txh]********************************************************************
542 Inserts a breakpoint at @var{where}, all options available. Can be called
543 when the state is "stopped" or "target_specified".
545 Return: An mi_bkpt structure or NULL if error.
547 ***************************************************************************/
549 mi_bkpt *MIDebugger::Breakpoint(const char *where, bool temporary,
550 const char *cond, int count, int thread,
553 if (state!=stopped && state!=target_specified)
555 return gmi_break_insert_full(h,temporary,hard_assist,cond,count,thread,where);
559 const int maxWhere=PATH_MAX+256;
561 mi_bkpt *MIDebugger::Breakpoint(mi_bkpt *b)
563 if (state!=stopped && state!=target_specified)
571 snprintf(buf,maxWhere,"%s:%d",b->file,b->line);
574 snprintf(buf,maxWhere,"%s",b->func);
576 case m_file_function:
577 snprintf(buf,maxWhere,"%s:%s",b->file,b->func);
580 snprintf(buf,maxWhere,"*%p",b->addr);
583 return Breakpoint(buf,b->disp==d_del,b->cond,b->ignore,b->thread,
587 /**[txh]********************************************************************
590 Inserts a breakpoint at @var{file} and @var{line} all options available.
591 Can be called when the state is "stopped" or "target_specified".
593 Return: An mi_bkpt structure or NULL if error.
595 ***************************************************************************/
597 mi_bkpt *MIDebugger::BreakpointFull(const char *file, int line,
598 bool temporary, const char *cond,
599 int count, int thread, bool hard_assist)
601 if (state!=stopped && state!=target_specified)
603 return gmi_break_insert_full_fl(h,file,line,temporary,hard_assist,cond,
607 /**[txh]********************************************************************
610 Removes the specified breakpoint. It doesn't free the structure. Can be
611 called when the state is "stopped" or "target_specified".
615 ***************************************************************************/
617 int MIDebugger::BreakDelete(mi_bkpt *b)
619 if ((state!=stopped && state!=target_specified) || !b)
621 return gmi_break_delete(h,b->number);
624 /**[txh]********************************************************************
627 Inserts a watchpoint for the specified expression. Can be called when the
628 state is "stopped" or "target_specified".
630 Return: An mi_wp structure or NULL if error.
632 ***************************************************************************/
634 mi_wp *MIDebugger::Watchpoint(enum mi_wp_mode mode, const char *exp)
636 if (state!=stopped && state!=target_specified)
638 return gmi_break_watch(h,mode,exp);
641 /**[txh]********************************************************************
644 Removes the specified watchpoint. It doesn't free the structure. Can be
645 called when the state is "stopped" or "target_specified".
649 ***************************************************************************/
651 int MIDebugger::WatchDelete(mi_wp *w)
653 if ((state!=stopped && state!=target_specified) || !w)
655 return gmi_break_delete(h,w->number);
658 /**[txh]********************************************************************
661 Puts a temporal breakpoint in main function and starts running. Can be
662 called when the state is "target_specified". If successful the state will
667 ***************************************************************************/
669 int MIDebugger::RunToMain()
671 if (state!=target_specified)
673 mi_bkpt *b=Breakpoint(mi_get_main_func(),true);
681 /**[txh]********************************************************************
684 Executes upto the next line, doesn't follow function calls. The @var{inst}
685 argument is for assembler. If the state is "target_specified" it will go to
686 the first line in the main function. If the state is "stopped" will use the
687 next command. If successfully the state will change to "running".
691 ***************************************************************************/
693 int MIDebugger::StepOver(bool inst)
697 if (state==target_specified)
698 {// We aren't running
705 res=gmi_exec_next_instruction(h);
707 res=gmi_exec_next(h);
714 /**[txh]********************************************************************
717 Executes until the specified point. If the state is "target_specified" it
718 uses a temporal breakpoint. If the state is "stopped" it uses -exec-until.
719 Fails for any other state.
723 ***************************************************************************/
725 int MIDebugger::GoTo(const char *file, int line)
729 if (state==target_specified)
730 {// We aren't running
731 // Use a temporal breakpoint
732 int l=strlen(file)+32;
734 snprintf(buf,l,"%s:%d",file,line);
735 mi_bkpt *b=Breakpoint(buf,true);
743 else if (state==stopped)
745 res=gmi_exec_until(h,file,line);
752 /**[txh]********************************************************************
755 Executes until the specified point. If the state is "target_specified" it
756 uses a temporal breakpoint. If the state is "stopped" it uses -exec-until.
757 Fails for any other state.
761 ***************************************************************************/
763 int MIDebugger::GoTo(void *addr)
767 if (state==target_specified)
768 {// We aren't running
769 // Use a temporal breakpoint
771 snprintf(buf,32,"*%p",addr);
772 mi_bkpt *b=Breakpoint(buf,true);
780 else if (state==stopped)
782 res=gmi_exec_until_addr(h,addr);
790 /**[txh]********************************************************************
793 Resumes execution until the end of the current funtion is reached. Only
794 usable when we are in the "stopped" state.
798 ***************************************************************************/
800 int MIDebugger::FinishFun()
804 int res=gmi_exec_finish(h);
810 /**[txh]********************************************************************
813 Returns immediately. Only usable when we are in the "stopped" state.
815 Return: !=NULL OK, the returned frame is the current location. That's a
816 synchronous function.
818 ***************************************************************************/
820 mi_frames *MIDebugger::ReturnNow()
824 return gmi_exec_return(h);
827 /**[txh]********************************************************************
830 Returns the current list of frames.
832 Return: !=NULL OK, the list of frames is returned.
834 ***************************************************************************/
836 mi_frames *MIDebugger::CallStack(bool args)
840 mi_frames *fr1=gmi_stack_list_frames(h);
842 {// Get the function arguments
843 mi_frames *fr2=gmi_stack_list_arguments(h,1);
845 {// Transfer them to the other list
846 mi_frames *p=fr1, *p2=fr2;
860 /**[txh]********************************************************************
863 Executes upto the next line, it follows function calls. The @var{inst}
864 argument is for assembler. If the state is "target_specified" it will go to
865 the first line in the main function. If the state is "stopped" will use the
866 next command. If successfully the state will change to "running".
870 ***************************************************************************/
872 int MIDebugger::TraceInto(bool inst)
876 if (state==target_specified)
877 {// We aren't running
884 res=gmi_exec_step_instruction(h);
886 res=gmi_exec_step(h);
893 /**[txh]********************************************************************
896 Evaluates the provided expression. If we get an error the error
897 description is returned instead. Can't be called if "disconnected" or
900 Return: The result of the expression (use free) or NULL.
902 ***************************************************************************/
904 char *MIDebugger::EvalExpression(const char *exp)
906 if (state==disconnected ||
907 state==running) // No async :-(
911 char *res=gmi_data_evaluate_expression(h,exp);
912 if (!res && mi_error_from_gdb)
913 {// Not valid, return the error
914 res=strdup(mi_error_from_gdb);
919 /**[txh]********************************************************************
922 Modifies the provided expression. If we get an error the error
923 description is returned instead. Can't be called if "disconnected" or
926 Return: The result of the expression (use free) or NULL.
928 ***************************************************************************/
930 char *MIDebugger::ModifyExpression(char *exp, char *newVal)
932 if (state==disconnected ||
933 state==running) // No async :-(
935 // Create an assignment
937 int l2=strlen(newVal);
938 char b[l1+l2+2], *s=b;
942 memcpy(++s,newVal,l2);
945 char *res=gmi_data_evaluate_expression(h,b);
946 if (!res && mi_error_from_gdb)
947 {// Not valid, return the error
948 res=strdup(mi_error_from_gdb);
953 /**[txh]********************************************************************
956 Sends a command to gdb.
960 ***************************************************************************/
962 int MIDebugger::Send(const char *command)
964 if (state==disconnected ||
965 state==running) // No async :-(
967 // TODO: detect and use -interpreter-exec?
968 mi_send(h,"%s\n",command);
969 return mi_res_simple_done(h);
973 /**[txh]********************************************************************
976 Fills the type and value fields of the mi_gvar provided list.
980 ***************************************************************************/
982 int MIDebugger::FillTypeVal(mi_gvar *var)
986 if (!var->type && !gmi_var_info_type(h,var))
988 if (!var->value && !gmi_var_evaluate_expression(h,var))
995 int MIDebugger::FillOneTypeVal(mi_gvar *var)
1001 if (!var->type && !gmi_var_info_type(h,var))
1003 var->type=strdup("");
1006 if (!var->value && !gmi_var_evaluate_expression(h,var))
1008 var->value=strdup("");
1014 int MIDebugger::AssigngVar(mi_gvar *var, const char *exp)
1018 return gmi_var_assign(h,var,exp);
1021 char *MIDebugger::Show(const char *var)
1023 if (state==running || state==disconnected)
1025 // GDB 5.x doesn't reply all in the response record, just to the console :-(
1027 if (h->catched_console)
1029 free(h->catched_console);
1030 h->catched_console=NULL;
1032 char *res=gmi_gdb_show(h,var);
1034 if (!res && h->catched_console)
1036 res=h->catched_console;
1037 h->catched_console=NULL;
1042 MIDebugger::endianType MIDebugger::GetTargetEndian()
1044 if (targetEndian!=enUnknown)
1045 return targetEndian;
1046 if (state!=stopped && state!=target_specified)
1049 char *end=Show("endian");
1052 if (strstr(end,"big"))
1054 else if (strstr(end,"little"))
1055 targetEndian=enLittle;
1058 return targetEndian;
1061 MIDebugger::archType MIDebugger::GetTargetArchitecture()
1063 if (targetArch!=arUnknown)
1065 if (state!=stopped && state!=target_specified)
1068 char *end=Show("architecture");
1071 if (strstr(end,"i386"))
1073 else if (strstr(end,"sparc"))
1075 else if (strstr(end,"pic14"))
1077 else if (strstr(end,"avr"))
1084 int MIDebugger::GetErrorNumberSt()
1086 if (mi_error==MI_GDB_DIED)
1088 state=target_specified;
1096 int MIDebugger::UpdateRegisters(mi_chg_reg *regs)
1099 mi_chg_reg *chg=GetChangedRegisters();
1102 mi_chg_reg *r=regs, *c;
1106 while (c && c->reg!=r->reg)