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
|
//===-- MIDriver.cpp --------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//++
// File: MIDriver.cpp
//
// Overview: CMIDriver implementation.
//
// Environment: Compilers: Visual C++ 12.
// gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
// Libraries: See MIReadmetxt.
//
// Copyright: None.
//--
// Third party headers:
#include <stdarg.h> // va_list, va_start, var_end
#include <iostream>
#include <lldb/API/SBError.h>
// In-house headers:
#include "Driver.h"
#include "MIDriver.h"
#include "MICmnResources.h"
#include "MICmnLog.h"
#include "MICmdMgr.h"
#include "MICmnLLDBDebugger.h"
#include "MICmnMIResultRecord.h"
#include "MICmnMIValueConst.h"
#include "MICmnThreadMgrStd.h"
#include "MIUtilDebug.h"
#include "MIUtilSingletonHelper.h"
#include "MICmnStreamStdout.h"
#include "MICmnStreamStderr.h"
// Instantiations:
#if _DEBUG
const CMIUtilString CMIDriver::ms_constMIVersion = MIRSRC( IDS_MI_VERSION_DESCRIPTION_DEBUG );
#else
const CMIUtilString CMIDriver::ms_constMIVersion = MIRSRC( IDS_MI_VERSION_DESCRIPTION ); // Matches version in resources file
#endif // _DEBUG
const CMIUtilString CMIDriver::ms_constAppNameShort( MIRSRC( IDS_MI_APPNAME_SHORT ) );
const CMIUtilString CMIDriver::ms_constAppNameLong( MIRSRC( IDS_MI_APPNAME_LONG ) );
//++ ------------------------------------------------------------------------------------
// Details: CMIDriver constructor.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIDriver::CMIDriver( void )
: m_bFallThruToOtherDriverEnabled( false )
, m_bDriverIsExiting( false )
, m_handleMainThread( 0 )
, m_rStdin( CMICmnStreamStdin::Instance() )
, m_rLldbDebugger( CMICmnLLDBDebugger::Instance() )
, m_rStdOut( CMICmnStreamStdout::Instance() )
, m_eCurrentDriverState( eDriverState_NotRunning )
{
}
//++ ------------------------------------------------------------------------------------
// Details: CMIDriver destructor.
// Type: Overridden.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIDriver::~CMIDriver( void )
{
}
//++ ------------------------------------------------------------------------------------
// Details: Set whether *this driver (the parent) is enabled to pass a command to its
// fall through (child) driver to interpret the command and do work instead
// (if *this driver decides it can't hanled the command).
// Type: Method.
// Args: vbYes - (R) True = yes fall through, false = do not pass on command.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::SetEnableFallThru( const bool vbYes )
{
m_bFallThruToOtherDriverEnabled = vbYes;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Get whether *this driver (the parent) is enabled to pass a command to its
// fall through (child) driver to interpret the command and do work instead
// (if *this driver decides it can't hanled the command).
// Type: Method.
// Args: None.
// Return: bool - True = yes fall through, false = do not pass on command.
// Throws: None.
//--
bool CMIDriver::GetEnableFallThru( void ) const
{
return m_bFallThruToOtherDriverEnabled;
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve MI's application name of itself.
// Type: Method.
// Args: None.
// Return: CMIUtilString & - Text description.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetAppNameShort( void ) const
{
return ms_constAppNameShort;
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve MI's application name of itself.
// Type: Method.
// Args: None.
// Return: CMIUtilString & - Text description.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetAppNameLong( void ) const
{
return ms_constAppNameLong;
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve MI's version description of itself.
// Type: Method.
// Args: None.
// Return: CMIUtilString & - Text description.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetVersionDescription( void ) const
{
return ms_constMIVersion;
}
//++ ------------------------------------------------------------------------------------
// Details: Initialize setup *this driver ready for use.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::Initialize( void )
{
m_eCurrentDriverState = eDriverState_Initialising;
m_clientUsageRefCnt++;
ClrErrorDescription();
if( m_bInitialized )
return MIstatus::success;
bool bOk = MIstatus::success;
CMIUtilString errMsg;
// Initialize all of the modules we depend on
MI::ModuleInit< CMICmnLog > ( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
MI::ModuleInit< CMICmnStreamStdout >( IDS_MI_INIT_ERR_STREAMSTDOUT , bOk, errMsg );
MI::ModuleInit< CMICmnStreamStderr >( IDS_MI_INIT_ERR_STREAMSTDERR , bOk, errMsg );
MI::ModuleInit< CMICmnResources > ( IDS_MI_INIT_ERR_RESOURCES , bOk, errMsg );
MI::ModuleInit< CMICmnThreadMgrStd >( IDS_MI_INIT_ERR_THREADMANAGER, bOk, errMsg );
MI::ModuleInit< CMICmnStreamStdin > ( IDS_MI_INIT_ERR_STREAMSTDIN , bOk, errMsg );
MI::ModuleInit< CMICmdMgr > ( IDS_MI_INIT_ERR_CMDMGR , bOk, errMsg );
bOk &= m_rLldbDebugger.SetDriver( *this );
MI::ModuleInit< CMICmnLLDBDebugger >( IDS_MI_INIT_ERR_LLDBDEBUGGER , bOk, errMsg );
#if MICONFIG_COMPILE_MIDRIVER_WITH_LLDBDRIVER
CMIDriverMgr & rDrvMgr = CMIDriverMgr::Instance();
bOk = bOk && rDrvMgr.RegisterDriver( *g_driver, "LLDB driver" ); // Will be pass thru driver
if( bOk )
{
bOk = SetEnableFallThru( false ); // This is intentional at this time - yet to be fully implemented
bOk = bOk && SetDriverToFallThruTo( *g_driver );
CMIUtilString strOtherDrvErrMsg;
if( bOk && GetEnableFallThru() && !g_driver->MISetup( strOtherDrvErrMsg ) )
{
bOk = false;
errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_FALLTHRUDRIVER ), strOtherDrvErrMsg.c_str() );
}
}
#endif // MICONFIG_COMPILE_MIDRIVER_WITH_LLDBDRIVER
m_bExitApp = false;
bOk = bOk && InitClientIDEToMIDriver(); // Init Eclipse IDE
m_bInitialized = bOk;
if( !bOk )
{
const CMIUtilString msg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_DRIVER ), errMsg.c_str() );
SetErrorDescription( msg );
return MIstatus::failure;
}
m_eCurrentDriverState = eDriverState_RunningNotDebugging;
return bOk;
}
//++ ------------------------------------------------------------------------------------
// Details: Unbind detach or release resources used by *this driver.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::Shutdown( void )
{
if( --m_clientUsageRefCnt > 0 )
return MIstatus::success;
if( !m_bInitialized )
return MIstatus::success;
m_eCurrentDriverState = eDriverState_ShuttingDown;
ClrErrorDescription();
bool bOk = MIstatus::success;
CMIUtilString errMsg;
// Shutdown all of the modules we depend on
MI::ModuleShutdown< CMICmnLLDBDebugger >( IDS_MI_INIT_ERR_LLDBDEBUGGER , bOk, errMsg );
MI::ModuleShutdown< CMICmdMgr > ( IDS_MI_INIT_ERR_CMDMGR , bOk, errMsg );
MI::ModuleShutdown< CMICmnStreamStdin > ( IDS_MI_INIT_ERR_STREAMSTDIN , bOk, errMsg );
MI::ModuleShutdown< CMICmnThreadMgrStd >( IDS_MI_INIT_ERR_THREADMANAGER, bOk, errMsg );
MI::ModuleShutdown< CMICmnResources > ( IDS_MI_INIT_ERR_RESOURCES , bOk, errMsg );
MI::ModuleShutdown< CMICmnStreamStderr >( IDS_MI_INIT_ERR_STREAMSTDERR , bOk, errMsg );
MI::ModuleShutdown< CMICmnStreamStdout >( IDS_MI_INIT_ERR_STREAMSTDOUT , bOk, errMsg );
MI::ModuleShutdown< CMICmnLog > ( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
if( !bOk )
{
SetErrorDescriptionn( MIRSRC( IDS_MI_SHUTDOWN_ERR ), errMsg.c_str() );
}
m_eCurrentDriverState = eDriverState_NotRunning;
return bOk;
}
//++ ------------------------------------------------------------------------------------
// Details: Work function. Client (the driver's user) is able to append their own message
// in to the MI's Log trace file.
// Type: Method.
// Args: vMessage - (R) Client's text message.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::WriteMessageToLog( const CMIUtilString & vMessage )
{
CMIUtilString msg;
msg = CMIUtilString::Format( MIRSRC( IDS_MI_CLIENT_MSG ), vMessage.c_str() );
return m_pLog->Write( msg, CMICmnLog::eLogVerbosity_ClientMsg );
}
//++ ------------------------------------------------------------------------------------
// Details: CDriverMgr calls *this driver initialize setup ready for use.
// Type: Overridden.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::DoInitialize( void )
{
return CMIDriver::Instance().Initialize();
}
//++ ------------------------------------------------------------------------------------
// Details: CDriverMgr calls *this driver to unbind detach or release resources used by
// *this driver.
// Type: Overridden.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::DoShutdown( void )
{
return CMIDriver::Instance().Shutdown();
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve the name for *this driver.
// Type: Overridden.
// Args: None.
// Return: CMIUtilString & - Driver name.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetName( void ) const
{
const CMIUtilString & rName = GetAppNameLong();
const CMIUtilString & rVsn = GetVersionDescription();
static CMIUtilString strName = CMIUtilString::Format( "%s %s", rName.c_str(), rVsn.c_str() );
return strName;
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve *this driver's last error condition.
// Type: Overridden.
// Args: None.
// Return: CMIUtilString - Text description.
// Throws: None.
//--
CMIUtilString CMIDriver::GetError( void ) const
{
return GetErrorDescription();
}
//++ ------------------------------------------------------------------------------------
// Details: Call *this driver to resize the console window.
// Type: Overridden.
// Args: vTermWidth - (R) New window column size.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
void CMIDriver::DoResizeWindow( const uint32_t vTermWidth )
{
GetTheDebugger().SetTerminalWidth( vTermWidth );
}
//++ ------------------------------------------------------------------------------------
// Details: Call *this driver to return it's debugger.
// Type: Overridden.
// Args: None.
// Return: lldb::SBDebugger & - LLDB debugger object reference.
// Throws: None.
//--
lldb::SBDebugger & CMIDriver::GetTheDebugger( void )
{
return m_rLldbDebugger.GetTheDebugger();
}
//++ ------------------------------------------------------------------------------------
// Details: Specify another driver *this driver can call should this driver not be able
// to handle the client data input. DoFallThruToAnotherDriver() makes the call.
// Type: Overridden.
// Args: vrOtherDriver - (R) Reference to another driver object.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::SetDriverToFallThruTo( const CMIDriverBase & vrOtherDriver )
{
m_pDriverFallThru = const_cast< CMIDriverBase * >( &vrOtherDriver );
return m_pDriverFallThru->SetDriverParent( *this );
}
//++ ------------------------------------------------------------------------------------
// Details: Proxy function CMIDriverMgr IDriver interface implementation. *this driver's
// implementation called from here to match the existing function name of the
// original LLDb driver class (the extra indirection is not necessarily required).
// Check the arguments that were passed to this program to make sure they are
// valid and to get their argument values (if any).
// Type: Overridden.
// Args: argc - (R) An integer that contains the count of arguments that follow in
// argv. The argc parameter is always greater than or equal to 1.
// argv - (R) An array of null-terminated strings representing command-line
// arguments entered by the user of the program. By convention,
// argv[0] is the command with which the program is invoked.
// vpStdOut - (R) Pointer to a standard output stream.
// vwbExiting - (W) True = *this want to exit, Reasons: help, invalid arg(s),
// version information only.
// False = Continue to work, start debugger i.e. Command
// interpreter.
// Return: lldb::SBError - LLDB current error status.
// Throws: None.
//--
lldb::SBError CMIDriver::DoParseArgs( const int argc, const char * argv[], FILE * vpStdOut, bool & vwbExiting )
{
return ParseArgs( argc, argv, vpStdOut, vwbExiting );
}
//++ ------------------------------------------------------------------------------------
// Details: Check the arguments that were passed to this program to make sure they are
// valid and to get their argument values (if any).
// Type: Overridden.
// Args: argc - (R) An integer that contains the count of arguments that follow in
// argv. The argc parameter is always greater than or equal to 1.
// argv - (R) An array of null-terminated strings representing command-line
// arguments entered by the user of the program. By convention,
// argv[0] is the command with which the program is invoked.
// vpStdOut - (R) Pointer to a standard output stream.
// vwbExiting - (W) True = *this want to exit, Reasons: help, invalid arg(s),
// version information only.
// False = Continue to work, start debugger i.e. Command
// interpreter.
// Return: lldb::SBError - LLDB current error status.
// Throws: None.
//--
lldb::SBError CMIDriver::ParseArgs( const int argc, const char * argv[], FILE * vpStdOut, bool & vwbExiting )
{
lldb::SBError errStatus;
// Do nothing - no options to handle for *this driver
return errStatus;
}
//++ ------------------------------------------------------------------------------------
// Details: A client can ask if *this driver is GDB/MI compatible.
// Type: Overridden.
// Args: None.
// Return: True - GBD/MI compatible LLDB front end.
// False - Not GBD/MI compatible LLDB front end.
// Throws: None.
//--
bool CMIDriver::GetDriverIsGDBMICompatibleDriver( void ) const
{
return true;
}
//++ ------------------------------------------------------------------------------------
// Details: Callback function for monitoring stream stdin object. Part of the visitor
// pattern.
// This function is called by the CMICmnStreamStdin::CThreadStdin
// "stdin monitor" thread (ID).
// Type: Overridden.
// Args: vStdInBuffer - (R) Copy of the current stdin line data.
// vrbYesExit - (RW) True = yes exit stdin monitoring, false = continue monitor.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::ReadLine( const CMIUtilString & vStdInBuffer, bool & vrwbYesExit )
{
// For debugging. Update prompt show stdin is working
//printf( "%s\n", vStdInBuffer.c_str() );
//fflush( stdout );
// Special case look for the quit command here so stop monitoring stdin stream
// So we do not go back to fgetc() and wait and hang thread on exit
if( vStdInBuffer == "quit" )
vrwbYesExit = true;
// 1. Put new line in the queue container by stdin monitor thread
// 2. Then *this driver calls ReadStdinLineQueue() when ready to read the queue in its
// own thread
const bool bOk = QueueMICommand( vStdInBuffer );
// Check to see if the *this driver is shutting down (exit application)
if( !vrwbYesExit )
vrwbYesExit = m_bDriverIsExiting;
return bOk;
}
//++ ------------------------------------------------------------------------------------
// Details: Start worker threads for the driver.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::StartWorkerThreads( void )
{
bool bOk = MIstatus::success;
// Grab the thread manager
CMICmnThreadMgrStd & rThreadMgr = CMICmnThreadMgrStd::Instance();
// Start the stdin thread
bOk &= m_rStdin.SetVisitor( *this );
if( bOk && !rThreadMgr.ThreadStart< CMICmnStreamStdin >( m_rStdin ))
{
const CMIUtilString errMsg = CMIUtilString::Format( MIRSRC( IDS_THREADMGR_ERR_THREAD_FAIL_CREATE ), CMICmnThreadMgrStd::Instance().GetErrorDescription().c_str() );
SetErrorDescriptionn( errMsg );
return MIstatus::failure;
}
// Start the event polling thread
if( bOk && !rThreadMgr.ThreadStart< CMICmnLLDBDebugger >( m_rLldbDebugger ) )
{
const CMIUtilString errMsg = CMIUtilString::Format( MIRSRC( IDS_THREADMGR_ERR_THREAD_FAIL_CREATE ), CMICmnThreadMgrStd::Instance().GetErrorDescription().c_str() );
SetErrorDescriptionn( errMsg );
return MIstatus::failure;
}
return bOk;
}
//++ ------------------------------------------------------------------------------------
// Details: Stop worker threads for the driver.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::StopWorkerThreads( void )
{
CMICmnThreadMgrStd & rThreadMgr = CMICmnThreadMgrStd::Instance();
return rThreadMgr.ThreadAllTerminate();
}
//++ ------------------------------------------------------------------------------------
// Details: Call this function puts *this driver to work.
// This function is used by the application's main thread.
// Type: Overridden.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::DoMainLoop( void )
{
if( !StartWorkerThreads() )
return MIstatus::failure;
// App is not quitting currently
m_bExitApp = false;
// While the app is active
while( !m_bExitApp )
{
// Poll stdin queue and dispatch
if( !ReadStdinLineQueue() )
{
// Something went wrong
break;
}
}
// Signal that the application is shutting down
DoAppQuit();
// Close and wait for the workers to stop
StopWorkerThreads();
// Ensure that a new line is sent as the last act of the dying driver
m_rStdOut.WriteMIResponse( "\n", false );
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: *this driver sits and waits for input to the stdin line queue shared by *this
// driver and the stdin monitor thread, it queues, *this reads, interprets and
// reacts.
// This function is used by the application's main thread.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::ReadStdinLineQueue( void )
{
// True when queue contains input
bool bHaveInput = false;
// Stores the current input line
CMIUtilString lineText;
{
// Lock while we access the queue
CMIUtilThreadLock lock( m_threadMutex );
if( !m_queueStdinLine.empty() )
{
lineText = m_queueStdinLine.front();
m_queueStdinLine.pop();
bHaveInput = !lineText.empty();
}
}
// Process while we have input
if( bHaveInput )
{
if( lineText == "quit" )
{
// We want to be exiting when receiving a quit command
m_bExitApp = true;
return MIstatus::success;
}
// Process the command
const bool bOk = InterpretCommand( lineText );
// Draw prompt if desired
if( bOk && m_rStdin.GetEnablePrompt() )
m_rStdOut.WriteMIResponse( m_rStdin.GetPrompt() );
// Input has been processed
bHaveInput = false;
}
else
{
// Give resources back to the OS
const std::chrono::milliseconds time( 1 );
std::this_thread::sleep_for( time );
}
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Set things in motion, set state etc that brings *this driver (and the
// application) to a tidy shutdown.
// This function is used by the application's main thread.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::DoAppQuit( void )
{
bool bYesQuit = true;
// Shutdown stuff, ready app for exit
{
CMIUtilThreadLock lock( m_threadMutex );
m_bDriverIsExiting = true;
}
return bYesQuit;
}
//++ ------------------------------------------------------------------------------------
// Details: *this driver passes text commands to a fall through driver is it does not
// understand them (the LLDB driver).
// This function is used by the application's main thread.
// Type: Method.
// Args: vTextLine - (R) Text data representing a possible command.
// vwbCmdYesValid - (W) True = Command valid, false = command not handled.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::InterpretCommandFallThruDriver( const CMIUtilString & vTextLine, bool & vwbCmdYesValid )
{
MIunused( vTextLine );
MIunused( vwbCmdYesValid );
// ToDo: Implement when less urgent work to be done or decide remove as not required
//bool bOk = MIstatus::success;
//bool bCmdNotUnderstood = true;
//if( bCmdNotUnderstood && GetEnableFallThru() )
//{
// CMIUtilString errMsg;
// bOk = DoFallThruToAnotherDriver( vStdInBuffer, errMsg );
// if( !bOk )
// {
// errMsg = errMsg.StripCREndOfLine();
// errMsg = errMsg.StripCRAll();
// const CMIDriverBase * pOtherDriver = GetDriverToFallThruTo();
// const MIchar * pName = pOtherDriver->GetDriverName().c_str();
// const MIchar * pId = pOtherDriver->GetDriverId().c_str();
// const CMIUtilString msg( CMIUtilString::Format( MIRSRC( IDS_DRIVER_ERR_FALLTHRU_DRIVER_ERR ), pName, pId, errMsg.c_str() ) );
// m_pLog->WriteMsg( msg );
// }
//}
//
//vwbCmdYesValid = bOk;
//CMIUtilString strNot;
//if( vwbCmdYesValid)
// strNot = CMIUtilString::Format( "%s ", MIRSRC( IDS_WORD_NOT ) );
//const CMIUtilString msg( CMIUtilString::Format( MIRSRC( IDS_FALLTHRU_DRIVER_CMD_RECEIVED ), vTextLine.c_str(), strNot.c_str() ) );
//m_pLog->WriteLog( msg );
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve the name for *this driver.
// Type: Overridden.
// Args: None.
// Return: CMIUtilString & - Driver name.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetDriverName( void ) const
{
return GetName();
}
//++ ------------------------------------------------------------------------------------
// Details: Get the unique ID for *this driver.
// Type: Overridden.
// Args: None.
// Return: CMIUtilString & - Text description.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetDriverId( void ) const
{
return GetId();
}
//++ ------------------------------------------------------------------------------------
// Details: This function allows *this driver to call on another driver to perform work
// should this driver not be able to handle the client data input.
// SetDriverToFallThruTo() specifies the fall through to driver.
// Check the error message if the function returns a failure.
// Type: Overridden.
// Args: vCmd - (R) Command instruction to interpret.
// vwErrMsg - (W) Error description on command failing.
// Return: MIstatus::success - Command succeeded.
// MIstatus::failure - Command failed.
// Throws: None.
//--
bool CMIDriver::DoFallThruToAnotherDriver( const CMIUtilString & vCmd, CMIUtilString & vwErrMsg )
{
bool bOk = MIstatus::success;
CMIDriverBase * pOtherDriver = GetDriverToFallThruTo();
if( pOtherDriver == nullptr )
return bOk;
return pOtherDriver->DoFallThruToAnotherDriver( vCmd, vwErrMsg );
}
//++ ------------------------------------------------------------------------------------
// Details: *this driver provides a file stream to other drivers on which *this driver
// write's out to and they read as expected input. *this driver is passing
// through commands to the (child) pass through assigned driver.
// Type: Overrdidden.
// Args: None.
// Return: FILE * - Pointer to stream.
// Throws: None.
//--
FILE * CMIDriver::GetStdin( void ) const
{
// Note this fn is called on CMIDriverMgr register driver so stream has to be
// available before *this driver has been initialized! Flaw?
// This very likely to change later to a stream that the pass thru driver
// will read and we write to give it 'input'
return stdin;
}
//++ ------------------------------------------------------------------------------------
// Details: *this driver provides a file stream to other pass through assigned drivers
// so they know what to write to.
// Type: Overridden.
// Args: None.
// Return: FILE * - Pointer to stream.
// Throws: None.
//--
FILE * CMIDriver::GetStdout( void ) const
{
// Note this fn is called on CMIDriverMgr register driver so stream has to be
// available before *this driver has been initialized! Flaw?
// Do not want to pass through driver to write to stdout
return NULL;
}
//++ ------------------------------------------------------------------------------------
// Details: *this driver provides a error file stream to other pass through assigned drivers
// so they know what to write to.
// Type: Overridden.
// Args: None.
// Return: FILE * - Pointer to stream.
// Throws: None.
//--
FILE * CMIDriver::GetStderr( void ) const
{
// Note this fn is called on CMIDriverMgr register driver so stream has to be
// available before *this driver has been initialized! Flaw?
// This very likely to change later to a stream that the pass thru driver
// will write to and *this driver reads from to pass on the CMICmnLog object
return stderr;
}
//++ ------------------------------------------------------------------------------------
// Details: Set a unique ID for *this driver. It cannot be empty.
// Type: Overridden.
// Args: vId - (R) Text description.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::SetId( const CMIUtilString & vId )
{
if( vId.empty() )
{
SetErrorDescriptionn( MIRSRC( IDS_DRIVER_ERR_ID_INVALID ), GetName().c_str(), vId.c_str() );
return MIstatus::failure;
}
m_strDriverId = vId;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Get the unique ID for *this driver.
// Type: Overridden.
// Args: None.
// Return: CMIUtilString & - Text description.
// Throws: None.
//--
const CMIUtilString & CMIDriver::GetId( void ) const
{
return m_strDriverId;
}
//++ ------------------------------------------------------------------------------------
// Details: Inject a command into the command processing system to be interpreted as a
// command read from stdin. The text representing the command is also written
// out to stdout as the command did not come from via stdin.
// Type: Method.
// Args: vMICmd - (R) Text data representing a possible command.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::InjectMICommand( const CMIUtilString & vMICmd )
{
const bool bOk = m_rStdOut.WriteMIResponse( vMICmd );
return bOk && QueueMICommand( vMICmd );
}
//++ ------------------------------------------------------------------------------------
// Details: Add a new command candidate to the command queue to be processed by the
// command system.
// Type: Method.
// Args: vMICmd - (R) Text data representing a possible command.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::QueueMICommand( const CMIUtilString & vMICmd )
{
CMIUtilThreadLock lock( m_threadMutex );
m_queueStdinLine.push( vMICmd );
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Interpret the text data and match against current commands to see if there
// is a match. If a match then the command is issued and actioned on. The
// text data if not understood by *this driver is past on to the Fall Thru
// driver.
// This function is used by the application's main thread.
// Type: Method.
// Args: vTextLine - (R) Text data representing a possible command.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::InterpretCommand( const CMIUtilString & vTextLine )
{
bool bCmdYesValid = false;
bool bOk = InterpretCommandThisDriver( vTextLine, bCmdYesValid );
if( bOk && !bCmdYesValid )
bOk = InterpretCommandFallThruDriver( vTextLine, bCmdYesValid );
return bOk;
}
//++ ------------------------------------------------------------------------------------
// Details: Interpret the text data and match against current commands to see if there
// is a match. If a match then the command is issued and actioned on. If a
// command cannot be found to match then vwbCmdYesValid is set to false and
// nothing else is done here.
// This function is used by the application's main thread.
// Type: Method.
// Args: vTextLine - (R) Text data representing a possible command.
// vwbCmdYesValid - (W) True = Command invalid, false = command acted on.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::InterpretCommandThisDriver( const CMIUtilString & vTextLine, bool & vwbCmdYesValid )
{
vwbCmdYesValid = false;
bool bCmdNotInCmdFactor = false;
SMICmdData cmdData;
CMICmdMgr & rCmdMgr = CMICmdMgr::Instance();
if( !rCmdMgr.CmdInterpret( vTextLine, vwbCmdYesValid, bCmdNotInCmdFactor, cmdData ) )
return MIstatus::failure;
if( vwbCmdYesValid )
{
// For debugging only
//m_pLog->WriteLog( cmdData.strMiCmdAll.c_str() );
return ExecuteCommand( cmdData );
}
// Write to the Log that a 'command' was not valid.
// Report back to the MI client via MI result record.
CMIUtilString strNotInCmdFactory;
if( bCmdNotInCmdFactor )
strNotInCmdFactory = CMIUtilString::Format( MIRSRC( IDS_DRIVER_CMD_NOT_IN_FACTORY ), cmdData.strMiCmd.c_str() );
const CMIUtilString strNot( CMIUtilString::Format( "%s ", MIRSRC( IDS_WORD_NOT ) ) );
const CMIUtilString msg( CMIUtilString::Format( MIRSRC( IDS_DRIVER_CMD_RECEIVED ), vTextLine.c_str(), strNot.c_str(), strNotInCmdFactory.c_str() ) );
const CMICmnMIValueConst vconst = CMICmnMIValueConst( msg );
const CMICmnMIValueResult valueResult( "msg", vconst );
const CMICmnMIResultRecord miResultRecord( cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error, valueResult );
m_rStdOut.WriteMIResponse( miResultRecord.GetString() );
// Proceed to wait for or execute next command
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Having previously had the potential command validated and found valid now
// get the command executed.
// This function is used by the application's main thread.
// Type: Method.
// Args: vCmdData - (RW) Command meta data.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIDriver::ExecuteCommand( const SMICmdData & vCmdData )
{
CMICmdMgr & rCmdMgr = CMICmdMgr::Instance();
return rCmdMgr.CmdExecute( vCmdData );
}
//++ ------------------------------------------------------------------------------------
// Details: Set the MI Driver's exit application flag. The application checks this flag
// after every stdin line is read so the exit may not be instantaneous.
// If vbForceExit is false the MI Driver queries its state and determines if is
// should exit or continue operating depending on that running state.
// This is related to the running state of the MI driver.
// Type: Overridden.
// Args: None.
// Return: None.
// Throws: None.
//--
void CMIDriver::SetExitApplicationFlag( const bool vbForceExit )
{
if( vbForceExit )
{
CMIUtilThreadLock lock( m_threadMutex );
m_bExitApp = true;
return;
}
// CODETAG_DEBUG_SESSION_RUNNING_PROG_RECEIVED_SIGINT_PAUSE_PROGRAM
// Did we receive a SIGINT from the client during a running debug program, if
// so then SIGINT is not to be taken as meaning kill the MI driver application
// but halt the inferior program being debugged instead
if( m_eCurrentDriverState == eDriverState_RunningDebugging )
{
InjectMICommand( "-exec-interrupt" );
return;
}
m_bExitApp = true;
}
//++ ------------------------------------------------------------------------------------
// Details: Get the MI Driver's exit exit application flag.
// This is related to the running state of the MI driver.
// Type: Method.
// Args: None.
// Return: bool - True = MI Driver is shutting down, false = MI driver is running.
// Throws: None.
//--
bool CMIDriver::GetExitApplicationFlag( void ) const
{
return m_bExitApp;
}
//++ ------------------------------------------------------------------------------------
// Details: Get the current running state of the MI Driver.
// Type: Method.
// Args: None.
// Return: DriverState_e - The current running state of the application.
// Throws: None.
//--
CMIDriver::DriverState_e CMIDriver::GetCurrentDriverState( void ) const
{
return m_eCurrentDriverState;
}
//++ ------------------------------------------------------------------------------------
// Details: Set the current running state of the MI Driver to running and currently in
// a debug session. The driver's state must in the state running and not in a
// debug session to set this new state.
// Type: Method.
// Return: MIstatus::success - Functionality succeeded.
// MIstatus::failure - Functionality failed.
// Return: DriverState_e - The current running state of the application.
// Throws: None.
//--
bool CMIDriver::SetDriverStateRunningNotDebugging( void )
{
// CODETAG_DEBUG_SESSION_RUNNING_PROG_RECEIVED_SIGINT_PAUSE_PROGRAM
if( m_eCurrentDriverState == eDriverState_RunningNotDebugging )
return MIstatus::success;
// Driver cannot be in the following states to set eDriverState_RunningNotDebugging
switch( m_eCurrentDriverState )
{
case eDriverState_NotRunning:
case eDriverState_Initialising:
case eDriverState_ShuttingDown:
{
SetErrorDescription( MIRSRC( IDS_DRIVER_ERR_DRIVER_STATE_ERROR ) );
return MIstatus::failure;
}
case eDriverState_RunningDebugging:
case eDriverState_RunningNotDebugging:
break;
case eDriverState_count:
default:
SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CODE_ERR_INVALID_ENUMERATION_VALUE ), "SetDriverStateRunningNotDebugging()" ) );
return MIstatus::failure;
}
// Driver must be in this state to set eDriverState_RunningNotDebugging
if( m_eCurrentDriverState != eDriverState_RunningDebugging )
{
SetErrorDescription( MIRSRC( IDS_DRIVER_ERR_DRIVER_STATE_ERROR ) );
return MIstatus::failure;
}
m_eCurrentDriverState = eDriverState_RunningNotDebugging;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Set the current running state of the MI Driver to running and currently not in
// a debug session. The driver's state must in the state running and in a
// debug session to set this new state.
// Type: Method.
// Return: MIstatus::success - Functionality succeeded.
// MIstatus::failure - Functionality failed.
// Return: DriverState_e - The current running state of the application.
// Throws: None.
//--
bool CMIDriver::SetDriverStateRunningDebugging( void )
{
// CODETAG_DEBUG_SESSION_RUNNING_PROG_RECEIVED_SIGINT_PAUSE_PROGRAM
if( m_eCurrentDriverState == eDriverState_RunningDebugging )
return MIstatus::success;
// Driver cannot be in the following states to set eDriverState_RunningDebugging
switch( m_eCurrentDriverState )
{
case eDriverState_NotRunning:
case eDriverState_Initialising:
case eDriverState_ShuttingDown:
{
SetErrorDescription( MIRSRC( IDS_DRIVER_ERR_DRIVER_STATE_ERROR ) );
return MIstatus::failure;
}
case eDriverState_RunningDebugging:
case eDriverState_RunningNotDebugging:
break;
case eDriverState_count:
default:
SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CODE_ERR_INVALID_ENUMERATION_VALUE ), "SetDriverStateRunningDebugging()" ) );
return MIstatus::failure;
}
// Driver must be in this state to set eDriverState_RunningDebugging
if( m_eCurrentDriverState != eDriverState_RunningNotDebugging )
{
SetErrorDescription( MIRSRC( IDS_DRIVER_ERR_DRIVER_STATE_ERROR ) );
return MIstatus::failure;
}
m_eCurrentDriverState = eDriverState_RunningDebugging;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Prepare the client IDE so it will start working/communicating with *this MI
// driver.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functionality succeeded.
// MIstatus::failure - Functionality failed.
// Throws: None.
//--
bool CMIDriver::InitClientIDEToMIDriver( void ) const
{
// Put other IDE init functions here
return InitClientIDEEclipse();
}
//++ ------------------------------------------------------------------------------------
// Details: The IDE Eclipse when debugging locally expects "(gdb)\n" character
// sequence otherwise it refuses to communicate and times out. This should be
// sent to Eclipse before anything else.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functionality succeeded.
// MIstatus::failure - Functionality failed.
// Throws: None.
//--
bool CMIDriver::InitClientIDEEclipse( void ) const
{
std::cout << "(gdb)" << std::endl;
return MIstatus::success;
}
|