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#ifndef V8_DEBUG_WASM_GDB_SERVER_GDB_REMOTE_UTIL_H_
61cb0ef41Sopenharmony_ci#define V8_DEBUG_WASM_GDB_SERVER_GDB_REMOTE_UTIL_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <string>
91cb0ef41Sopenharmony_ci#include <vector>
101cb0ef41Sopenharmony_ci#include "src/flags/flags.h"
111cb0ef41Sopenharmony_ci#include "src/utils/utils.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_cinamespace wasm {
161cb0ef41Sopenharmony_cinamespace gdb_server {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci#define TRACE_GDB_REMOTE(...)                                            \
191cb0ef41Sopenharmony_ci  do {                                                                   \
201cb0ef41Sopenharmony_ci    if (FLAG_trace_wasm_gdb_remote) PrintF("[gdb-remote] " __VA_ARGS__); \
211cb0ef41Sopenharmony_ci  } while (false)
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Convert from 0-255 to a pair of ASCII chars (0-9,a-f).
241cb0ef41Sopenharmony_civoid UInt8ToHex(uint8_t byte, char chars[2]);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Convert a pair of hex chars into a value 0-255 or return false if either
271cb0ef41Sopenharmony_ci// input character is not a valid nibble.
281cb0ef41Sopenharmony_cibool HexToUInt8(const char chars[2], uint8_t* byte);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// Convert from ASCII (0-9,a-f,A-F) to 4b unsigned or return false if the
311cb0ef41Sopenharmony_ci// input char is unexpected.
321cb0ef41Sopenharmony_cibool NibbleToUInt8(char ch, uint8_t* byte);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cistd::vector<std::string> V8_EXPORT_PRIVATE StringSplit(const std::string& instr,
351cb0ef41Sopenharmony_ci                                                       const char* delim);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// Convert the memory pointed to by {mem} into a hex string in GDB-remote
381cb0ef41Sopenharmony_ci// format.
391cb0ef41Sopenharmony_cistd::string Mem2Hex(const uint8_t* mem, size_t count);
401cb0ef41Sopenharmony_cistd::string Mem2Hex(const std::string& str);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// For LLDB debugging, an address in a Wasm module code space is represented
431cb0ef41Sopenharmony_ci// with 64 bits, where the first 32 bits identify the module id:
441cb0ef41Sopenharmony_ci// +--------------------+--------------------+
451cb0ef41Sopenharmony_ci// |     module_id      |       offset       |
461cb0ef41Sopenharmony_ci// +--------------------+--------------------+
471cb0ef41Sopenharmony_ci//  <----- 32 bit -----> <----- 32 bit ----->
481cb0ef41Sopenharmony_ciclass wasm_addr_t {
491cb0ef41Sopenharmony_ci public:
501cb0ef41Sopenharmony_ci  wasm_addr_t(uint32_t module_id, uint32_t offset)
511cb0ef41Sopenharmony_ci      : module_id_(module_id), offset_(offset) {}
521cb0ef41Sopenharmony_ci  explicit wasm_addr_t(uint64_t address)
531cb0ef41Sopenharmony_ci      : module_id_(address >> 32), offset_(address & 0xffffffff) {}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  inline uint32_t ModuleId() const { return module_id_; }
561cb0ef41Sopenharmony_ci  inline uint32_t Offset() const { return offset_; }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  inline operator uint64_t() const {
591cb0ef41Sopenharmony_ci    return static_cast<uint64_t>(module_id_) << 32 | offset_;
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci private:
631cb0ef41Sopenharmony_ci  uint32_t module_id_;
641cb0ef41Sopenharmony_ci  uint32_t offset_;
651cb0ef41Sopenharmony_ci};
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci}  // namespace gdb_server
681cb0ef41Sopenharmony_ci}  // namespace wasm
691cb0ef41Sopenharmony_ci}  // namespace internal
701cb0ef41Sopenharmony_ci}  // namespace v8
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci#endif  // V8_DEBUG_WASM_GDB_SERVER_GDB_REMOTE_UTIL_H_
73