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 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// 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 "event_notifier.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>
#include "file_logger.h"
#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
#include <sys/types.h>
#include <signal.h>
#if 0
# define DBG_LOG 1
static wxFFile gfp(wxT("debugger.log"), wxT("w+"));
#else
# define DBG_LOG 0
#endif
const wxEventType wxEVT_GDB_STOP_DEBUGGER = wxNewEventType();
//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" ) );
#ifdef __WXMSW__
string.Replace("\\r\\n", "\r\n");
#endif
string = string.Trim();
}
static wxString MakeId()
{
static unsigned int counter( 0 );
wxString newId;
newId.Printf( wxT( "%08u" ), ++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_break_at_main( false )
, m_attachedMode(false)
, m_goingDown(false)
, 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
EventNotifier::Get()->Connect(wxEVT_GDB_STOP_DEBUGGER, wxCommandEventHandler(DbgGdb::OnKillGDB), NULL, this);
}
DbgGdb::~DbgGdb()
{
#ifdef __WXMSW__
if ( Kernel32Dll ) {
FreeLibrary( Kernel32Dll );
Kernel32Dll = NULL;
}
#endif
EventNotifier::Get()->Disconnect(wxEVT_GDB_STOP_DEBUGGER, wxCommandEventHandler(DbgGdb::OnKillGDB), NULL, this);
}
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 DebugSessionInfo& si)
{
//set the environment variables
EnvSetter env( m_env, NULL, m_debuggeeProjectName );
wxString dbgExeName;
if ( ! DoLocateGdbExecutable( si.debuggerPath, dbgExeName ) ) {
return false;
}
wxString cmd;
#if defined (__WXGTK__) || defined (__WXMAC__)
cmd << dbgExeName;
if ( !si.ttyName.IsEmpty() ) {
cmd << wxT( " --tty=" ) << si.ttyName;
}
cmd << wxT( " --interpreter=mi " ) << si.exeName;
#else
cmd << dbgExeName << wxT( " --interpreter=mi " ) << si.exeName;
#endif
m_debuggeePid = wxNOT_FOUND;
m_attachedMode = false;
m_observer->UpdateAddLine( wxString::Format( wxT( "Current working dir: %s" ), wxGetCwd().c_str() ) );
m_observer->UpdateAddLine( wxString::Format( wxT( "Launching gdb from : %s" ), si.cwd.c_str() ) );
m_observer->UpdateAddLine( wxString::Format( wxT( "Starting debugger : %s" ), cmd.c_str() ) );
#ifdef __WXMSW__
// When using remote debugging on Windows we need a console window, as this is the only
// mechanism to send a Ctrl-C event and signal a SIGINT to interrupt the target.
bool needs_console = GetIsRemoteDebugging() | m_info.showTerminal;
#else
bool needs_console = m_info.showTerminal;
#endif
m_gdbProcess = CreateAsyncProcess( this,
cmd,
// show console?
needs_console ? IProcessCreateConsole : IProcessCreateDefault,
si.cwd );
if ( !m_gdbProcess ) {
return false;
}
#ifdef __WXMSW__
if ( GetIsRemoteDebugging() ) {
// This doesn't really make sense, but AttachConsole fails without it...
AllocConsole();
FreeConsole(); // Disconnect any existing console window.
if ( !AttachConsole( m_gdbProcess->GetPid() ) )
m_observer->UpdateAddLine( wxString::Format(wxT("AttachConsole returned error %d"), GetLastError()));
// We can at least make the window invisible if the user doesn't want to see it.
if ( !m_info.showTerminal )
SetWindowPos(GetConsoleWindow(), HWND_BOTTOM, 0, 0, 0, 0, SWP_HIDEWINDOW);
// Finally we ignore SIGINT so we don't get killed by our own signal
signal(SIGINT, SIG_IGN);
}
#endif
m_gdbProcess->SetHardKill( true );
DoInitializeGdb( si );
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::Run( const wxString &args, const wxString &comm )
{
if ( !GetIsRemoteDebugging() ) {
// add handler for this command
wxString setArgsCommands;
setArgsCommands << wxT("-exec-arguments ") << args;
if(!WriteCommand(setArgsCommands, NULL))
return false;
return WriteCommand( wxT( "-exec-run " ), new DbgCmdHandlerExecRun( m_observer, this ) );
} else {
// attach to the remote gdb server
wxString cmd;
//cmd << wxT("-target-select remote ") << comm << wxT(" ") << args;
if ( GetIsRemoteExtended() )
cmd << wxT( "target extended-remote " ) << comm << wxT( " " ) << args;
else
cmd << wxT( "target remote " ) << comm << wxT( " " ) << args;
return WriteCommand( cmd, new DbgCmdHandlerRemoteDebugging( m_observer, this ) );
}
}
void DbgGdb::DoCleanup()
{
if ( m_gdbProcess ) {
delete m_gdbProcess;
m_gdbProcess = NULL;
}
m_goingDown = false;
m_attachedMode = false;
SetIsRemoteDebugging( false );
SetIsRemoteExtended( false );
EmptyQueue();
m_gdbOutputArr.Clear();
m_bpList.clear();
m_debuggeeProjectName.Clear();
// Clear any bufferd output
m_gdbOutputIncompleteLine.Clear();
// Free allocated console for this session
m_consoleFinder.FreeConsole();
}
bool DbgGdb::Stop()
{
m_goingDown = true;
if ( !m_attachedMode ) {
wxKill(m_debuggeePid, wxSIGKILL, NULL, wxKILL_CHILDREN);
}
wxCommandEvent event(wxEVT_GDB_STOP_DEBUGGER);
EventNotifier::Get()->AddPendingEvent(event);
return true;
}
bool DbgGdb::Next()
{
return WriteCommand( wxT( "-exec-next" ), new DbgCmdHandlerAsyncCmd( m_observer, this ) );
}
bool DbgGdb::NextInstruction()
{
return WriteCommand( wxT( "-exec-next-instruction" ), 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 )
{
wxString breakinsertcmd(wxT("-break-insert "));
if ( m_info.enablePendingBreakpoints ) {
breakinsertcmd << wxT("-f ");
}
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 = breakinsertcmd + wxT("-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 << breakinsertcmd;
if ( bp.is_temp ) {
command << " -t ";
}
if ( !bp.is_enabled ) {
command << " -d ";
}
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 = breakinsertcmd + wxT("-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
DbgCmdHandlerBp* dbgCommandHandler = new DbgCmdHandlerBp( m_observer, this, bp, &m_bpList, bp.bp_type );
return WriteCommand( gdbCommand, dbgCommandHandler);
}
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: %ld" ), m_debuggeePid ) );
#ifdef __WXMSW__
if ( !GetIsRemoteDebugging() && DebugBreakProcessFunc ) {
// we have DebugBreakProcess
HANDLE process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ( DWORD )m_debuggeePid );
BOOL res = DebugBreakProcessFunc( process );
CloseHandle(process);
return res == TRUE;
}
if ( GetIsRemoteDebugging() ) {
// We need to send GDB a Ctrl-C event. Using DebugBreakProcess just leaves
// it unresponsive.
return GenerateConsoleCtrlEvent( CTRL_C_EVENT, 0 );
}
// 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
} else {
::wxMessageBox(_("Can't interrupt debuggee process: I don't know its PID!"), wxT("CodeLite"));
}
return false;
}
bool DbgGdb::QueryFileLine()
{
#if defined (__WXGTK__) || defined(__WXMAC__)
if(!WriteCommand( wxT( "-stack-info-frame" ), new DbgCmdHandlerGetLine( m_observer, this ) ))
return false;
#elif defined (__WXMSW__)
if(!WriteCommand( wxT( "-file-list-exec-source-file" ), new DbgCmdHandlerGetLine( m_observer, this ) ))
return false;
#endif
return true;
}
bool DbgGdb::QueryLocals()
{
return WriteCommand( wxT( "-stack-list-variables 2" ), new DbgCmdHandlerLocals( m_observer ) );
}
bool DbgGdb::ExecuteCmd( const wxString &cmd )
{
if( m_gdbProcess ) {
if ( m_info.enableDebugLog ) {
#if DBG_LOG
if(gfp.IsOpened()) {
gfp.Write(wxString::Format( wxT( "DEBUG>>%s\n" ), cmd.c_str() ));
gfp.Flush();
}
#else
m_observer->UpdateAddLine( wxString::Format( wxT( "DEBUG>>%s" ), cmd.c_str() ) );
#endif
}
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 curline;
if ( !m_gdbProcess || m_gdbOutputArr.IsEmpty() ) {
return;
}
while ( DoGetNextLine( curline ) ) {
GetDebugeePID(curline);
// For string manipulations without damaging the original line read
wxString tmpline ( curline );
StripString( tmpline );
tmpline.Trim().Trim( false );
if ( m_info.enableDebugLog ) {
//Is logging enabled?
if ( curline.IsEmpty() == false && !tmpline.StartsWith( wxT( ">" ) ) ) {
wxString strdebug( wxT( "DEBUG>>" ) );
strdebug << curline;
#if DBG_LOG
if(gfp.IsOpened()) {
gfp.Write(strdebug);
gfp.Flush();
}
#else
m_observer->UpdateAddLine( strdebug );
#endif
}
}
if ( reConnectionRefused.Matches( curline ) ) {
StripString( curline );
#ifdef __WXGTK__
m_consoleFinder.FreeConsole();
#endif
m_observer->UpdateAddLine( curline );
m_observer->UpdateGotControl( DBG_EXITED_NORMALLY );
return;
}
// Check for "Operation not permitted" usually means
// that the process does not have enough permission to
// attach to the process
if( curline.Contains(wxT("Operation not permitted")) ) {
#ifdef __WXGTK__
m_consoleFinder.FreeConsole();
#endif
m_observer->UpdateAddLine( _("Failed to start debugger: permission denied") );
m_observer->UpdateGotControl( DBG_EXITED_NORMALLY );
return;
}
if( tmpline.StartsWith( wxT( ">" ) ) ) {
// Shell line, probably user command line
continue;
}
if ( curline.StartsWith( wxT( "~" ) ) || curline.StartsWith( wxT( "&" ) ) || curline.StartsWith("@") ) {
// lines starting with ~ are considered "console stream" message
// and are important to the CLI handler
bool consoleStream( false );
bool targetConsoleStream(false);
if ( curline.StartsWith( wxT( "~" ) ) ) {
consoleStream = true;
}
if ( curline.StartsWith( wxT( "@" ) ) ) {
targetConsoleStream = true;
}
// Filter out some gdb error lines...
if ( FilterMessage( curline ) ) {
continue;
}
StripString( curline );
// If we got a valid "CLI Handler" instead of writing the output to
// the output view, concatenate it into the handler buffer
if ( targetConsoleStream ) {
m_observer->UpdateAddLine( curline );
} else if ( consoleStream && GetCliHandler()) {
GetCliHandler()->Append( curline );
} else if ( consoleStream ) {
// log message
m_observer->UpdateAddLine( curline );
}
} else if ( reCommand.Matches( curline ) ) {
//not a gdb message, get the command associated with the message
wxString id = reCommand.GetMatch( curline, 1 );
if ( GetCliHandler() && GetCliHandler()->GetCommandId() == id ) {
// probably the "^done" message of the CLI command
GetCliHandler()->ProcessOutput( curline );
SetCliHandler( NULL ); // we are done processing the CLI
} else {
//strip the id from the line
curline = curline.Mid( 8 );
DoProcessAsyncCommand( curline, id );
}
} else if ( curline.StartsWith( wxT( "^done" ) ) || curline.StartsWith( wxT( "*stopped" ) ) ) {
//Unregistered command, use the default AsyncCommand handler to process the line
DbgCmdHandlerAsyncCmd cmd( m_observer, this );
cmd.ProcessOutput( curline );
} else {
//Unknow format, just log it
if( m_info.enableDebugLog && !FilterMessage( curline ) ) {
m_observer->UpdateAddLine( curline );
}
}
}
}
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 );
bool errorProcessed (false);
if ( handler && handler->WantsErrors() ) {
errorProcessed = handler->ProcessOutput( line );
}
if ( handler ) {
delete handler;
}
StripString( line );
//We also need to pass the control back to the program
if (!errorProcessed) {
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()
{
int max = m_info.maxCallStackFrames;
#ifndef __WXMAC__
wxString command = wxString::Format("-stack-list-frames 0 %i", max);
#else
// Under OSX GDB version is old and does not support the min-max version
// of callstack info
wxString command = "-stack-list-frames";
#endif
return WriteCommand(command, 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 WriteCommand( wxT( "-thread-info" ), 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;
DoCleanup();
m_observer->UpdateGotControl( DBG_EXITED_NORMALLY );
}
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, int userReason )
{
wxString cmd;
cmd << wxT( "-var-create - * \"" ) << expression << wxT( "\"" );
return WriteCommand( cmd, new DbgCmdResolveTypeHandler( expression, this, userReason ) );
}
bool DbgGdb::WatchMemory( const wxString& address, size_t count, size_t columns )
{
// make the line per WORD size
int divider ( columns );
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, columns ) );
}
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;
codelite_gdbinit_file << wxFileName::GetTempDir() << wxFileName::GetPathSeparator() << wxT( "codelite_gdbinit.txt" );
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();
dbgExeName << wxT( " --command=\"" ) << codelite_gdbinit_file << wxT( "\"" );
}
return true;
}
// Initialization stage
bool DbgGdb::DoInitializeGdb(const DebugSessionInfo& sessionInfo)
{
m_goingDown = false;
m_internalBpId = wxNOT_FOUND;
#ifdef __WXMSW__
ExecuteCmd( wxT( "set new-console on" ) );
#endif
ExecuteCmd( wxT( "set unwindonsignal on" ) );
wxString breakinsertcmd(wxT("-break-insert "));
if ( m_info.enablePendingBreakpoints ) {
ExecuteCmd( wxT( "set breakpoint pending on" ) );
breakinsertcmd << wxT("-f ");
}
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" ) );
// 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<sessionInfo.cmds.GetCount(); i++ ) {
ExecuteCmd( sessionInfo.cmds.Item( i ) );
}
// keep the list of breakpoints
m_bpList = sessionInfo.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 'internal' breakpoint at main. Once this breakpoint is hit
// set all breakpoints and remove the 'internal' one.
// Then 'continue', unless the user has said he actually _wants_ to break at main
WriteCommand( breakinsertcmd + wxT("-t main"), new DbgFindMainBreakpointIdHandler( m_observer, this ) );
}
if (m_info.breakAtWinMain) {
// Set a breakpoint at WinMain
// Use a temporary one, so that it isn't duplicated in future sessions
WriteCommand( breakinsertcmd + wxT("-t main"), NULL );
// Flag that we've done this. DbgFindMainBreakpointIdHandler::ProcessOutput uses this
// to decide whether or not to 'continue' after setting BPs after main()
SetShouldBreakAtMain(true);
} else {
SetShouldBreakAtMain(false); // Needs explicitly to be set, in case the user has just changed his options
}
// Enable python based pretty printing?
if ( sessionInfo.enablePrettyPrinting ) {
WriteCommand( wxT( "-enable-pretty-printing" ), NULL );
}
// Add the additional search paths
for(size_t i=0; i<sessionInfo.searchPaths.GetCount(); ++i) {
wxString dirCmd;
wxString path = sessionInfo.searchPaths.Item(i);
path.Trim().Trim(false);
if ( path.Contains(" ") ) {
path.Prepend('"').Append('"');
}
dirCmd << "-environment-directory " << path;
WriteCommand( dirCmd, 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, bool persistent, int userReason)
{
wxString cmd;
cmd << wxT( "-var-create - ");
if(persistent) {
cmd << wxT("* ");
} else {
cmd << wxT("@ ");
}
cmd << wxT("\"") << 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, int userReason )
{
wxString cmd;
cmd << wxT( "-var-evaluate-expression \"" ) << name << wxT( "\"" );
return WriteCommand( cmd, new DbgCmdEvalVarObj( m_observer, name, userReason ) );
}
void DbgGdb::OnDataRead( wxCommandEvent& e )
{
// Data arrived from the debugger
ProcessEventData *ped = ( ProcessEventData * )e.GetClientData();
wxString bufferRead;
bufferRead << ped->GetData();
delete ped;
if( !m_gdbProcess || !m_gdbProcess->IsAlive() )
return;
CL_DEBUG("GDB>> %s", bufferRead);
wxArrayString lines = wxStringTokenize( bufferRead, wxT( "\n" ), wxTOKEN_STRTOK );
if(lines.IsEmpty())
return;
// Prepend the partially saved line from previous iteration to the first line
// of this iteration
lines.Item(0).Prepend(m_gdbOutputIncompleteLine);
m_gdbOutputIncompleteLine.Clear();
// If the last line is in-complete, remove it from the array and keep it for next iteration
if(!bufferRead.EndsWith(wxT("\n"))) {
m_gdbOutputIncompleteLine = lines.Last();
lines.RemoveAt(lines.GetCount()-1);
}
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 DbgCmdHandlerExecRun( m_observer, this ) );
}
bool DbgGdb::SetVariableObbjectDisplayFormat(const wxString& name, DisplayFormat displayFormat)
{
wxString df, cmd;
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;
return WriteCommand( cmd, NULL );
}
bool DbgGdb::UpdateVariableObject(const wxString& name, int userReason)
{
wxString cmd;
cmd << wxT( "-var-update \"" ) << name << wxT( "\" " );
return WriteCommand( cmd, new DbgVarObjUpdate(m_observer, this, name, userReason) );
}
void DbgGdb::AssignValue(const wxString& expression, const wxString& newValue)
{
wxString cmd;
cmd << wxT("set variable ") << expression << wxT("=") << newValue;
ExecuteCmd(cmd);
}
void DbgGdb::GetDebugeePID(const wxString& line)
{
if ( m_debuggeePid == wxNOT_FOUND ) {
if ( GetIsRemoteDebugging() ) {
m_debuggeePid = m_gdbProcess->GetPid();
} else {
static wxRegEx reDebuggerPidWin(wxT("New Thread ([0-9]+)\\.(0[xX][0-9a-fA-F]+)"));
static wxRegEx reGroupStarted(wxT("id=\"([0-9]+)\""));
static wxRegEx reSwitchToThread(wxT("Switching to process ([0-9]+)"));
// test for the debuggee PID
// in the line with the following pattern:
// =thread-group-started,id="i1",pid="15599"
if(m_debuggeePid < 0 && !line.IsEmpty()) {
wxString debuggeePidStr;
if(line.Contains(wxT("=thread-group-started")) && reGroupStarted.Matches(line)) {
debuggeePidStr = reGroupStarted.GetMatch(line, 1);
} else if(line.Contains(wxT("=thread-group-created")) && reGroupStarted.Matches(line)) {
debuggeePidStr = reGroupStarted.GetMatch(line, 1);
} else if(reDebuggerPidWin.Matches(line)) {
debuggeePidStr = reDebuggerPidWin.GetMatch(line, 1);
} else if(reSwitchToThread.Matches(line)) {
debuggeePidStr = reSwitchToThread.GetMatch(line, 1);
}
if(!debuggeePidStr.IsEmpty()) {
long iPid(0);
if(debuggeePidStr.ToLong(&iPid)) {
m_debuggeePid = iPid;
wxString msg;
msg << wxT( ">> Debuggee process ID: " ) << m_debuggeePid;
m_observer->UpdateAddLine( msg );
// Now there's a known pid, the debugger can be interrupted to let any to-be-disabled bps be disabled. So...
m_observer->DebuggerPidValid();
}
}
}
}
}
}
bool DbgGdb::Jump(wxString filename, int line)
{
BreakpointInfo bp;
bp.Create(filename, line, -1);
bp.bp_type = BP_type_tempbreak;
Break(bp);
// by default, use full paths for the file name
wxFileName fn( filename );
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;
command << wxT( "-exec-jump " ) << wxT( "\"\\\"" ) << tmpfileName << wxT( ":" ) << line << wxT( "\\\"\"" );
//return WriteCommand( command, new DbgCmdHandlerAsyncCmd( m_observer, this ) );
return ExecCLICommand( command, new DbgCmdJumpHandler( m_observer ) );
}
void DbgGdb::OnKillGDB(wxCommandEvent& e)
{
wxUnusedVar(e);
DoCleanup();
m_observer->UpdateGotControl( DBG_DBGR_KILLED );
}
bool DbgGdb::Disassemble(const wxString& filename, int lineNumber)
{
// Trigger a "disasseble" call
if ( /*filename.IsEmpty() || lineNumber == wxNOT_FOUND*/ true ) {
// Use the $pc
if ( !WriteCommand("-data-disassemble -s \"$pc -100\" -e \"$pc + 100\" -- 0", new DbgCmdHandlerDisasseble(m_observer, this)) )
return false;
} else {
// else, use the file and line provided
wxString tmpfile = filename;
tmpfile.Replace("\\", "/"); // gdb does not like backslashes...
if ( !WriteCommand(wxString() << "-data-disassemble -f \"" << tmpfile << "\" -l " << lineNumber << " -n -1 -- 0",
new DbgCmdHandlerDisasseble(m_observer, this)) )
return false;
}
// get the current instruction
if ( !WriteCommand("-data-disassemble -s \"$pc\" -e \"$pc + 1\" -- 0", new DbgCmdHandlerDisassebleCurLine(m_observer, this)) )
return false;
return true;
}
bool DbgGdb::Attach(const DebugSessionInfo& si)
{
//set the environment variables
EnvSetter env( m_env, NULL, m_debuggeeProjectName );
wxString dbgExeName;
if ( ! DoLocateGdbExecutable( si.debuggerPath, dbgExeName ) ) {
return false;
}
wxString cmd;
#if defined (__WXGTK__) || defined (__WXMAC__)
cmd << dbgExeName;
if ( !si.ttyName.IsEmpty() ) {
cmd << wxT( " --tty=" ) << si.ttyName;
}
cmd << wxT( " --interpreter=mi " );
#else
cmd << dbgExeName << wxT( " --interpreter=mi " );
cmd << ProcUtils::GetProcessNameByPid( si.PID ) << wxT( " " );
#endif
//if(sudoCmd.IsEmpty() == false) {
// cmd.Prepend(sudoCmd + wxT(" "));
//}
m_attachedMode = true;
m_debuggeePid = si.PID;
cmd << wxT( " --pid=" ) << m_debuggeePid;
wxLogMessage( cmd );
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( si );
m_observer->UpdateGotControl( DBG_END_STEPPING );
return true;
}
bool DbgGdb::ListRegisters()
{
return WriteCommand("-data-list-register-names", new DbgCmdHandlerRegisterNames(m_observer, this));
}
|