Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / include / gdbstub / helpers.h
blob26140ef1ac09113e1dd1694245ecb46868d308bd
1 /*
2 * gdbstub helpers
4 * These are all used by the various frontends and have to be host
5 * aware to ensure things are store in target order.
7 * Copyright (c) 2022 Linaro Ltd
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #ifndef _GDBSTUB_HELPERS_H_
13 #define _GDBSTUB_HELPERS_H_
15 #ifndef COMPILING_PER_TARGET
16 #error "gdbstub helpers should only be included by target specific code"
17 #endif
19 #include "exec/tswap.h"
20 #include "cpu-param.h"
23 * The GDB remote protocol transfers values in target byte order. As
24 * the gdbstub may be batching up several register values we always
25 * append to the array.
28 static inline int gdb_get_reg8(GByteArray *buf, uint8_t val)
30 g_byte_array_append(buf, &val, 1);
31 return 1;
34 static inline int gdb_get_reg16(GByteArray *buf, uint16_t val)
36 uint16_t to_word = tswap16(val);
37 g_byte_array_append(buf, (uint8_t *) &to_word, 2);
38 return 2;
41 static inline int gdb_get_reg32(GByteArray *buf, uint32_t val)
43 uint32_t to_long = tswap32(val);
44 g_byte_array_append(buf, (uint8_t *) &to_long, 4);
45 return 4;
48 static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
50 uint64_t to_quad = tswap64(val);
51 g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
52 return 8;
55 static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
56 uint64_t val_lo)
58 uint64_t to_quad;
59 #if TARGET_BIG_ENDIAN
60 to_quad = tswap64(val_hi);
61 g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
62 to_quad = tswap64(val_lo);
63 g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
64 #else
65 to_quad = tswap64(val_lo);
66 g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
67 to_quad = tswap64(val_hi);
68 g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
69 #endif
70 return 16;
73 static inline int gdb_get_zeroes(GByteArray *array, size_t len)
75 guint oldlen = array->len;
76 g_byte_array_set_size(array, oldlen + len);
77 memset(array->data + oldlen, 0, len);
79 return len;
82 /**
83 * gdb_get_reg_ptr: get pointer to start of last element
84 * @len: length of element
86 * This is a helper function to extract the pointer to the last
87 * element for additional processing. Some front-ends do additional
88 * dynamic swapping of the elements based on CPU state.
90 static inline uint8_t *gdb_get_reg_ptr(GByteArray *buf, int len)
92 return buf->data + buf->len - len;
95 #if TARGET_LONG_BITS == 64
96 #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val)
97 #define ldtul_p(addr) ldq_p(addr)
98 #else
99 #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val)
100 #define ldtul_p(addr) ldl_p(addr)
101 #endif
103 #endif /* _GDBSTUB_HELPERS_H_ */