1 //===-- MICmnLLDBProxySBValue.cpp -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // Third Party Headers:
12 #include "lldb/API/SBError.h"
15 #include "MICmnLLDBDebugSessionInfo.h"
16 #include "MICmnLLDBProxySBValue.h"
17 #include "MIUtilString.h"
20 // Details: Retrieve the numerical value from the SBValue object. If the
22 // it could indicate the SBValue object does not represent an internal
24 // Type: Static method.
25 // Args: vrValue - (R) The SBValue object to get a value from.
26 // vwValue - (W) The numerical value.
27 // Return: MIstatus::success - Functionality succeeded.
28 // MIstatus::failure - Functionality failed.
31 bool CMICmnLLDBProxySBValue::GetValueAsUnsigned(const lldb::SBValue &vrValue,
33 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
34 bool bCompositeType = true;
35 MIuint64 nFailValue = 0;
36 MIuint64 nValue = rValue.GetValueAsUnsigned(nFailValue);
37 if (nValue == nFailValue) {
38 nFailValue = 5; // Some arbitrary number
39 nValue = rValue.GetValueAsUnsigned(nFailValue);
40 if (nValue != nFailValue) {
41 bCompositeType = false;
45 bCompositeType = false;
49 return (bCompositeType ? MIstatus::failure : MIstatus::success);
53 // Details: Retrieve the numerical value from the SBValue object. If the
55 // it could indicate the SBValue object does not represent an internal
57 // Type: Static method.
58 // Args: vrValue - (R) The SBValue object to get a value from.
59 // vwValue - (W) The numerical value.
60 // Return: MIstatus::success - Functionality succeeded.
61 // MIstatus::failure - Functionality failed.
64 bool CMICmnLLDBProxySBValue::GetValueAsSigned(const lldb::SBValue &vrValue,
66 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
67 bool bCompositeType = true;
68 MIuint64 nFailValue = 0;
69 MIuint64 nValue = rValue.GetValueAsSigned(nFailValue);
70 if (nValue == nFailValue) {
71 nFailValue = 5; // Some arbitrary number
72 nValue = rValue.GetValueAsSigned(nFailValue);
73 if (nValue != nFailValue) {
74 bCompositeType = false;
78 bCompositeType = false;
82 return (bCompositeType ? MIstatus::failure : MIstatus::success);
86 // Details: Retrieve the NUL terminated string from the SBValue object if it of
89 // Type: Static method.
90 // Args: vrValue - (R) The SBValue object to get a value from.
91 // vwCString - (W) The text data '\0' terminated.
92 // Return: MIstatus::success - Functionality succeeded.
93 // MIstatus::failure - Functionality failed, not suitable type.
96 bool CMICmnLLDBProxySBValue::GetCString(const lldb::SBValue &vrValue,
97 CMIUtilString &vwCString) {
98 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
99 const char *pCType = rValue.GetTypeName();
100 if (pCType == nullptr)
101 return MIstatus::failure;
103 const char *pType = "unsigned char *";
104 if (!CMIUtilString::Compare(pCType, pType))
105 return MIstatus::failure;
107 const CMIUtilString strAddr(rValue.GetValue());
109 if (!strAddr.ExtractNumber(nNum))
110 return MIstatus::failure;
112 CMICmnLLDBDebugSessionInfo &rSessionInfo(
113 CMICmnLLDBDebugSessionInfo::Instance());
114 lldb::SBProcess sbProcess = rSessionInfo.GetProcess();
115 MIuint nBufferSize = 64;
116 bool bNeedResize = false;
117 char *pBuffer = static_cast<char *>(::malloc(nBufferSize));
120 const size_t nReadSize = sbProcess.ReadCStringFromMemory(
121 (lldb::addr_t)nNum, pBuffer, nBufferSize, error);
122 if (nReadSize == (nBufferSize - 1)) {
124 nBufferSize = nBufferSize << 1;
125 pBuffer = static_cast<char *>(::realloc(pBuffer, nBufferSize));
128 } while (bNeedResize);
131 free((void *)pBuffer);
133 return MIstatus::success;