| 1 |
//===-- MessageObjects.cpp ------------------------------------------------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "MessageObjects.h" |
| 10 |
#include "lldb/Utility/Args.h" |
| 11 |
#include "lldb/Utility/StringExtractor.h" |
| 12 |
#include "llvm/ADT/StringExtras.h" |
| 13 |
#include "gtest/gtest.h" |
| 14 |
|
| 15 |
using namespace lldb_private; |
| 16 |
using namespace lldb; |
| 17 |
using namespace llvm; |
| 18 |
namespace llgs_tests { |
| 19 |
|
| 20 |
Expected<ProcessInfo> ProcessInfo::create(StringRef response) { |
| 21 |
ProcessInfo process_info; |
| 22 |
auto elements_or_error = SplitUniquePairList("ProcessInfo", response); |
| 23 |
if (!elements_or_error) |
| 24 |
return elements_or_error.takeError(); |
| 25 |
|
| 26 |
auto &elements = *elements_or_error; |
| 27 |
if (elements["pid"].getAsInteger(16, process_info.m_pid)) |
| 28 |
return make_parsing_error("ProcessInfo: pid"); |
| 29 |
if (elements["parent-pid"].getAsInteger(16, process_info.m_parent_pid)) |
| 30 |
return make_parsing_error("ProcessInfo: parent-pid"); |
| 31 |
if (elements["real-uid"].getAsInteger(16, process_info.m_real_uid)) |
| 32 |
return make_parsing_error("ProcessInfo: real-uid"); |
| 33 |
if (elements["real-gid"].getAsInteger(16, process_info.m_real_gid)) |
| 34 |
return make_parsing_error("ProcessInfo: real-uid"); |
| 35 |
if (elements["effective-uid"].getAsInteger(16, process_info.m_effective_uid)) |
| 36 |
return make_parsing_error("ProcessInfo: effective-uid"); |
| 37 |
if (elements["effective-gid"].getAsInteger(16, process_info.m_effective_gid)) |
| 38 |
return make_parsing_error("ProcessInfo: effective-gid"); |
| 39 |
if (elements["ptrsize"].getAsInteger(10, process_info.m_ptrsize)) |
| 40 |
return make_parsing_error("ProcessInfo: ptrsize"); |
| 41 |
|
| 42 |
process_info.m_triple = fromHex(elements["triple"]); |
| 43 |
StringRef endian_str = elements["endian"]; |
| 44 |
if (endian_str == "little") |
| 45 |
process_info.m_endian = support::little; |
| 46 |
else if (endian_str == "big") |
| 47 |
process_info.m_endian = support::big; |
| 48 |
else |
| 49 |
return make_parsing_error("ProcessInfo: endian"); |
| 50 |
|
| 51 |
return process_info; |
| 52 |
} |
| 53 |
|
| 54 |
lldb::pid_t ProcessInfo::GetPid() const { return m_pid; } |
| 55 |
|
| 56 |
support::endianness ProcessInfo::GetEndian() const { return m_endian; } |
| 57 |
|
| 58 |
//====== ThreadInfo ============================================================ |
| 59 |
ThreadInfo::ThreadInfo(StringRef name, StringRef reason, RegisterMap registers, |
| 60 |
unsigned int) |
| 61 |
: m_name(name.str()), m_reason(reason.str()), |
| 62 |
m_registers(std::move(registers)) {} |
| 63 |
|
| 64 |
const RegisterValue *ThreadInfo::ReadRegister(unsigned int Id) const { |
| 65 |
auto Iter = m_registers.find(Id); |
| 66 |
return Iter == m_registers.end() ? nullptr : &Iter->getSecond(); |
| 67 |
} |
| 68 |
|
| 69 |
//====== JThreadsInfo ========================================================== |
| 70 |
|
| 71 |
Expected<RegisterMap> |
| 72 |
JThreadsInfo::parseRegisters(const StructuredData::Dictionary &Dict, |
| 73 |
ArrayRef<RegisterInfo> RegInfos) { |
| 74 |
RegisterMap Result; |
| 75 |
|
| 76 |
auto KeysObj = Dict.GetKeys(); |
| 77 |
auto Keys = KeysObj->GetAsArray(); |
| 78 |
for (size_t i = 0; i < Keys->GetSize(); i++) { |
| 79 |
StringRef KeyStr, ValueStr; |
| 80 |
Keys->GetItemAtIndexAsString(i, KeyStr); |
| 81 |
Dict.GetValueForKeyAsString(KeyStr, ValueStr); |
| 82 |
unsigned int Register; |
| 83 |
if (!llvm::to_integer(KeyStr, Register, 10)) |
| 84 |
return make_parsing_error("JThreadsInfo: register key[{0}]", i); |
| 85 |
|
| 86 |
auto RegValOr = |
| 87 |
parseRegisterValue(RegInfos[Register], ValueStr, support::big); |
| 88 |
if (!RegValOr) |
| 89 |
return RegValOr.takeError(); |
| 90 |
Result[Register] = std::move(*RegValOr); |
| 91 |
} |
| 92 |
return std::move(Result); |
| 93 |
} |
| 94 |
|
| 95 |
Expected<JThreadsInfo> JThreadsInfo::create(StringRef Response, |
| 96 |
ArrayRef<RegisterInfo> RegInfos) { |
| 97 |
JThreadsInfo jthreads_info; |
| 98 |
|
| 99 |
StructuredData::ObjectSP json = |
| 100 |
StructuredData::ParseJSON(std::string(Response)); |
| 101 |
StructuredData::Array *array = json->GetAsArray(); |
| 102 |
if (!array) |
| 103 |
return make_parsing_error("JThreadsInfo: JSON array"); |
| 104 |
|
| 105 |
for (size_t i = 0; i < array->GetSize(); i++) { |
| 106 |
StructuredData::Dictionary *thread_info; |
| 107 |
array->GetItemAtIndexAsDictionary(i, thread_info); |
| 108 |
if (!thread_info) |
| 109 |
return make_parsing_error("JThreadsInfo: JSON obj at {0}", i); |
| 110 |
|
| 111 |
StringRef name, reason; |
| 112 |
thread_info->GetValueForKeyAsString("name", name); |
| 113 |
thread_info->GetValueForKeyAsString("reason", reason); |
| 114 |
uint64_t signal; |
| 115 |
thread_info->GetValueForKeyAsInteger("signal", signal); |
| 116 |
uint64_t tid; |
| 117 |
thread_info->GetValueForKeyAsInteger("tid", tid); |
| 118 |
|
| 119 |
StructuredData::Dictionary *register_dict; |
| 120 |
thread_info->GetValueForKeyAsDictionary("registers", register_dict); |
| 121 |
if (!register_dict) |
| 122 |
return make_parsing_error("JThreadsInfo: registers JSON obj"); |
| 123 |
|
| 124 |
auto RegsOr = parseRegisters(*register_dict, RegInfos); |
| 125 |
if (!RegsOr) |
| 126 |
return RegsOr.takeError(); |
| 127 |
jthreads_info.m_thread_infos[tid] = |
| 128 |
ThreadInfo(name, reason, std::move(*RegsOr), signal); |
| 129 |
} |
| 130 |
|
| 131 |
return jthreads_info; |
| 132 |
} |
| 133 |
|
| 134 |
const ThreadInfoMap &JThreadsInfo::GetThreadInfos() const { |
| 135 |
return m_thread_infos; |
| 136 |
} |
| 137 |
|
| 138 |
Expected<RegisterInfo> RegisterInfoParser::create(StringRef Response) { |
| 139 |
auto ElementsOr = SplitUniquePairList("RegisterInfoParser", Response); |
| 140 |
if (!ElementsOr) |
| 141 |
return ElementsOr.takeError(); |
| 142 |
auto &Elements = *ElementsOr; |
| 143 |
|
| 144 |
RegisterInfo Info = { |
| 145 |
nullptr, // Name |
| 146 |
nullptr, // Alt name |
| 147 |
0, // byte size |
| 148 |
0, // offset |
| 149 |
eEncodingUint, // encoding |
| 150 |
eFormatHex, // format |
| 151 |
{ |
| 152 |
LLDB_INVALID_REGNUM, // eh_frame reg num |
| 153 |
LLDB_INVALID_REGNUM, // DWARF reg num |
| 154 |
LLDB_INVALID_REGNUM, // generic reg num |
| 155 |
LLDB_INVALID_REGNUM, // process plugin reg num |
| 156 |
LLDB_INVALID_REGNUM // native register number |
| 157 |
}, |
| 158 |
nullptr, |
| 159 |
nullptr, |
| 160 |
}; |
| 161 |
Info.name = ConstString(Elements["name"]).GetCString(); |
| 162 |
if (!Info.name) |
| 163 |
return make_parsing_error("qRegisterInfo: name"); |
| 164 |
|
| 165 |
Info.alt_name = ConstString(Elements["alt-name"]).GetCString(); |
| 166 |
|
| 167 |
if (!to_integer(Elements["bitsize"], Info.byte_size, 10)) |
| 168 |
return make_parsing_error("qRegisterInfo: bit-size"); |
| 169 |
Info.byte_size /= CHAR_BIT; |
| 170 |
|
| 171 |
if (!to_integer(Elements["offset"], Info.byte_offset, 10)) |
| 172 |
Info.byte_offset = LLDB_INVALID_INDEX32; |
| 173 |
|
| 174 |
Info.encoding = Args::StringToEncoding(Elements["encoding"]); |
| 175 |
if (Info.encoding == eEncodingInvalid) |
| 176 |
return make_parsing_error("qRegisterInfo: encoding"); |
| 177 |
|
| 178 |
Info.format = StringSwitch<Format>(Elements["format"]) |
| 179 |
.Case("binary", eFormatBinary) |
| 180 |
.Case("decimal", eFormatDecimal) |
| 181 |
.Case("hex", eFormatHex) |
| 182 |
.Case("float", eFormatFloat) |
| 183 |
.Case("vector-sint8", eFormatVectorOfSInt8) |
| 184 |
.Case("vector-uint8", eFormatVectorOfUInt8) |
| 185 |
.Case("vector-sint16", eFormatVectorOfSInt16) |
| 186 |
.Case("vector-uint16", eFormatVectorOfUInt16) |
| 187 |
.Case("vector-sint32", eFormatVectorOfSInt32) |
| 188 |
.Case("vector-uint32", eFormatVectorOfUInt32) |
| 189 |
.Case("vector-float32", eFormatVectorOfFloat32) |
| 190 |
.Case("vector-uint64", eFormatVectorOfUInt64) |
| 191 |
.Case("vector-uint128", eFormatVectorOfUInt128) |
| 192 |
.Default(eFormatInvalid); |
| 193 |
if (Info.format == eFormatInvalid) |
| 194 |
return make_parsing_error("qRegisterInfo: format"); |
| 195 |
|
| 196 |
Info.kinds[eRegisterKindGeneric] = |
| 197 |
Args::StringToGenericRegister(Elements["generic"]); |
| 198 |
|
| 199 |
return std::move(Info); |
| 200 |
} |
| 201 |
|
| 202 |
Expected<RegisterValue> parseRegisterValue(const RegisterInfo &Info, |
| 203 |
StringRef HexValue, |
| 204 |
llvm::support::endianness Endian, |
| 205 |
bool ZeroPad) { |
| 206 |
SmallString<128> Storage; |
| 207 |
if (ZeroPad && HexValue.size() < Info.byte_size * 2) { |
| 208 |
Storage.insert(Storage.begin(), Info.byte_size * 2 - HexValue.size(), '0'); |
| 209 |
Storage += HexValue; |
| 210 |
HexValue = Storage; |
| 211 |
} |
| 212 |
|
| 213 |
SmallVector<uint8_t, 64> Bytes(HexValue.size() / 2); |
| 214 |
StringExtractor(HexValue).GetHexBytes(Bytes, '\xcc'); |
| 215 |
RegisterValue Value; |
| 216 |
Status ST; |
| 217 |
Value.SetFromMemoryData( |
| 218 |
Info, Bytes.data(), Bytes.size(), |
| 219 |
Endian == support::little ? eByteOrderLittle : eByteOrderBig, ST); |
| 220 |
if (ST.Fail()) |
| 221 |
return ST.ToError(); |
| 222 |
return Value; |
| 223 |
} |
| 224 |
|
| 225 |
//====== StopReply ============================================================= |
| 226 |
Expected<std::unique_ptr<StopReply>> |
| 227 |
StopReply::create(StringRef Response, llvm::support::endianness Endian, |
| 228 |
ArrayRef<RegisterInfo> RegInfos) { |
| 229 |
if (Response.size() < 3) |
| 230 |
return make_parsing_error("StopReply: Invalid packet"); |
| 231 |
if (Response.consume_front("T")) |
| 232 |
return StopReplyStop::create(Response, Endian, RegInfos); |
| 233 |
if (Response.consume_front("W")) |
| 234 |
return StopReplyExit::create(Response); |
| 235 |
return make_parsing_error("StopReply: Invalid packet"); |
| 236 |
} |
| 237 |
|
| 238 |
Expected<RegisterMap> StopReplyStop::parseRegisters( |
| 239 |
const StringMap<SmallVector<StringRef, 2>> &Elements, |
| 240 |
support::endianness Endian, ArrayRef<lldb_private::RegisterInfo> RegInfos) { |
| 241 |
|
| 242 |
RegisterMap Result; |
| 243 |
for (const auto &E : Elements) { |
| 244 |
StringRef Key = E.getKey(); |
| 245 |
const auto &Val = E.getValue(); |
| 246 |
if (Key.size() != 2) |
| 247 |
continue; |
| 248 |
|
| 249 |
unsigned int Reg; |
| 250 |
if (!to_integer(Key, Reg, 16)) |
| 251 |
continue; |
| 252 |
|
| 253 |
if (Val.size() != 1) |
| 254 |
return make_parsing_error( |
| 255 |
"StopReplyStop: multiple entries for register field [{0:x}]", Reg); |
| 256 |
|
| 257 |
auto RegValOr = parseRegisterValue(RegInfos[Reg], Val[0], Endian); |
| 258 |
if (!RegValOr) |
| 259 |
return RegValOr.takeError(); |
| 260 |
Result[Reg] = std::move(*RegValOr); |
| 261 |
} |
| 262 |
return std::move(Result); |
| 263 |
} |
| 264 |
|
| 265 |
Expected<std::unique_ptr<StopReplyStop>> |
| 266 |
StopReplyStop::create(StringRef Response, support::endianness Endian, |
| 267 |
ArrayRef<RegisterInfo> RegInfos) { |
| 268 |
unsigned int Signal; |
| 269 |
StringRef SignalStr = Response.take_front(2); |
| 270 |
Response = Response.drop_front(2); |
| 271 |
if (!to_integer(SignalStr, Signal, 16)) |
| 272 |
return make_parsing_error("StopReply: stop signal"); |
| 273 |
|
| 274 |
auto Elements = SplitPairList(Response); |
| 275 |
for (StringRef Field : |
| 276 |
{"name", "reason", "thread", "threads", "thread-pcs"}) { |
| 277 |
// This will insert an empty field if there is none. In the future, we |
| 278 |
// should probably differentiate between these fields not being present and |
| 279 |
// them being empty, but right now no tests depends on this. |
| 280 |
if (Elements.insert({Field, {""}}).first->second.size() != 1) |
| 281 |
return make_parsing_error( |
| 282 |
"StopReply: got multiple responses for the {0} field", Field); |
| 283 |
} |
| 284 |
StringRef Name = Elements["name"][0]; |
| 285 |
StringRef Reason = Elements["reason"][0]; |
| 286 |
|
| 287 |
lldb::tid_t Thread; |
| 288 |
if (!to_integer(Elements["thread"][0], Thread, 16)) |
| 289 |
return make_parsing_error("StopReply: thread"); |
| 290 |
|
| 291 |
SmallVector<StringRef, 20> Threads; |
| 292 |
SmallVector<StringRef, 20> Pcs; |
| 293 |
Elements["threads"][0].split(Threads, ','); |
| 294 |
Elements["thread-pcs"][0].split(Pcs, ','); |
| 295 |
if (Threads.size() != Pcs.size()) |
| 296 |
return make_parsing_error("StopReply: thread/PC count mismatch"); |
| 297 |
|
| 298 |
RegisterMap ThreadPcs; |
| 299 |
const RegisterInfo *PcInfo = find_if(RegInfos, [](const RegisterInfo &Info) { |
| 300 |
return Info.kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_PC; |
| 301 |
}); |
| 302 |
assert(PcInfo); |
| 303 |
|
| 304 |
for (auto ThreadPc : zip(Threads, Pcs)) { |
| 305 |
lldb::tid_t Id; |
| 306 |
if (!to_integer(std::get<0>(ThreadPc), Id, 16)) |
| 307 |
return make_parsing_error("StopReply: Thread id '{0}'", |
| 308 |
std::get<0>(ThreadPc)); |
| 309 |
|
| 310 |
auto PcOr = parseRegisterValue(*PcInfo, std::get<1>(ThreadPc), Endian, |
| 311 |
/*ZeroPad*/ true); |
| 312 |
if (!PcOr) |
| 313 |
return PcOr.takeError(); |
| 314 |
ThreadPcs[Id] = std::move(*PcOr); |
| 315 |
} |
| 316 |
|
| 317 |
auto RegistersOr = parseRegisters(Elements, Endian, RegInfos); |
| 318 |
if (!RegistersOr) |
| 319 |
return RegistersOr.takeError(); |
| 320 |
|
| 321 |
return std::make_unique<StopReplyStop>(Signal, Thread, Name, |
| 322 |
std::move(ThreadPcs), |
| 323 |
std::move(*RegistersOr), Reason); |
| 324 |
} |
| 325 |
|
| 326 |
Expected<std::unique_ptr<StopReplyExit>> |
| 327 |
StopReplyExit::create(StringRef Response) { |
| 328 |
uint8_t Status; |
| 329 |
if (!to_integer(Response, Status, 16)) |
| 330 |
return make_parsing_error("StopReply: exit status"); |
| 331 |
return std::make_unique<StopReplyExit>(Status); |
| 332 |
} |
| 333 |
|
| 334 |
//====== Globals =============================================================== |
| 335 |
Expected<StringMap<StringRef>> SplitUniquePairList(StringRef caller, |
| 336 |
StringRef str) { |
| 337 |
SmallVector<StringRef, 20> elements; |
| 338 |
str.split(elements, ';'); |
| 339 |
|
| 340 |
StringMap<StringRef> pairs; |
| 341 |
for (StringRef s : elements) { |
| 342 |
std::pair<StringRef, StringRef> pair = s.split(':'); |
| 343 |
if (pairs.count(pair.first)) |
| 344 |
return make_parsing_error("{0}: Duplicate Key: {1}", caller, pair.first); |
| 345 |
|
| 346 |
pairs.insert(pair); |
| 347 |
} |
| 348 |
|
| 349 |
return pairs; |
| 350 |
} |
| 351 |
|
| 352 |
StringMap<SmallVector<StringRef, 2>> SplitPairList(StringRef str) { |
| 353 |
SmallVector<StringRef, 20> elements; |
| 354 |
str.split(elements, ';'); |
| 355 |
|
| 356 |
StringMap<SmallVector<StringRef, 2>> pairs; |
| 357 |
for (StringRef s : elements) { |
| 358 |
std::pair<StringRef, StringRef> pair = s.split(':'); |
| 359 |
pairs[pair.first].push_back(pair.second); |
| 360 |
} |
| 361 |
|
| 362 |
return pairs; |
| 363 |
} |
| 364 |
} // namespace llgs_tests |
| 365 |
|
| 366 |
std::ostream &lldb_private::operator<<(std::ostream &OS, |
| 367 |
const RegisterValue &RegVal) { |
| 368 |
ArrayRef<uint8_t> Bytes(static_cast<const uint8_t *>(RegVal.GetBytes()), |
| 369 |
RegVal.GetByteSize()); |
| 370 |
return OS << formatv("RegisterValue[{0}]: {1:@[x-2]}", RegVal.GetByteSize(), |
| 371 |
make_range(Bytes.begin(), Bytes.end())) |
| 372 |
.str(); |
| 373 |
} |