summaryrefslogtreecommitdiff
path: root/tools/debugserver/source/DNBDataRef.h
blob: 9a19f20227e67ce7211235960cd6a63c71f6a5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//===-- DNBDataRef.h --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  Created by Greg Clayton on 1/11/06.
//
//===----------------------------------------------------------------------===//
//
//  DNBDataRef is a class that can extract data in normal or byte
//  swapped order from a data buffer that someone else owns. The data
//  buffer needs to remain intact as long as the DNBDataRef object
//  needs the data. Strings returned are pointers into the data buffer
//  and will need to be copied if they are needed after the data buffer
//  is no longer around.
//
//===----------------------------------------------------------------------===//

#ifndef __DNBDataRef_h__
#define __DNBDataRef_h__

#include "DNBDefs.h"
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

class DNBDataRef {
public:
  // For use with Dump
  typedef enum {
    TypeUInt8 = 0,
    TypeChar,
    TypeUInt16,
    TypeUInt32,
    TypeUInt64,
    TypePointer,
    TypeULEB128,
    TypeSLEB128
  } Type;
  typedef uint32_t offset_t;
  typedef nub_addr_t addr_t;

  DNBDataRef();
  DNBDataRef(const uint8_t *start, size_t size, bool swap);
  ~DNBDataRef();
  void Clear() {
    DNBDataRef::SetData(NULL, 0);
    m_swap = false;
  }

  size_t BytesLeft(size_t offset) const {
    const size_t size = GetSize();
    if (size > offset)
      return size - offset;
    return 0;
  }

  bool ValidOffset(offset_t offset) const { return BytesLeft(offset) > 0; }
  bool ValidOffsetForDataOfSize(offset_t offset, uint32_t num_bytes) const {
    return num_bytes <= BytesLeft(offset);
  }
  size_t GetSize() const { return m_end - m_start; }
  const uint8_t *GetDataStart() const { return m_start; }
  const uint8_t *GetDataEnd() const { return m_end; }
  bool GetSwap() const { return m_swap; }
  void SetSwap(bool swap) { m_swap = swap; }
  void SetData(const uint8_t *start, size_t size) {
    m_start = start;
    if (m_start != NULL)
      m_end = start + size;
    else
      m_end = NULL;
  }
  uint8_t GetPointerSize() const { return m_ptrSize; }
  void SetPointerSize(uint8_t size) { m_ptrSize = size; }
  void SetEHPtrBaseAddrPCRelative(addr_t addr = INVALID_NUB_ADDRESS) {
    m_addrPCRelative = addr;
  }
  void SetEHPtrBaseAddrTEXT(addr_t addr = INVALID_NUB_ADDRESS) {
    m_addrTEXT = addr;
  }
  void SetEHPtrBaseAddrDATA(addr_t addr = INVALID_NUB_ADDRESS) {
    m_addrDATA = addr;
  }
  uint8_t Get8(offset_t *offset_ptr) const;
  uint16_t Get16(offset_t *offset_ptr) const;
  uint32_t Get32(offset_t *offset_ptr) const;
  uint64_t Get64(offset_t *offset_ptr) const;
  uint32_t GetMax32(offset_t *offset_ptr, uint32_t byte_size) const;
  uint64_t GetMax64(offset_t *offset_ptr, uint32_t byte_size) const;
  uint64_t GetPointer(offset_t *offset_ptr) const;
  //  uint64_t        GetDwarfEHPtr(offset_t *offset_ptr, uint32_t eh_ptr_enc)
  //  const;
  const char *GetCStr(offset_t *offset_ptr, uint32_t fixed_length = 0) const;
  const char *PeekCStr(offset_t offset) const {
    if (ValidOffset(offset))
      return (const char *)m_start + offset;
    return NULL;
  }

  const uint8_t *GetData(offset_t *offset_ptr, uint32_t length) const;
  uint64_t Get_ULEB128(offset_t *offset_ptr) const;
  int64_t Get_SLEB128(offset_t *offset_ptr) const;
  void Skip_LEB128(offset_t *offset_ptr) const;

  uint32_t Dump(offset_t startOffset, offset_t endOffset, uint64_t offsetBase,
                DNBDataRef::Type type, uint32_t numPerLine,
                const char *typeFormat = NULL);

protected:
  const uint8_t *m_start;
  const uint8_t *m_end;
  bool m_swap;
  uint8_t m_ptrSize;
  addr_t m_addrPCRelative;
  addr_t m_addrTEXT;
  addr_t m_addrDATA;
};

#endif // #ifndef __DNBDataRef_h__