blob: 5884abc6b8f6041cc15c0f873a52e456d2b7447a [file] [log] [blame]
Deepak Panickal6f9c4682014-05-16 10:51:01 +00001//===-- 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//++
Zachary Turner1d6af022014-11-17 18:06:21 +000011// File: MICmnLLDBProxySBValue.cpp
Deepak Panickal6f9c4682014-05-16 10:51:01 +000012//
Zachary Turner1d6af022014-11-17 18:06:21 +000013// Overview: CMICmnLLDBProxySBValue implementation.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000014//
Zachary Turner1d6af022014-11-17 18:06:21 +000015// Environment: Compilers: Visual C++ 12.
16// gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17// Libraries: See MIReadmetxt.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000018//
Zachary Turner1d6af022014-11-17 18:06:21 +000019// Copyright: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000020//--
21
Deepak Panickal7f6edd52014-08-08 17:39:47 +000022#include <stdlib.h>
23
Deepak Panickal6f9c4682014-05-16 10:51:01 +000024// Third Party Headers:
Greg Claytonee3626e2015-01-20 00:04:26 +000025#include "lldb/API/SBError.h"
Deepak Panickal6f9c4682014-05-16 10:51:01 +000026
27// In-house headers:
28#include "MICmnLLDBProxySBValue.h"
29#include "MIUtilString.h"
30#include "MICmnLLDBDebugSessionInfo.h"
31
32//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000033// 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.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000041//--
Zachary Turner1d6af022014-11-17 18:06:21 +000042bool
43CMICmnLLDBProxySBValue::GetValueAsUnsigned(const lldb::SBValue &vrValue, MIuint64 &vwValue)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000044{
Zachary Turner1d6af022014-11-17 18:06:21 +000045 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
46 bool bCompositeType = true;
47 MIuint64 nFailValue = 0;
48 MIuint64 nValue = rValue.GetValueAsUnsigned(nFailValue);
49 if (nValue == nFailValue)
50 {
51 nFailValue = 5; // Some arbitary number
52 nValue = rValue.GetValueAsUnsigned(nFailValue);
53 if (nValue != nFailValue)
54 {
55 bCompositeType = false;
56 vwValue = nValue;
57 }
58 }
59 else
60 {
61 bCompositeType = false;
62 vwValue = nValue;
63 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +000064
Zachary Turner1d6af022014-11-17 18:06:21 +000065 return (bCompositeType ? MIstatus::failure : MIstatus::success);
Deepak Panickal6f9c4682014-05-16 10:51:01 +000066}
67
68//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000069// Details: Retrieve the numerical value from the SBValue object. If the function fails
70// it could indicate the SBValue object does not represent an internal type.
71// Type: Static method.
72// Args: vrValue - (R) The SBValue object to get a value from.
73// vwValue - (W) The numerical value.
74// Return: MIstatus::success - Functionality succeeded.
75// MIstatus::failure - Functionality failed.
76// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000077//--
Zachary Turner1d6af022014-11-17 18:06:21 +000078bool
79CMICmnLLDBProxySBValue::GetValueAsSigned(const lldb::SBValue &vrValue, MIint64 &vwValue)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000080{
Zachary Turner1d6af022014-11-17 18:06:21 +000081 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
82 bool bCompositeType = true;
83 MIuint64 nFailValue = 0;
84 MIuint64 nValue = rValue.GetValueAsSigned(nFailValue);
85 if (nValue == nFailValue)
86 {
87 nFailValue = 5; // Some arbitary number
88 nValue = rValue.GetValueAsSigned(nFailValue);
89 if (nValue != nFailValue)
90 {
91 bCompositeType = false;
92 vwValue = nValue;
93 }
94 }
95 else
96 {
97 bCompositeType = false;
98 vwValue = nValue;
99 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000100
Zachary Turner1d6af022014-11-17 18:06:21 +0000101 return (bCompositeType ? MIstatus::failure : MIstatus::success);
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000102}
103
104//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000105// Details: Retrieve the NUL terminated string from the SBValue object if it of the type
106// unsigned char *.
107// Type: Static method.
108// Args: vrValue - (R) The SBValue object to get a value from.
109// vwCString - (W) The text data '\0' terminated.
110// Return: MIstatus::success - Functionality succeeded.
111// MIstatus::failure - Functionality failed, not suitable type.
112// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000113//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000114bool
115CMICmnLLDBProxySBValue::GetCString(const lldb::SBValue &vrValue, CMIUtilString &vwCString)
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000116{
Zachary Turner1d6af022014-11-17 18:06:21 +0000117 lldb::SBValue &rValue = const_cast<lldb::SBValue &>(vrValue);
118 const MIchar *pCType = rValue.GetTypeName();
119 if (pCType == nullptr)
120 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000121
Zachary Turner1d6af022014-11-17 18:06:21 +0000122 const MIchar *pType = "unsigned char *";
123 if (!CMIUtilString::Compare(pCType, pType))
124 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000125
Zachary Turner1d6af022014-11-17 18:06:21 +0000126 const CMIUtilString strAddr(rValue.GetValue());
127 MIint64 nNum = 0;
128 if (!strAddr.ExtractNumber(nNum))
129 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000130
Zachary Turner1d6af022014-11-17 18:06:21 +0000131 CMICmnLLDBDebugSessionInfo &rSessionInfo(CMICmnLLDBDebugSessionInfo::Instance());
132 lldb::SBProcess &rProcess = rSessionInfo.m_lldbProcess;
133 MIuint nBufferSize = 64;
134 bool bNeedResize = false;
135 MIchar *pBuffer = static_cast<MIchar *>(::malloc(nBufferSize));
136 do
137 {
138 lldb::SBError error;
139 const size_t nReadSize = rProcess.ReadCStringFromMemory((lldb::addr_t)nNum, pBuffer, nBufferSize, error);
140 if (nReadSize == (nBufferSize - 1))
141 {
142 bNeedResize = true;
143 nBufferSize = nBufferSize << 1;
144 pBuffer = static_cast<MIchar *>(::realloc(pBuffer, nBufferSize));
145 }
146 else
147 bNeedResize = false;
148 } while (bNeedResize);
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000149
Zachary Turner1d6af022014-11-17 18:06:21 +0000150 vwCString = pBuffer;
151 free((void *)pBuffer);
152
153 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000154}