1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/wasm/memory-tracing.h"
6
7#include <cinttypes>
8
9#include "src/base/memory.h"
10#include "src/base/strings.h"
11#include "src/base/vector.h"
12
13namespace v8 {
14namespace internal {
15namespace wasm {
16
17void TraceMemoryOperation(base::Optional<ExecutionTier> tier,
18                          const MemoryTracingInfo* info, int func_index,
19                          int position, uint8_t* mem_start) {
20  base::EmbeddedVector<char, 91> value;
21  auto mem_rep = static_cast<MachineRepresentation>(info->mem_rep);
22  Address address = reinterpret_cast<Address>(mem_start) + info->offset;
23  switch (mem_rep) {
24#define TRACE_TYPE(rep, str, format, ctype1, ctype2)              \
25  case MachineRepresentation::rep:                                \
26    base::SNPrintF(value, str ":" format,                         \
27                   base::ReadLittleEndianValue<ctype1>(address),  \
28                   base::ReadLittleEndianValue<ctype2>(address)); \
29    break;
30    TRACE_TYPE(kWord8, " i8", "%d / %02x", uint8_t, uint8_t)
31    TRACE_TYPE(kWord16, "i16", "%d / %04x", uint16_t, uint16_t)
32    TRACE_TYPE(kWord32, "i32", "%d / %08x", uint32_t, uint32_t)
33    TRACE_TYPE(kWord64, "i64", "%" PRId64 " / %016" PRIx64, uint64_t, uint64_t)
34    TRACE_TYPE(kFloat32, "f32", "%f / %08x", float, uint32_t)
35    TRACE_TYPE(kFloat64, "f64", "%f / %016" PRIx64, double, uint64_t)
36#undef TRACE_TYPE
37    case MachineRepresentation::kSimd128:
38      SNPrintF(value, "s128:%d %d %d %d / %08x %08x %08x %08x",
39               base::ReadLittleEndianValue<uint32_t>(address),
40               base::ReadLittleEndianValue<uint32_t>(address + 4),
41               base::ReadLittleEndianValue<uint32_t>(address + 8),
42               base::ReadLittleEndianValue<uint32_t>(address + 12),
43               base::ReadLittleEndianValue<uint32_t>(address),
44               base::ReadLittleEndianValue<uint32_t>(address + 4),
45               base::ReadLittleEndianValue<uint32_t>(address + 8),
46               base::ReadLittleEndianValue<uint32_t>(address + 12));
47      break;
48    default:
49      SNPrintF(value, "???");
50  }
51  const char* eng =
52      tier.has_value() ? ExecutionTierToString(tier.value()) : "?";
53  printf("%-11s func:%6d:0x%-6x%s %016" PRIuPTR " val: %s\n", eng, func_index,
54         position, info->is_store ? " store to" : "load from", info->offset,
55         value.begin());
56}
57
58}  // namespace wasm
59}  // namespace internal
60}  // namespace v8
61