backtrace.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _TILE_BACKTRACE_H
  15. #define _TILE_BACKTRACE_H
  16. #include <linux/types.h>
  17. #include <arch/chip.h>
  18. #if defined(__tile__)
  19. typedef unsigned long VirtualAddress;
  20. #elif CHIP_VA_WIDTH() > 32
  21. typedef unsigned long long VirtualAddress;
  22. #else
  23. typedef unsigned int VirtualAddress;
  24. #endif
  25. /** Reads 'size' bytes from 'address' and writes the data to 'result'.
  26. * Returns true if successful, else false (e.g. memory not readable).
  27. */
  28. typedef bool (*BacktraceMemoryReader)(void *result,
  29. VirtualAddress address,
  30. unsigned int size,
  31. void *extra);
  32. typedef struct {
  33. /** Current PC. */
  34. VirtualAddress pc;
  35. /** Current stack pointer value. */
  36. VirtualAddress sp;
  37. /** Current frame pointer value (i.e. caller's stack pointer) */
  38. VirtualAddress fp;
  39. /** Internal use only: caller's PC for first frame. */
  40. VirtualAddress initial_frame_caller_pc;
  41. /** Internal use only: callback to read memory. */
  42. BacktraceMemoryReader read_memory_func;
  43. /** Internal use only: arbitrary argument to read_memory_func. */
  44. void *read_memory_func_extra;
  45. } BacktraceIterator;
  46. /** Initializes a backtracer to start from the given location.
  47. *
  48. * If the frame pointer cannot be determined it is set to -1.
  49. *
  50. * @param state The state to be filled in.
  51. * @param read_memory_func A callback that reads memory. If NULL, a default
  52. * value is provided.
  53. * @param read_memory_func_extra An arbitrary argument to read_memory_func.
  54. * @param pc The current PC.
  55. * @param lr The current value of the 'lr' register.
  56. * @param sp The current value of the 'sp' register.
  57. * @param r52 The current value of the 'r52' register.
  58. */
  59. extern void backtrace_init(BacktraceIterator *state,
  60. BacktraceMemoryReader read_memory_func,
  61. void *read_memory_func_extra,
  62. VirtualAddress pc, VirtualAddress lr,
  63. VirtualAddress sp, VirtualAddress r52);
  64. /** Advances the backtracing state to the calling frame, returning
  65. * true iff successful.
  66. */
  67. extern bool backtrace_next(BacktraceIterator *state);
  68. typedef enum {
  69. /* We have no idea what the caller's pc is. */
  70. PC_LOC_UNKNOWN,
  71. /* The caller's pc is currently in lr. */
  72. PC_LOC_IN_LR,
  73. /* The caller's pc can be found by dereferencing the caller's sp. */
  74. PC_LOC_ON_STACK
  75. } CallerPCLocation;
  76. typedef enum {
  77. /* We have no idea what the caller's sp is. */
  78. SP_LOC_UNKNOWN,
  79. /* The caller's sp is currently in r52. */
  80. SP_LOC_IN_R52,
  81. /* The caller's sp can be found by adding a certain constant
  82. * to the current value of sp.
  83. */
  84. SP_LOC_OFFSET
  85. } CallerSPLocation;
  86. /* Bit values ORed into CALLER_* values for info ops. */
  87. enum {
  88. /* Setting the low bit on any of these values means the info op
  89. * applies only to one bundle ago.
  90. */
  91. ONE_BUNDLE_AGO_FLAG = 1,
  92. /* Setting this bit on a CALLER_SP_* value means the PC is in LR.
  93. * If not set, PC is on the stack.
  94. */
  95. PC_IN_LR_FLAG = 2,
  96. /* This many of the low bits of a CALLER_SP_* value are for the
  97. * flag bits above.
  98. */
  99. NUM_INFO_OP_FLAGS = 2,
  100. /* We cannot have one in the memory pipe so this is the maximum. */
  101. MAX_INFO_OPS_PER_BUNDLE = 2
  102. };
  103. /** Internal constants used to define 'info' operands. */
  104. enum {
  105. /* 0 and 1 are reserved, as are all negative numbers. */
  106. CALLER_UNKNOWN_BASE = 2,
  107. CALLER_SP_IN_R52_BASE = 4,
  108. CALLER_SP_OFFSET_BASE = 8,
  109. /* Marks the entry point of certain functions. */
  110. ENTRY_POINT_INFO_OP = 16
  111. };
  112. /** Current backtracer state describing where it thinks the caller is. */
  113. typedef struct {
  114. /*
  115. * Public fields
  116. */
  117. /* How do we find the caller's PC? */
  118. CallerPCLocation pc_location : 8;
  119. /* How do we find the caller's SP? */
  120. CallerSPLocation sp_location : 8;
  121. /* If sp_location == SP_LOC_OFFSET, then caller_sp == sp +
  122. * loc->sp_offset. Else this field is undefined.
  123. */
  124. uint16_t sp_offset;
  125. /* In the most recently visited bundle a terminating bundle? */
  126. bool at_terminating_bundle;
  127. /*
  128. * Private fields
  129. */
  130. /* Will the forward scanner see someone clobbering sp
  131. * (i.e. changing it with something other than addi sp, sp, N?)
  132. */
  133. bool sp_clobber_follows;
  134. /* Operand to next "visible" info op (no more than one bundle past
  135. * the next terminating bundle), or -32768 if none.
  136. */
  137. int16_t next_info_operand;
  138. /* Is the info of in next_info_op in the very next bundle? */
  139. bool is_next_info_operand_adjacent;
  140. } CallerLocation;
  141. #endif /* _TILE_BACKTRACE_H */