1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : debuggergdb.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "debuggergdb.h"
#include <wx/msgdlg.h>
#include "processreaderthread.h"
#include "asyncprocess.h"
#include <wx/ffile.h>
#include "exelocator.h"
#include "environmentconfig.h"
#include "dirkeeper.h"
#include "dbgcmd.h"
#include "wx/regex.h"
#include "debuggerobserver.h"
#include "wx/filename.h"
#include "procutils.h"
#include "wx/tokenzr.h"
#include <algorithm>
#ifdef __WXMSW__
#include "windows.h"
// On Windows lower than XP, the function DebugBreakProcess does not exist
// so we need to bind it dynamically
typedef WINBASEAPI BOOL WINAPI ( *DBG_BREAK_PROC_FUNC_PTR )( HANDLE );
DBG_BREAK_PROC_FUNC_PTR DebugBreakProcessFunc = NULL;
HINSTANCE Kernel32Dll = NULL;
// define a dummy control handler
BOOL CtrlHandler( DWORD fdwCtrlType ) {
wxUnusedVar( fdwCtrlType );
// return FALSE so other process in our group are allowed to process this event
return FALSE;
}
#endif
#if defined(__WXGTK__) || defined (__WXMAC__)
#include <sys/types.h>
#include <signal.h>
#endif
//Using the running image of child Thread 46912568064384 (LWP 7051).
static wxRegEx reInfoProgram1( wxT( "\\(LWP[ \t]([0-9]+)\\)" ) );
//Using the running image of child process 10011.
static wxRegEx reInfoProgram2( wxT( "child process ([0-9]+)" ) );
//Using the running image of child thread 4124.0x117c
static wxRegEx reInfoProgram3( wxT( "Using the running image of child thread ([0-9]+)" ) );
#ifdef __WXMSW__
static wxRegEx reConnectionRefused( wxT( "[0-9a-zA-Z/\\\\-\\_]*:[0-9]+: No connection could be made because the target machine actively refused it." ) );
#else
static wxRegEx reConnectionRefused( wxT( "[0-9a-zA-Z/\\\\-\\_]*:[0-9]+: Connection refused." ) );
#endif
DebuggerInfo GetDebuggerInfo() {
DebuggerInfo info = {
wxT( "GNU gdb debugger" ),
wxT( "CreateDebuggerGDB" ),
wxT( "v2.0" ),
wxT( "Eran Ifrah" )
};
return info;
}
IDebugger *CreateDebuggerGDB() {
static DbgGdb theGdbDebugger;
theGdbDebugger.SetName( wxT( "GNU gdb debugger" ) );
DebuggerInformation info;
info.name = theGdbDebugger.GetName();
theGdbDebugger.SetDebuggerInformation( info );
return &theGdbDebugger;
}
// Removes MI additional characters from string
static void StripString( wxString &string ) {
string.Replace( wxT( "\\n\"" ), wxT( "\"" ) );
string = string.AfterFirst( wxT( '"' ) );
string = string.BeforeLast( wxT( '"' ) );
string.Replace( wxT( "\\\"" ), wxT( "\"" ) );
string.Replace( wxT( "\\\\" ), wxT( "\\" ) );
string.Replace( wxT( "\\\\r\\\\n" ), wxT( "\r\n" ) );
string.Replace( wxT( "\\\\n" ), wxT( "\n" ) );
string.Replace( wxT( "\\\\r" ), wxT( "\r" ) );
string = string.Trim();
}
static wxString MakeId() {
static size_t counter( 0 );
wxString newId;
newId.Printf( wxT( "%08d" ), ++counter );
return newId;
}
BEGIN_EVENT_TABLE( DbgGdb, wxEvtHandler )
EVT_COMMAND( wxID_ANY, wxEVT_PROC_DATA_READ, DbgGdb::OnDataRead )
EVT_COMMAND( wxID_ANY, wxEVT_PROC_TERMINATED, DbgGdb::OnProcessEnd )
END_EVENT_TABLE()
DbgGdb::DbgGdb()
: m_debuggeePid( wxNOT_FOUND )
, m_cliHandler ( NULL )
, m_internalBpId( wxNOT_FOUND ) {
#ifdef __WXMSW__
Kernel32Dll = LoadLibrary( wxT( "kernel32.dll" ) );
if ( Kernel32Dll ) {
DebugBreakProcessFunc = ( DBG_BREAK_PROC_FUNC_PTR )GetProcAddress( Kernel32Dll, "DebugBreakProcess" );
} else {
// we dont have DebugBreakProcess, try to work with Control handlers
if ( SetConsoleCtrlHandler( ( PHANDLER_ROUTINE ) CtrlHandler, TRUE ) == FALSE ) {
wxLogMessage( wxString::Format( wxT( "failed to install ConsoleCtrlHandler: %d" ), GetLastError() ) );
}
}
if ( SetConsoleCtrlHandler( ( PHANDLER_ROUTINE ) CtrlHandler, TRUE ) == FALSE ) {
wxLogMessage( wxString::Format( wxT( "failed to install ConsoleCtrlHandler: %d" ), GetLastError() ) );
}
#endif
}
DbgGdb::~DbgGdb() {
#ifdef __WXMSW__
if ( Kernel32Dll ) {
FreeLibrary( Kernel32Dll );
Kernel32Dll = NULL;
}
#endif
}
void DbgGdb::RegisterHandler( const wxString &id, DbgCmdHandler *cmd ) {
m_handlers[id] = cmd;
}
DbgCmdHandler *DbgGdb::PopHandler( const wxString &id ) {
HandlersMap::iterator it = m_handlers.find( id );
if ( it == m_handlers.end() ) {
return NULL;
}
DbgCmdHandler *cmd = it->second;
m_handlers.erase( it );
return cmd;
}
void DbgGdb::EmptyQueue() {
HandlersMap::iterator iter = m_handlers.begin();
while ( iter != m_handlers.end() ) {
delete iter->second;
iter++;
}
m_handlers.clear();
}
bool DbgGdb::Start( const wxString &debuggerPath, const wxString & exeName, int pid, const std::vector<BreakpointInfo> &bpList, const wxArrayString &cmds ) {
wxString dbgExeName;
if ( ! DoLocateGdbExecutable( debuggerPath, dbgExeName ) ) {
return false;
}
wxString cmd;
#if defined (__WXGTK__) || defined (__WXMAC__)
//On GTK and other platforms, open a new terminal and direct all
//debugee process into it
wxString ptyName;
if ( !m_consoleFinder.FindConsole( wxT( "CodeLite: gdb" ), ptyName ) ) {
wxLogMessage( wxT( "Failed to allocate console for debugger" ) );
return false;
}
cmd << dbgExeName << wxT( " --tty=" ) << ptyName << wxT( " --interpreter=mi " );
#else
cmd << dbgExeName << wxT( " --interpreter=mi " );
cmd << ProcUtils::GetProcessNameByPid( pid ) << wxT( " " );
#endif
m_debuggeePid = pid;
cmd << wxT( " --pid=" ) << m_debuggeePid;
wxLogMessage( cmd );
//set the environment variables
EnvSetter env( m_env );
m_observer->UpdateAddLine( wxString::Format( wxT( "Current working dir: %s" ), wxGetCwd().c_str() ) );
m_observer->UpdateAddLine( wxString::Format( wxT( "Launching gdb from : %s" ), wxGetCwd().c_str() ) );
m_observer->UpdateAddLine( wxString::Format( wxT( "Starting debugger : %s" ), cmd.c_str() ) );
m_gdbProcess = CreateAsyncProcess( this, cmd );
if ( !m_gdbProcess ) {
return false;
}
m_gdbProcess->SetHardKill( true );
DoInitializeGdb( bpList, cmds );
m_observer->UpdateGotControl( DBG_END_STEPPING );
return true;
}
bool DbgGdb::Start( const wxString &debuggerPath, const wxString &exeName, const wxString &cwd, const std::vector<BreakpointInfo> &bpList, const wxArrayString &cmds ) {
wxString dbgExeName;
if ( ! DoLocateGdbExecutable( debuggerPath, dbgExeName ) ) {
return false;
}
wxString cmd;
#if defined (__WXGTK__) || defined (__WXMAC__)
//On GTK and other platforms, open a new terminal and direct all
//debugee process into it
wxString ptyName;
if ( !m_consoleFinder.FindConsole( exeName, ptyName ) ) {
wxLogMessage( wxT( "Failed to allocate console for debugger, do u have Xterm installed?" ) );
return false;
}
cmd << dbgExeName << wxT( " --tty=" ) << ptyName << wxT( " --interpreter=mi " ) << exeName;
#else
cmd << dbgExeName << wxT( " --interpreter=mi " ) << exeName;
#endif
m_debuggeePid = wxNOT_FOUND;
//set the environment variables
EnvSetter env( m_env );
m_observer->UpdateAddLine( wxString::Format( wxT( "Current working dir: %s" ), wxGetCwd().c_str() ) );
m_observer->UpdateAddLine( wxString::Format( wxT( "Launching gdb from : %s" ), cwd.c_str() ) );
m_observer->UpdateAddLine( wxString::Format( wxT( "Starting debugger : %s" ), cmd.c_str() ) );
m_gdbProcess = CreateAsyncProcess( this, cmd, cwd );
if ( !m_gdbProcess ) {
return false;
}
m_gdbProcess->SetHardKill( true );
DoInitializeGdb( bpList, cmds );
return true;
}
bool DbgGdb::WriteCommand( const wxString &command, DbgCmdHandler *handler ) {
wxString cmd;
wxString id = MakeId( );
cmd << id << command;
if ( !ExecuteCmd( cmd ) ) {
return false;
}
RegisterHandler( id, handler );
return true;
}
bool DbgGdb::Start( const wxString &exeName, const wxString &cwd, const std::vector<BreakpointInfo> &bpList, const wxArrayString &cmds ) {
return Start( wxT( "gdb" ), exeName, cwd, bpList, cmds );
}
bool DbgGdb::Run( const wxString &args, const wxString &comm ) {
if ( !GetIsRemoteDebugging() ) {
// add handler for this command
return WriteCommand( wxT( "-exec-run " ) + args, new DbgCmdHandlerAsyncCmd( m_observer, this ) );
} else {
// attach to the remote gdb server
wxString cmd;
//cmd << wxT("-target-select remote ") << comm << wxT(" ") << args;
cmd << wxT( "target remote " ) << comm << wxT( " " ) << args;
return WriteCommand( cmd, new DbgCmdHandlerRemoteDebugging( m_observer, this ) );
}
}
bool DbgGdb::Stop() {
if ( m_gdbProcess ) {
delete m_gdbProcess;
m_gdbProcess = NULL;
}
//free allocated console for this session
m_consoleFinder.FreeConsole();
//return control to the program
m_observer->UpdateGotControl( DBG_DBGR_KILLED );
EmptyQueue();
m_gdbOutputArr.Clear();
m_bpList.clear();
return true;
}
bool DbgGdb::Next() {
return WriteCommand( wxT( "-exec-next" ), new DbgCmdHandlerAsyncCmd( m_observer, this ) );
}
void DbgGdb::SetBreakpoints() {
for ( size_t i=0; i< m_bpList.size(); i++ ) {
// Without the 'unnecessary' cast in the next line, bpinfo.bp_type is seen as (e.g.) 4 instead of BP_type_tempbreak, ruining switch statments :/
BreakpointInfo bpinfo = ( BreakpointInfo )m_bpList.at( i );
Break( bpinfo );
}
}
bool DbgGdb::Break( const BreakpointInfo& bp ) {
wxFileName fn( bp.file );
// by default, use full paths for the file name when setting breakpoints
wxString tmpfileName( fn.GetFullPath() );;
if ( m_info.useRelativeFilePaths ) {
// user set the option to use relative paths (file name w/o the full path)
tmpfileName = fn.GetFullName();
}
tmpfileName.Replace( wxT( "\\" ), wxT( "/" ) );
wxString command;
switch ( bp.bp_type ) {
case BP_type_watchpt:
//----------------------------------
// Watchpoints
//----------------------------------
command = wxT( "-break-watch " );
switch ( bp.watchpoint_type ) {
case WP_watch:
// nothing to add, simple watchpoint - trigrred when BP is write
break;
case WP_rwatch:
// read watchpoint
command << wxT( "-r " );
break;
case WP_awatch:
// access watchpoint
command << wxT( "-a " );
break;
}
command << bp.watchpt_data;
break;
case BP_type_tempbreak:
//----------------------------------
// Temporary breakpoints
//----------------------------------
command = wxT( "-break-insert -t " );
break;
case BP_type_condbreak:
case BP_type_break:
default:
// Should be standard breakpts. But if someone tries to make an ignored temp bp
// it won't have the BP_type_tempbreak type, so check again here
command = ( bp.is_temp ? wxT( "-break-insert -t " ) : wxT( "-break-insert " ) );
break;
}
//------------------------------------------------------------------------
// prepare the 'break where' string (address, file:line or regex)
//------------------------------------------------------------------------
wxString breakWhere, ignoreCounnt, condition, gdbCommand;
if ( bp.memory_address.IsEmpty() == false ) {
// Memory is easy: just prepend *. gdb copes happily with (at least) hex or decimal
breakWhere << wxT( '*' ) << bp.memory_address;
} else if ( bp.bp_type != BP_type_watchpt ) {
// Function and Lineno locations can/should be prepended by a filename (but see later)
if ( ! tmpfileName.IsEmpty() && bp.lineno > 0 ) {
breakWhere << wxT( "\"\\\"" ) << tmpfileName << wxT( ":" ) << bp.lineno << wxT( "\\\"\"" );
} else if ( ! bp.function_name.IsEmpty() ) {
if ( bp.regex ) {
// update the command
command = wxT( "-break-insert -r " );
}
breakWhere = bp.function_name;
}
}
//------------------------------------------------------------------------
// prepare the conditions
//------------------------------------------------------------------------
if ( bp.conditions.IsEmpty() == false ) {
condition << wxT( "-c " ) << wxT( "\"" ) << bp.conditions << wxT( "\" " );
}
//------------------------------------------------------------------------
// prepare the ignore count
//------------------------------------------------------------------------
if ( bp.ignore_number > 0 ) {
ignoreCounnt << wxT( "-i " ) << bp.ignore_number << wxT( " " );
}
// concatenate all the string into one command to pass to gdb
gdbCommand << command << condition << ignoreCounnt << breakWhere;
// execute it
return WriteCommand( gdbCommand, new DbgCmdHandlerBp( m_observer, this, bp, &m_bpList, bp.bp_type ) );
}
bool DbgGdb::SetIgnoreLevel( const int bid, const int ignorecount ) {
if ( bid == -1 ) { // Sanity check
return false;
}
wxString command( wxT( "-break-after " ) );
command << bid << wxT( " " ) << ignorecount;
return WriteCommand( command, NULL );
}
bool DbgGdb::SetEnabledState( const int bid, const bool enable ) {
if ( bid == -1 ) { // Sanity check
return false;
}
wxString command( wxT( "-break-disable " ) );
if ( enable ) {
command = wxT( "-break-enable " );
}
command << bid;
return WriteCommand( command, NULL );
}
bool DbgGdb::SetCondition( const BreakpointInfo& bp ) {
if ( bp.debugger_id == -1 ) { // Sanity check
return false;
}
wxString command( wxT( "-break-condition " ) );
command << bp.debugger_id << wxT( " " ) << bp.conditions;
return WriteCommand( command, new DbgCmdSetConditionHandler( m_observer, bp ) );
}
bool DbgGdb::SetCommands( const BreakpointInfo& bp ) {
if ( bp.debugger_id == -1 ) { // Sanity check
return false;
}
// There isn't (currentl) a MI command-list command, so use the CLI one
// This doesn't actually work either, but at least the commands are visible in -break-list
wxString command( wxT( "commands " ) );
command << bp.debugger_id << wxT( '\n' ) << bp.commandlist << wxT( "\nend" );
if ( m_info.enableDebugLog ) {
m_observer->UpdateAddLine( command );
}
// If we really wanted, we could get the output (for bp 3) of "commands 3"
// but as that's not very informative, and we're only faking the command-list anyway, don't bother
return WriteCommand( command, NULL );
}
bool DbgGdb::Continue() {
return WriteCommand( wxT( "-exec-continue" ), new DbgCmdHandlerAsyncCmd( m_observer, this ) );
}
bool DbgGdb::StepIn() {
return WriteCommand( wxT( "-exec-step" ), new DbgCmdHandlerAsyncCmd( m_observer, this ) );
}
bool DbgGdb::StepOut() {
return WriteCommand( wxT( "-exec-finish" ), new DbgCmdHandlerAsyncCmd( m_observer, this ) );
}
bool DbgGdb::IsRunning() {
return m_gdbProcess != NULL;
}
bool DbgGdb::Interrupt() {
if ( m_debuggeePid > 0 ) {
m_observer->UpdateAddLine( wxString::Format( wxT( "Interrupting debugee process: %d" ), m_debuggeePid ) );
#ifdef __WXMSW__
if ( DebugBreakProcessFunc ) {
// we have DebugBreakProcess
HANDLE process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ( DWORD )m_debuggeePid );
BOOL res = DebugBreakProcessFunc( process );
return res == TRUE;
}
// on Windows version < XP we need to find a solution for interrupting the
// debuggee process
return false;
#else
kill( m_debuggeePid, SIGINT );
return true;
#endif
}
return false;
}
bool DbgGdb::QueryFileLine() {
#if defined (__WXMSW__) || defined (__WXGTK__)
return WriteCommand( wxT( "-file-list-exec-source-file" ), new DbgCmdHandlerGetLine( m_observer ) );
#else
//Mac
return WriteCommand( wxT( "-stack-list-frames 0 0" ), new DbgCmdHandlerGetLine( m_observer ) );
#endif
}
bool DbgGdb::QueryLocals() {
bool res = WriteCommand( wxT( "-stack-list-locals 2" ), new DbgCmdHandlerLocals( m_observer ) );
if ( !res ) {
return false;
}
return WriteCommand( wxT( "-stack-list-arguments 2 0 0 " ), new DbgCmdHandlerFuncArgs( m_observer ) );
}
bool DbgGdb::ExecuteCmd( const wxString &cmd ) {
if( m_gdbProcess ) {
if ( m_info.enableDebugLog ) {
m_observer->UpdateAddLine( wxString::Format( wxT( "DEBUG>>%s" ), cmd.c_str() ) );
}
return m_gdbProcess->Write( cmd );
}
return false;
}
bool DbgGdb::RemoveAllBreaks() {
return ExecuteCmd( wxT( "delete" ) );
}
bool DbgGdb::RemoveBreak( int bid ) {
wxString command;
command << wxT( "-break-delete " ) << bid;
return WriteCommand( command, NULL );
}
bool DbgGdb::FilterMessage( const wxString &msg ) {
wxString tmpmsg ( msg );
StripString( tmpmsg );
tmpmsg.Trim().Trim( false );
if ( tmpmsg.Contains( wxT( "Variable object not found" ) ) || msg.Contains( wxT( "Variable object not found" ) ) ) {
return true;
}
if ( tmpmsg.Contains( wxT( "mi_cmd_var_create: unable to create variable object" ) )||msg.Contains( wxT( "mi_cmd_var_create: unable to create variable object" ) ) ) {
return true;
}
if ( tmpmsg.Contains( wxT( "Variable object not found" ) )|| msg.Contains( wxT( "Variable object not found" ) ) ) {
return true;
}
if ( tmpmsg.Contains( wxT( "No symbol \"this\" in current context" ) )||msg.Contains( wxT( "No symbol \"this\" in current context" ) ) ) {
return true;
}
if ( tmpmsg.Contains( wxT( "*running,thread-id" ) ) ) {
return true;
}
if ( tmpmsg.StartsWith( wxT( ">" ) )||msg.StartsWith( wxT( ">" ) ) ) {
// shell line
return true;
}
return false;
}
void DbgGdb::Poke() {
static wxRegEx reCommand( wxT( "^([0-9]{8})" ) );
//poll the debugger output
wxString line;
if ( !m_gdbProcess || m_gdbOutputArr.IsEmpty() ) {
return;
}
if ( m_debuggeePid == wxNOT_FOUND ) {
if ( GetIsRemoteDebugging() ) {
m_debuggeePid = m_gdbProcess->GetPid();
} else {
std::vector<long> children;
ProcUtils::GetChildren( m_gdbProcess->GetPid(), children );
std::sort( children.begin(), children.end() );
if ( children.empty() == false ) {
m_debuggeePid = children.at( 0 );
}
if ( m_debuggeePid != wxNOT_FOUND ) {
wxString msg;
msg << wxT( "Debuggee process ID: " ) << m_debuggeePid;
m_observer->UpdateAddLine( msg );
}
}
}
while ( DoGetNextLine( line ) ) {
// For string manipulations without damaging the original line read
wxString tmpline ( line );
StripString( tmpline );
tmpline.Trim().Trim( false );
if ( m_info.enableDebugLog ) {
//Is logging enabled?
if ( line.IsEmpty() == false && !tmpline.StartsWith( wxT( ">" ) ) ) {
wxString strdebug( wxT( "DEBUG>>" ) );
strdebug << line;
m_observer->UpdateAddLine( strdebug );
}
}
if ( reConnectionRefused.Matches( line ) ) {
StripString( line );
#ifdef __WXGTK__
m_consoleFinder.FreeConsole();
#endif
m_observer->UpdateAddLine( line );
m_observer->UpdateGotControl( DBG_EXITED_NORMALLY );
return;
}
if( tmpline.StartsWith( wxT( ">" ) ) ) {
// Shell line, probably user command line
continue;
}
if ( line.StartsWith( wxT( "~" ) ) || line.StartsWith( wxT( "&" ) ) ) {
// lines starting with ~ are considered "console stream" message
// and are important to the CLI handler
bool consoleStream( false );
if ( line.StartsWith( wxT( "~" ) ) ) {
consoleStream = true;
}
// Filter out some gdb error lines...
if ( FilterMessage( line ) ) {
continue;
}
StripString( line );
// If we got a valid "CLI Handler" instead of writing the output to
// the output view, concatenate it into the handler buffer
if ( GetCliHandler() && consoleStream ) {
GetCliHandler()->Append( line );
} else if ( consoleStream ) {
// log message
m_observer->UpdateAddLine( line );
}
} else if ( reCommand.Matches( line ) ) {
//not a gdb message, get the command associated with the message
wxString id = reCommand.GetMatch( line, 1 );
if ( GetCliHandler() && GetCliHandler()->GetCommandId() == id ) {
// probably the "^done" message of the CLI command
GetCliHandler()->ProcessOutput( line );
SetCliHandler( NULL ); // we are done processing the CLI
} else {
//strip the id from the line
line = line.Mid( 8 );
DoProcessAsyncCommand( line, id );
}
} else if ( line.StartsWith( wxT( "^done" ) ) || line.StartsWith( wxT( "*stopped" ) ) ) {
//Unregistered command, use the default AsyncCommand handler to process the line
DbgCmdHandlerAsyncCmd cmd( m_observer, this );
cmd.ProcessOutput( line );
} else {
//Unknow format, just log it
if( m_info.enableDebugLog && !FilterMessage( line ) ) {
m_observer->UpdateAddLine( line );
}
}
}
}
void DbgGdb::DoProcessAsyncCommand( wxString &line, wxString &id ) {
if ( line.StartsWith( wxT( "^error" ) ) ) {
// the command was error, for example:
// finish in the outer most frame
// print the error message and remove the command from the queue
DbgCmdHandler *handler = PopHandler( id );
if ( handler && handler->WantsErrors() ) {
handler->ProcessOutput( line );
}
if ( handler ) {
delete handler;
}
StripString( line );
//We also need to pass the control back to the program
m_observer->UpdateGotControl( DBG_CMD_ERROR );
if ( !FilterMessage( line ) && m_info.enableDebugLog ) {
m_observer->UpdateAddLine( line );
}
} else if ( line.StartsWith( wxT( "^done" ) ) || line.StartsWith( wxT( "^connected" ) ) ) {
//The synchronous operation was successful, results are the return values.
DbgCmdHandler *handler = PopHandler( id );
if ( handler ) {
handler->ProcessOutput( line );
delete handler;
}
} else if ( line.StartsWith( wxT( "^running" ) ) ) {
//asynchronous command was executed
//send event that we dont have the control anymore
m_observer->UpdateLostControl();
} else if ( line.StartsWith( wxT( "*stopped" ) ) ) {
//get the stop reason,
if ( line == wxT( "*stopped" ) ) {
if ( m_bpList.empty() ) {
ExecuteCmd( wxT( "set auto-solib-add off" ) );
ExecuteCmd( wxT( "set stop-on-solib-events 0" ) );
} else {
//no reason for the failure, this means that we stopped due to
//hitting a loading of shared library
//try to place all breakpoints which previously failed
SetBreakpoints();
}
Continue();
} else {
//GDB/MI Out-of-band Records
//caused by async command, this line indicates that we have the control back
DbgCmdHandler *handler = PopHandler( id );
if ( handler ) {
handler->ProcessOutput( line );
delete handler;
}
}
}
}
bool DbgGdb::EvaluateExpressionToString( const wxString &expression, const wxString &format ) {
static int counter( 0 );
wxString watchName( wxT( "watch_num_" ) );
watchName << ++counter;
wxString command;
command << wxT( "-var-create " ) << watchName << wxT( " * " ) << expression;
//first create the expression
bool res = WriteCommand( command, new DbgCmdHandlerVarCreator( m_observer ) );
if ( !res ) {
//probably gdb is down
return false;
}
command.clear();
command << wxT( "-var-set-format " ) << watchName << wxT( " " ) << format;
//first create the expression
res = WriteCommand( command, NULL );
if ( !res ) {
//probably gdb is down
return false;
}
//execute the watch command
command.clear();
command << wxT( "-var-evaluate-expression " ) << watchName;
res = WriteCommand( command, new DbgCmdHandlerEvalExpr( m_observer, expression ) );
if ( !res ) {
//probably gdb is down
return false;
}
//and make sure we delete this variable
return DeleteVariableObject( watchName );
}
bool DbgGdb::ListFrames() {
return WriteCommand( wxT( "-stack-list-frames" ), new DbgCmdStackList( m_observer ) );
}
bool DbgGdb::SetFrame( int frame ) {
wxString command;
command << wxT( "frame " ) << frame;
return WriteCommand( command, new DbgCmdSelectFrame( m_observer ) );
}
bool DbgGdb::ListThreads() {
return ExecCLICommand( wxT( "info threads" ), new DbgCmdListThreads( m_observer ) );
}
bool DbgGdb::SelectThread( long threadId ) {
wxString command;
command << wxT( "-thread-select " ) << threadId;
return WriteCommand( command, NULL );
}
void DbgGdb::OnProcessEnd( wxCommandEvent &e ) {
ProcessEventData *ped = ( ProcessEventData * )e.GetClientData();
delete ped;
if ( m_gdbProcess ) {
delete m_gdbProcess;
m_gdbProcess = NULL;
}
m_observer->UpdateGotControl( DBG_EXITED_NORMALLY );
m_gdbOutputArr.Clear();
m_consoleFinder.FreeConsole();
SetIsRemoteDebugging( false );
}
bool DbgGdb::GetAsciiViewerContent( const wxString &dbgCommand, const wxString& expression ) {
wxString cmd;
cmd << dbgCommand << wxT( " " ) << expression;
return ExecCLICommand( cmd, new DbgCmdGetTipHandler( m_observer, expression ) );
}
bool DbgGdb::ResolveType( const wxString& expression ) {
wxString cmd;
cmd << wxT( "-var-create - * \"" ) << expression << wxT( "\"" );
return WriteCommand( cmd, new DbgCmdResolveTypeHandler( expression, this ) );
}
bool DbgGdb::WatchMemory( const wxString& address, size_t count ) {
// make the line per WORD size
int divider ( sizeof( unsigned long ) );
int factor( ( int )( count/divider ) );
if ( count % divider != 0 ) {
factor = ( int )( count / divider ) + 1;
}
// at this point, 'factor' contains the number rows
// and the 'divider' is the columns
wxString cmd;
cmd << wxT( "-data-read-memory \"" ) << address << wxT( "\" x 1 " ) << factor << wxT( " " ) << divider << wxT( " ?" );
return WriteCommand( cmd, new DbgCmdWatchMemory( m_observer, address, count ) );
}
bool DbgGdb::SetMemory( const wxString& address, size_t count, const wxString& hex_value ) {
wxString cmd;
wxString hexCommaDlimArr;
wxArrayString hexArr = wxStringTokenize( hex_value, wxT( " " ), wxTOKEN_STRTOK );
for ( size_t i=0; i<hexArr.GetCount(); i++ ) {
hexCommaDlimArr << hexArr.Item( i ) << wxT( "," );
}
hexCommaDlimArr.RemoveLast();
cmd << wxT( "set {char[" ) << count << wxT( "]}" ) << address << wxT( "={" ) << hexCommaDlimArr << wxT( "}" );
return ExecuteCmd( cmd );
}
void DbgGdb::SetDebuggerInformation( const DebuggerInformation& info ) {
IDebugger::SetDebuggerInformation( info );
m_consoleFinder.SetConsoleCommand( info.consoleCommand );
}
void DbgGdb::BreakList() {
( void )WriteCommand( wxT( "-break-list" ), new DbgCmdBreakList( m_observer ) );
}
bool DbgGdb::DoLocateGdbExecutable( const wxString& debuggerPath, wxString& dbgExeName ) {
if ( m_gdbProcess ) {
//dont allow second instance of the debugger
return false;
}
wxString cmd;
dbgExeName = debuggerPath;
if ( dbgExeName.IsEmpty() ) {
dbgExeName = wxT( "gdb" );
}
wxString actualPath;
if ( ExeLocator::Locate( dbgExeName, actualPath ) == false ) {
wxMessageBox( wxString::Format( wxT( "Failed to locate gdb! at '%s'" ), dbgExeName.c_str() ),
wxT( "CodeLite" ) );
return false;
}
// set the debugger specific startup commands
wxString startupInfo ( m_info.startupCommands );
// We must replace TABS with spaces or else gdb will hang...
startupInfo.Replace( wxT( "\t" ), wxT( " " ) );
// Write the content into a file
wxString codelite_gdbinit_file;
#ifdef __WXMSW__
codelite_gdbinit_file << wxFileName::GetTempDir() << wxFileName::GetPathSeparator() << wxT( "codelite_gdbinit.txt" );
#else
wxString home_env;
if( !wxGetEnv( wxT( "HOME" ), &home_env ) ) {
m_observer->UpdateAddLine( wxString::Format( wxT( "Failed to read HOME environment variable" ) ) );
} else {
codelite_gdbinit_file << home_env << wxT( "/" ) << wxT( ".gdbinit" );
if( wxFileName::FileExists( codelite_gdbinit_file ) && !wxFileName::FileExists( codelite_gdbinit_file + wxT( ".backup" ) ) ) {
wxCopyFile( codelite_gdbinit_file, codelite_gdbinit_file + wxT( ".backup" ) );
m_observer->UpdateAddLine( wxString::Format( wxT( ".gdbinit file was backup to %s" ), wxString( codelite_gdbinit_file + wxT( ".backup" ) ).c_str() ) );
}
}
#endif
wxFFile file;
if ( !file.Open( codelite_gdbinit_file, wxT( "w+b" ) ) ) {
m_observer->UpdateAddLine( wxString::Format( wxT( "Failed to generate gdbinit file at %s" ), codelite_gdbinit_file.c_str() ) );
} else {
m_observer->UpdateAddLine( wxString::Format( wxT( "Using gdbinit file: %s" ), codelite_gdbinit_file.c_str() ) );
file.Write( startupInfo );
file.Close();
#ifdef __WXMSW__
dbgExeName << wxT( " --command=\"" ) << codelite_gdbinit_file << wxT( "\"" );
#endif
}
return true;
}
// Initialization stage
bool DbgGdb::DoInitializeGdb( const std::vector<BreakpointInfo> &bpList, const wxArrayString &cmds ) {
m_internalBpId = wxNOT_FOUND;
#ifdef __WXMSW__
ExecuteCmd( wxT( "set new-console on" ) );
#endif
ExecuteCmd( wxT( "set unwindonsignal on" ) );
if ( m_info.enablePendingBreakpoints ) {
ExecuteCmd( wxT( "set breakpoint pending on" ) );
}
if ( m_info.catchThrow ) {
ExecuteCmd( wxT( "catch throw" ) );
}
#ifdef __WXMSW__
if ( m_info.debugAsserts ) {
ExecuteCmd( wxT( "break assert" ) );
}
#endif
ExecuteCmd( wxT( "set width 0" ) );
ExecuteCmd( wxT( "set height 0" ) );
ExecuteCmd( wxT( "set print pretty on" ) ); // pretty printing
// Number of elements to show for arrays (including strings)
wxString sizeCommand;
sizeCommand << wxT( "set print elements " ) << m_info.maxDisplayStringSize;
ExecuteCmd( sizeCommand );
// set the project startup commands
for ( size_t i=0; i<cmds.GetCount(); i++ ) {
ExecuteCmd( cmds.Item( i ) );
}
// keep the list of breakpoints
m_bpList = bpList;
bool setBreakpointsAfterMain( m_info.applyBreakpointsAfterProgramStarted );
if( GetIsRemoteDebugging() == false && !setBreakpointsAfterMain ) {
// When remote debugging, apply the breakpoints after we connect the
// gdbserver
SetBreakpoints();
} else if( setBreakpointsAfterMain && m_bpList.empty() == false ) {
// Place an intermediate breakpoint at main, once this breakpoint is hit
// set all breakpoints and remove that breakpoint + continue
WriteCommand( wxT( "-break-insert -t main" ), new DbgFindMainBreakpointIdHandler( m_observer, this ) );
}
if ( m_info.breakAtWinMain ) {
//try also to set breakpoint at WinMain
WriteCommand( wxT( "-break-insert main" ), NULL );
}
return true;
}
bool DbgGdb::ExecCLICommand( const wxString& command, DbgCmdCLIHandler* handler ) {
wxString cmd;
wxString id = MakeId( );
cmd << id << command;
//send the command to gdb
if ( !ExecuteCmd( cmd ) ) {
return false;
}
if ( handler ) {
handler->SetCommandId( id );
SetCliHandler( handler );
}
return true;
}
void DbgGdb::SetCliHandler( DbgCmdCLIHandler* handler ) {
if( m_cliHandler ) {
delete m_cliHandler;
}
m_cliHandler = handler;
}
DbgCmdCLIHandler* DbgGdb::GetCliHandler() {
return m_cliHandler;
}
bool DbgGdb::ListChildren( const wxString& name, int userReason ) {
wxString cmd;
cmd << wxT( "-var-list-children \"" ) << name << wxT( "\"" );
return WriteCommand( cmd, new DbgCmdListChildren( m_observer, name, userReason ) );
}
bool DbgGdb::CreateVariableObject( const wxString& expression, int userReason ) {
wxString cmd;
cmd << wxT( "-var-create - * \"" ) << expression << wxT( "\"" );
return WriteCommand( cmd, new DbgCmdCreateVarObj( m_observer, this, expression, userReason ) );
}
bool DbgGdb::DeleteVariableObject( const wxString& name ) {
wxString cmd;
cmd << wxT( "-var-delete \"" ) << name << wxT( "\"" );
return WriteCommand( cmd, NULL );
}
bool DbgGdb::EvaluateVariableObject( const wxString& name, DisplayFormat displayFormat, int userReason ) {
wxString cmd;
wxString df;
switch( displayFormat ) {
case DBG_DF_BINARY:
df = wxT( "binary" );
break;
case DBG_DF_DECIMAL:
df = wxT( "decimal" );
break;
case DBG_DF_HEXADECIMAL:
df = wxT( "hexadecimal" );
break;
case DBG_DF_OCTAL:
df = wxT( "octal" );
break;
default:
case DBG_DF_NATURAL:
df = wxT( "natural" );
break;
}
cmd << wxT( "-var-set-format \"" ) << name << wxT( "\" " ) << df;
WriteCommand( cmd, NULL );
cmd.Clear();
cmd << wxT( "-var-evaluate-expression \"" ) << name << wxT( "\"" );
return WriteCommand( cmd, new DbgCmdEvalVarObj( m_observer, name, displayFormat, userReason ) );
}
void DbgGdb::OnDataRead( wxCommandEvent& e ) {
// Data arrived from the debugger
ProcessEventData *ped = ( ProcessEventData * )e.GetClientData();
wxString bufferRead;
bufferRead << ped->GetData();
delete ped;
wxArrayString lines = wxStringTokenize( bufferRead, wxT( "\n" ), wxTOKEN_STRTOK );
for( size_t i=0; i<lines.GetCount(); i++ ) {
wxString line = lines.Item( i );
line.Replace( wxT( "(gdb)" ), wxT( "" ) );
line.Trim().Trim( false );
if ( line.IsEmpty() == false ) {
m_gdbOutputArr.Add( line );
}
}
if ( m_gdbOutputArr.IsEmpty() == false ) {
// Trigger GDB processing
Poke();
}
}
bool DbgGdb::DoGetNextLine( wxString& line ) {
line.Clear();
if ( m_gdbOutputArr.IsEmpty() ) {
return false;
}
line = m_gdbOutputArr.Item( 0 );
m_gdbOutputArr.RemoveAt( 0 );
line.Replace( wxT( "(gdb)" ), wxT( "" ) );
line.Trim().Trim( false );
if( line.IsEmpty() ) {
return false;
}
return true;
}
void DbgGdb::SetInternalMainBpID( int bpId ) {
m_internalBpId = bpId;
}
bool DbgGdb::Restart() {
return WriteCommand( wxT( "-exec-run " ) , new DbgCmdHandlerAsyncCmd( m_observer, this ) );
}
|