2 * TargetValue.h -- Access to target values using OMPD callbacks
5 //===----------------------------------------------------------------------===//
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //===----------------------------------------------------------------------===//
13 #include "omp-tools.h"
14 #include "ompd-private.h"
17 #ifndef SRC_TARGET_VALUE_H_
18 #define SRC_TARGET_VALUE_H_
32 std::map
<ompd_address_space_context_t
*, std::map
<const char *, TType
>>
36 TTypeFactory() : ttypes() {}
37 TType
&getType(ompd_address_space_context_t
*context
, const char *typName
,
38 ompd_addr_t segment
= OMPD_SEGMENT_UNSPECIFIED
);
41 static thread_local TTypeFactory tf
= TTypeFactory();
46 std::map
<const char *, ompd_size_t
> fieldOffsets
;
47 std::map
<const char *, ompd_size_t
> fieldSizes
;
48 std::map
<const char *, uint64_t> bitfieldMasks
;
49 ompd_addr_t descSegment
;
51 ompd_address_space_context_t
*context
;
53 TType(ompd_address_space_context_t
*context
, const char *typeName
,
54 ompd_addr_t _segment
= OMPD_SEGMENT_UNSPECIFIED
);
57 TType(bool, ompd_addr_t _segment
= OMPD_SEGMENT_UNSPECIFIED
)
58 : descSegment(_segment
), isvoid(true) {}
59 bool isVoid() const { return isvoid
; }
60 ompd_rc_t
getElementOffset(const char *fieldName
, ompd_size_t
*offset
);
61 ompd_rc_t
getElementSize(const char *fieldName
, ompd_size_t
*size
);
62 ompd_rc_t
getBitfieldMask(const char *fieldName
, uint64_t *bitfieldmask
);
63 ompd_rc_t
getSize(ompd_size_t
*size
);
68 static TType
nullType(true);
72 * As TValue is designed to concatenate operations, we use TError
73 * to catch errors that might happen on each operation and provide
74 * the according error code and which operation raised the error.
80 TError() : errorCode(ompd_rc_ok
) {}
81 TError(const ompd_rc_t
&error
) : errorCode(error
) {}
84 std::string
toString() {
85 return std::string("TError messages not implemented yet");
93 * This class encapsules the access to target values by using OMPD
94 * callback functions. The member functions are designed to concatenate
95 * the operations that are needed to access values from structures
96 * e.g., _a[6]->_b._c would read like :
98 * "_a").cast("A",2).getArrayElement(6).access("_b").cast("B").access("_c")
106 ompd_address_space_context_t
*context
;
107 ompd_thread_context_t
*tcontext
;
108 ompd_address_t symbolAddr
;
109 ompd_size_t fieldSize
;
112 static const ompd_callbacks_t
*callbacks
;
113 static ompd_device_type_sizes_t type_sizes
;
115 TValue() : errorState(ompd_rc_error
) {}
117 * Create a target value object from symbol name
119 TValue(ompd_address_space_context_t
*_context
, const char *_valueName
,
120 ompd_addr_t segment
= OMPD_SEGMENT_UNSPECIFIED
)
121 : TValue(_context
, NULL
, _valueName
, segment
) {}
123 TValue(ompd_address_space_context_t
*context
, ompd_thread_context_t
*tcontext
,
124 const char *valueName
, ompd_addr_t segment
= OMPD_SEGMENT_UNSPECIFIED
);
126 * Create a target value object from target value address
128 TValue(ompd_address_space_context_t
*_context
, ompd_address_t _addr
)
129 : TValue(_context
, NULL
, _addr
) {}
130 TValue(ompd_address_space_context_t
*context
, ompd_thread_context_t
*tcontext
,
131 ompd_address_t addr
);
133 * Cast the target value object to some type of typeName
135 * This call modifies the object and returns a reference to the modified
138 TValue
&cast(const char *typeName
);
141 * Cast the target value object to some pointer of type typename
142 * pointerlevel gives the number of *
143 * e.g., char** would be: cast("char",2)
145 * This call modifies the object and returns a reference to the modified
148 TValue
&cast(const char *typeName
, int pointerLevel
,
149 ompd_addr_t segment
= OMPD_SEGMENT_UNSPECIFIED
);
152 * Get the target address of the target value
154 ompd_rc_t
getAddress(ompd_address_t
*addr
) const;
156 * Get the raw memory copy of the target value
158 ompd_rc_t
getRawValue(void *buf
, int count
);
160 * Fetch a string copy from the target. "this" represents the pointer
161 * that holds the value of a null terminated character string. "buf"
162 * points to the destination string to be allocated and copied to.
163 * Returns 'ompd_rc_error' to signify a truncated string or a target
166 ompd_rc_t
getString(const char **buf
);
168 * Get a new target value object for the dereferenced target value
169 * reduces the pointer level, uses the target value as new target address,
170 * keeps the target type
172 TValue
dereference() const;
174 * Cast to a base type
175 * Only values of base type may be read from target
177 TBaseValue
castBase(ompd_target_prim_types_t baseType
) const;
179 * Cast to a base type
180 * Get the size by fieldsize from runtime
182 TBaseValue
castBase() const;
184 * Cast to a base type
185 * Get the size by name from the rtl
187 TBaseValue
castBase(const char *varName
);
189 * Resolve field access for structs/unions
190 * this supports both "->" and "." operator.
192 TValue
access(const char *fieldName
) const;
194 * Tests for a field bit in a bitfield
196 ompd_rc_t
check(const char *bitfieldName
, ompd_word_t
*isSet
) const;
198 * Get an array element
200 TValue
getArrayElement(int elemNumber
) const;
202 * Get an element of a pointer array
204 TValue
getPtrArrayElement(int elemNumber
) const;
206 * Did we raise some error yet?
208 bool gotError() const { return errorState
.errorCode
!= ompd_rc_ok
; }
212 ompd_rc_t
getError() const { return errorState
.errorCode
; }
214 * Did we raise some error yet?
216 std::string
getErrorMessage() { return errorState
.toString(); }
219 class TBaseValue
: public TValue
{
221 ompd_size_t baseTypeSize
= 0;
222 TBaseValue(const TValue
&, ompd_target_prim_types_t baseType
);
223 TBaseValue(const TValue
&, ompd_size_t baseTypeSize
);
226 ompd_rc_t
getValue(void *buf
, int count
);
227 template <typename T
> ompd_rc_t
getValue(T
&buf
);
232 template <typename T
> ompd_rc_t
TBaseValue::getValue(T
&buf
) {
233 assert(sizeof(T
) >= baseTypeSize
);
234 if (sizeof(T
) == baseTypeSize
)
235 return getValue(&buf
, 1);
238 ompd_rc_t ret
= getValue(tmp
, 1);
239 switch (baseTypeSize
) {
241 buf
= (T
) * ((int8_t *)tmp
);
244 buf
= (T
) * ((int16_t *)tmp
);
247 buf
= (T
) * ((int32_t *)tmp
);
250 buf
= (T
) * ((int64_t *)tmp
);
253 assert(0 && "Invalid baseTypeSize");
259 #define EXTERN_C extern "C"
264 #endif /*SRC_TARGET_VALUE_H_*/