11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#include "src/debug/wasm/gdb-server/gdb-remote-util.h" 61cb0ef41Sopenharmony_ciusing std::string; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_cinamespace v8 { 91cb0ef41Sopenharmony_cinamespace internal { 101cb0ef41Sopenharmony_cinamespace wasm { 111cb0ef41Sopenharmony_cinamespace gdb_server { 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci// GDB expects lower case values. 141cb0ef41Sopenharmony_cistatic const char kHexChars[] = "0123456789abcdef"; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_civoid UInt8ToHex(uint8_t byte, char chars[2]) { 171cb0ef41Sopenharmony_ci DCHECK(chars); 181cb0ef41Sopenharmony_ci chars[0] = kHexChars[byte >> 4]; 191cb0ef41Sopenharmony_ci chars[1] = kHexChars[byte & 0xF]; 201cb0ef41Sopenharmony_ci} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cibool HexToUInt8(const char chars[2], uint8_t* byte) { 231cb0ef41Sopenharmony_ci uint8_t o1, o2; 241cb0ef41Sopenharmony_ci if (NibbleToUInt8(chars[0], &o1) && NibbleToUInt8(chars[1], &o2)) { 251cb0ef41Sopenharmony_ci *byte = (o1 << 4) + o2; 261cb0ef41Sopenharmony_ci return true; 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci return false; 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cibool NibbleToUInt8(char ch, uint8_t* byte) { 331cb0ef41Sopenharmony_ci DCHECK(byte); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci // Check for nibble of a-f 361cb0ef41Sopenharmony_ci if ((ch >= 'a') && (ch <= 'f')) { 371cb0ef41Sopenharmony_ci *byte = (ch - 'a' + 10); 381cb0ef41Sopenharmony_ci return true; 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci // Check for nibble of A-F 421cb0ef41Sopenharmony_ci if ((ch >= 'A') && (ch <= 'F')) { 431cb0ef41Sopenharmony_ci *byte = (ch - 'A' + 10); 441cb0ef41Sopenharmony_ci return true; 451cb0ef41Sopenharmony_ci } 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci // Check for nibble of 0-9 481cb0ef41Sopenharmony_ci if ((ch >= '0') && (ch <= '9')) { 491cb0ef41Sopenharmony_ci *byte = (ch - '0'); 501cb0ef41Sopenharmony_ci return true; 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci // Not a valid nibble representation 541cb0ef41Sopenharmony_ci return false; 551cb0ef41Sopenharmony_ci} 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cistd::vector<std::string> StringSplit(const string& instr, const char* delim) { 581cb0ef41Sopenharmony_ci std::vector<std::string> result; 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci const char* in = instr.data(); 611cb0ef41Sopenharmony_ci if (nullptr == in) return result; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci // Check if we have nothing to do 641cb0ef41Sopenharmony_ci if (nullptr == delim) { 651cb0ef41Sopenharmony_ci result.push_back(string(in)); 661cb0ef41Sopenharmony_ci return result; 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci while (*in) { 701cb0ef41Sopenharmony_ci // Toss all preceeding delimiters 711cb0ef41Sopenharmony_ci while (*in && strchr(delim, *in)) in++; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci // If we still have something to process 741cb0ef41Sopenharmony_ci if (*in) { 751cb0ef41Sopenharmony_ci const char* start = in; 761cb0ef41Sopenharmony_ci size_t len = 0; 771cb0ef41Sopenharmony_ci // Keep moving forward for all valid chars 781cb0ef41Sopenharmony_ci while (*in && (strchr(delim, *in) == nullptr)) { 791cb0ef41Sopenharmony_ci len++; 801cb0ef41Sopenharmony_ci in++; 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci // Build this token and add it to the array. 841cb0ef41Sopenharmony_ci result.push_back(string{start, len}); 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci return result; 881cb0ef41Sopenharmony_ci} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_cistd::string Mem2Hex(const uint8_t* mem, size_t count) { 911cb0ef41Sopenharmony_ci std::vector<char> result(count * 2 + 1); 921cb0ef41Sopenharmony_ci for (size_t i = 0; i < count; i++) UInt8ToHex(*mem++, &result[i * 2]); 931cb0ef41Sopenharmony_ci result[count * 2] = '\0'; 941cb0ef41Sopenharmony_ci return result.data(); 951cb0ef41Sopenharmony_ci} 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_cistd::string Mem2Hex(const std::string& str) { 981cb0ef41Sopenharmony_ci return Mem2Hex(reinterpret_cast<const uint8_t*>(str.data()), str.size()); 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci} // namespace gdb_server 1021cb0ef41Sopenharmony_ci} // namespace wasm 1031cb0ef41Sopenharmony_ci} // namespace internal 1041cb0ef41Sopenharmony_ci} // namespace v8 105