]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmnLLDBProxySBValue.cpp
Update LLDB snapshot to upstream r216948 (git 50f7fe44)
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmnLLDBProxySBValue.cpp
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 #include <stdlib.h>
23
24 // Third Party Headers:
25 #include <lldb/API/SBError.h>
26
27 // In-house headers:
28 #include "MICmnLLDBProxySBValue.h"
29 #include "MIUtilString.h"
30 #include "MICmnLLDBDebugSessionInfo.h"
31
32 //++ ------------------------------------------------------------------------------------
33 // Details:     Retrieve the numerical value from the SBValue object. If the function fails
34 //                      it could indicate the SBValue object does not represent an internal type.
35 // Type:        Static method.
36 // Args:        vrValue - (R) The SBValue object to get a value from.
37 //                      vwValue - (W) The numerical value.
38 // Return:      MIstatus::success - Functionality succeeded.
39 //                      MIstatus::failure - Functionality failed.
40 // Throws:      None.
41 //--
42 bool CMICmnLLDBProxySBValue::GetValueAsUnsigned( const lldb::SBValue & vrValue, MIuint64 & vwValue )
43 {
44         lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue );
45         bool bCompositeType = true;
46         MIuint64 nFailValue = 0;
47         MIuint64 nValue = rValue.GetValueAsUnsigned( nFailValue );
48         if( nValue == nFailValue )
49         {
50                 nFailValue = 5; // Some arbitary number
51                 nValue = rValue.GetValueAsUnsigned( nFailValue );
52                 if( nValue != nFailValue )
53                 {
54                         bCompositeType = false;
55                         vwValue = nValue;
56                 }
57         }
58         else
59         {
60                 bCompositeType = false;
61                 vwValue = nValue;
62         }
63
64         return (bCompositeType ? MIstatus::failure : MIstatus::success);
65 }
66
67 //++ ------------------------------------------------------------------------------------
68 // Details:     Retrieve the numerical value from the SBValue object. If the function fails
69 //                      it could indicate the SBValue object does not represent an internal type.
70 // Type:        Static method.
71 // Args:        vrValue - (R) The SBValue object to get a value from.
72 //                      vwValue - (W) The numerical value.
73 // Return:      MIstatus::success - Functionality succeeded.
74 //                      MIstatus::failure - Functionality failed.
75 // Throws:      None.
76 //--
77 bool CMICmnLLDBProxySBValue::GetValueAsSigned( const lldb::SBValue & vrValue, MIint64 & vwValue )
78 {
79         lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue );
80         bool bCompositeType = true;
81         MIuint64 nFailValue = 0;
82         MIuint64 nValue = rValue.GetValueAsSigned( nFailValue );
83         if( nValue == nFailValue )
84         {
85                 nFailValue = 5; // Some arbitary number
86                 nValue = rValue.GetValueAsSigned( nFailValue );
87                 if( nValue != nFailValue )
88                 {
89                         bCompositeType = false;
90                         vwValue = nValue;
91                 }
92         }
93         else
94         {
95                 bCompositeType = false;
96                 vwValue = nValue;
97         }
98
99         return (bCompositeType ? MIstatus::failure : MIstatus::success);
100 }
101
102 //++ ------------------------------------------------------------------------------------
103 // Details:     Retrieve the NUL terminated string from the SBValue object if it of the type
104 //                      unsigned char *. 
105 // Type:        Static method.
106 // Args:        vrValue         - (R) The SBValue object to get a value from.
107 //                      vwCString       - (W) The text data '\0' terminated.
108 // Return:      MIstatus::success - Functionality succeeded.
109 //                      MIstatus::failure - Functionality failed, not suitable type.
110 // Throws:      None.
111 //--
112 bool CMICmnLLDBProxySBValue::GetCString( const lldb::SBValue & vrValue, CMIUtilString & vwCString )
113 {
114         lldb::SBValue & rValue = const_cast< lldb::SBValue & >( vrValue );
115         const MIchar * pCType = rValue.GetTypeName();
116         if( pCType == nullptr )
117                 return MIstatus::failure;
118         
119         const MIchar * pType = "unsigned char *";
120         if( !CMIUtilString::Compare( pCType, pType ) )
121                 return MIstatus::failure;
122
123         const CMIUtilString strAddr( rValue.GetValue() );
124         MIint64 nNum = 0;
125         if( !strAddr.ExtractNumber( nNum ) )
126                 return MIstatus::failure;
127
128         CMICmnLLDBDebugSessionInfo & rSessionInfo( CMICmnLLDBDebugSessionInfo::Instance() );
129         lldb::SBProcess & rProcess = rSessionInfo.m_lldbProcess;
130         MIuint nBufferSize = 64;
131         bool bNeedResize = false;
132         MIchar * pBuffer = static_cast< MIchar * >( ::malloc( nBufferSize ) );
133         do
134         {
135                 lldb::SBError error;
136                 const size_t nReadSize = rProcess.ReadCStringFromMemory( (lldb::addr_t) nNum, pBuffer, nBufferSize, error );
137                 if( nReadSize == (nBufferSize - 1) )
138                 {
139                         bNeedResize = true;
140                         nBufferSize  = nBufferSize << 1;
141                         pBuffer = static_cast< MIchar * >( ::realloc( pBuffer, nBufferSize ) );
142                 }
143                 else
144                         bNeedResize = false;
145         } 
146         while( bNeedResize );
147
148         vwCString = pBuffer;
149         free( (void *) pBuffer );
150
151         return MIstatus::success;
152 }
153