2
0

helpers.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * gdbstub helpers
  3. *
  4. * These are all used by the various frontends and have to be host
  5. * aware to ensure things are store in target order.
  6. *
  7. * Copyright (c) 2022 Linaro Ltd
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. */
  11. #ifndef _GDBSTUB_HELPERS_H_
  12. #define _GDBSTUB_HELPERS_H_
  13. #ifdef COMPILING_PER_TARGET
  14. #include "cpu.h"
  15. /*
  16. * The GDB remote protocol transfers values in target byte order. As
  17. * the gdbstub may be batching up several register values we always
  18. * append to the array.
  19. */
  20. static inline int gdb_get_reg8(GByteArray *buf, uint8_t val)
  21. {
  22. g_byte_array_append(buf, &val, 1);
  23. return 1;
  24. }
  25. static inline int gdb_get_reg16(GByteArray *buf, uint16_t val)
  26. {
  27. uint16_t to_word = tswap16(val);
  28. g_byte_array_append(buf, (uint8_t *) &to_word, 2);
  29. return 2;
  30. }
  31. static inline int gdb_get_reg32(GByteArray *buf, uint32_t val)
  32. {
  33. uint32_t to_long = tswap32(val);
  34. g_byte_array_append(buf, (uint8_t *) &to_long, 4);
  35. return 4;
  36. }
  37. static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
  38. {
  39. uint64_t to_quad = tswap64(val);
  40. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  41. return 8;
  42. }
  43. static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
  44. uint64_t val_lo)
  45. {
  46. uint64_t to_quad;
  47. #if TARGET_BIG_ENDIAN
  48. to_quad = tswap64(val_hi);
  49. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  50. to_quad = tswap64(val_lo);
  51. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  52. #else
  53. to_quad = tswap64(val_lo);
  54. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  55. to_quad = tswap64(val_hi);
  56. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  57. #endif
  58. return 16;
  59. }
  60. static inline int gdb_get_zeroes(GByteArray *array, size_t len)
  61. {
  62. guint oldlen = array->len;
  63. g_byte_array_set_size(array, oldlen + len);
  64. memset(array->data + oldlen, 0, len);
  65. return len;
  66. }
  67. /**
  68. * gdb_get_reg_ptr: get pointer to start of last element
  69. * @len: length of element
  70. *
  71. * This is a helper function to extract the pointer to the last
  72. * element for additional processing. Some front-ends do additional
  73. * dynamic swapping of the elements based on CPU state.
  74. */
  75. static inline uint8_t *gdb_get_reg_ptr(GByteArray *buf, int len)
  76. {
  77. return buf->data + buf->len - len;
  78. }
  79. #if TARGET_LONG_BITS == 64
  80. #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val)
  81. #define ldtul_p(addr) ldq_p(addr)
  82. #else
  83. #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val)
  84. #define ldtul_p(addr) ldl_p(addr)
  85. #endif
  86. #else
  87. #error "gdbstub helpers should only be included by target specific code"
  88. #endif
  89. #endif /* _GDBSTUB_HELPERS_H_ */