Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame^] | 1 | //===-- MICmnLLDBProxySBValue.cpp -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | //++ |
| 11 | // File: MICmnLLDBProxySBValue.cpp |
| 12 | // |
| 13 | // Overview: CMICmnLLDBProxySBValue implementation. |
| 14 | // |
| 15 | // Environment: Compilers: Visual C++ 12. |
| 16 | // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1 |
| 17 | // Libraries: See MIReadmetxt. |
| 18 | // |
| 19 | // Copyright: None. |
| 20 | //-- |
| 21 | |
| 22 | // Third Party Headers: |
| 23 | #include <lldb/API/SBError.h> |
| 24 | |
| 25 | // In-house headers: |
| 26 | #include "MICmnLLDBProxySBValue.h" |
| 27 | #include "MIUtilString.h" |
| 28 | #include "MICmnLLDBDebugSessionInfo.h" |
| 29 | |
| 30 | //++ ------------------------------------------------------------------------------------ |
| 31 | // Details: Retrieve the numerical value from the SBValue object. If the function fails |
| 32 | // it could indicate the SBValue object does not represent an internal type. |
| 33 | // Type: Static method. |
| 34 | // Args: vrValue - (R) The SBValue object to get a value from. |
| 35 | // vwValue - (W) The numerical value. |
| 36 | // Return: MIstatus::success - Functionality succeeded. |
| 37 | // MIstatus::failure - Functionality failed. |
| 38 | // Throws: None. |
| 39 | //-- |
| 40 | bool CMICmnLLDBProxySBValue::GetValueAsUnsigned( const lldb::SBValue & vrValue, MIuint64 & vwValue ) |
| 41 | { |
| 42 | lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue ); |
| 43 | bool bCompositeType = true; |
| 44 | MIuint64 nFailValue = 0; |
| 45 | MIuint64 nValue = rValue.GetValueAsUnsigned( nFailValue ); |
| 46 | if( nValue == nFailValue ) |
| 47 | { |
| 48 | nFailValue = 5; // Some arbitary number |
| 49 | nValue = rValue.GetValueAsUnsigned( nFailValue ); |
| 50 | if( nValue != nFailValue ) |
| 51 | { |
| 52 | bCompositeType = false; |
| 53 | vwValue = nValue; |
| 54 | } |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | bCompositeType = false; |
| 59 | vwValue = nValue; |
| 60 | } |
| 61 | |
| 62 | return (bCompositeType ? MIstatus::failure : MIstatus::success); |
| 63 | } |
| 64 | |
| 65 | //++ ------------------------------------------------------------------------------------ |
| 66 | // Details: Retrieve the numerical value from the SBValue object. If the function fails |
| 67 | // it could indicate the SBValue object does not represent an internal type. |
| 68 | // Type: Static method. |
| 69 | // Args: vrValue - (R) The SBValue object to get a value from. |
| 70 | // vwValue - (W) The numerical value. |
| 71 | // Return: MIstatus::success - Functionality succeeded. |
| 72 | // MIstatus::failure - Functionality failed. |
| 73 | // Throws: None. |
| 74 | //-- |
| 75 | bool CMICmnLLDBProxySBValue::GetValueAsSigned( const lldb::SBValue & vrValue, MIint64 & vwValue ) |
| 76 | { |
| 77 | lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue ); |
| 78 | bool bCompositeType = true; |
| 79 | MIuint64 nFailValue = 0; |
| 80 | MIuint64 nValue = rValue.GetValueAsSigned( nFailValue ); |
| 81 | if( nValue == nFailValue ) |
| 82 | { |
| 83 | nFailValue = 5; // Some arbitary number |
| 84 | nValue = rValue.GetValueAsSigned( nFailValue ); |
| 85 | if( nValue != nFailValue ) |
| 86 | { |
| 87 | bCompositeType = false; |
| 88 | vwValue = nValue; |
| 89 | } |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | bCompositeType = false; |
| 94 | vwValue = nValue; |
| 95 | } |
| 96 | |
| 97 | return (bCompositeType ? MIstatus::failure : MIstatus::success); |
| 98 | } |
| 99 | |
| 100 | //++ ------------------------------------------------------------------------------------ |
| 101 | // Details: Retrieve the NUL terminated string from the SBValue object if it of the type |
| 102 | // unsigned char *. |
| 103 | // Type: Static method. |
| 104 | // Args: vrValue - (R) The SBValue object to get a value from. |
| 105 | // vwCString - (W) The text data '\0' terminated. |
| 106 | // Return: MIstatus::success - Functionality succeeded. |
| 107 | // MIstatus::failure - Functionality failed, not suitable type. |
| 108 | // Throws: None. |
| 109 | //-- |
| 110 | bool CMICmnLLDBProxySBValue::GetCString( const lldb::SBValue & vrValue, CMIUtilString & vwCString ) |
| 111 | { |
| 112 | lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue ); |
| 113 | const char * pCType = rValue.GetTypeName(); |
| 114 | if( pCType == nullptr ) |
| 115 | return MIstatus::failure; |
| 116 | |
| 117 | const MIchar * pType = "unsigned char *"; |
| 118 | if( !CMIUtilString::Compare( pCType, pType ) ) |
| 119 | return MIstatus::failure; |
| 120 | |
| 121 | const CMIUtilString strAddr( rValue.GetValue() ); |
| 122 | MIint64 nNum = 0; |
| 123 | if( !strAddr.ExtractNumber( nNum ) ) |
| 124 | return MIstatus::failure; |
| 125 | |
| 126 | CMICmnLLDBDebugSessionInfo & rSessionInfo( CMICmnLLDBDebugSessionInfo::Instance() ); |
| 127 | lldb::SBProcess & rProcess = rSessionInfo.m_lldbProcess; |
| 128 | MIuint nBufferSize = 64; |
| 129 | bool bNeedResize = false; |
| 130 | char * pBuffer = static_cast< char * >( ::malloc( nBufferSize ) ); |
| 131 | do |
| 132 | { |
| 133 | lldb::SBError error; |
| 134 | const size_t nReadSize = rProcess.ReadCStringFromMemory( (lldb::addr_t) nNum, pBuffer, nBufferSize, error ); |
| 135 | if( nReadSize == (nBufferSize - 1) ) |
| 136 | { |
| 137 | bNeedResize = true; |
| 138 | nBufferSize = nBufferSize << 1; |
| 139 | pBuffer = static_cast< char * >( ::realloc( pBuffer, nBufferSize ) ); |
| 140 | } |
| 141 | else |
| 142 | bNeedResize = false; |
| 143 | } |
| 144 | while( bNeedResize ); |
| 145 | |
| 146 | vwCString = pBuffer; |
| 147 | free( (void *) pBuffer ); |
| 148 | |
| 149 | return MIstatus::success; |
| 150 | } |