1/* 2* \file trc_ret_stack.cpp 3* \brief OpenCSD : trace decoder return stack feature. 4* 5* \copyright Copyright (c) 2017, ARM Limited. All Rights Reserved. 6*/ 7 8/* 9* Redistribution and use in source and binary forms, with or without modification, 10* are permitted provided that the following conditions are met: 11* 12* 1. Redistributions of source code must retain the above copyright notice, 13* this list of conditions and the following disclaimer. 14* 15* 2. Redistributions in binary form must reproduce the above copyright notice, 16* this list of conditions and the following disclaimer in the documentation 17* and/or other materials provided with the distribution. 18* 19* 3. Neither the name of the copyright holder nor the names of its contributors 20* may be used to endorse or promote products derived from this software without 21* specific prior written permission. 22* 23* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33*/ 34#include "common/trc_ret_stack.h" 35 36#ifdef TRC_RET_STACK_DEBUG 37#include <sstream> 38#include <iostream> 39#include "common/trc_component.h" 40 41#define LOG_POP(A,O,I) LogOp("Pop",A,O,I) 42#define LOG_PUSH(A,O,I) LogOp("Push",A,O,I) 43#define LOG_FLUSH() LogOp("Flush",0,-1000,(const ocsd_isa)0) 44 45// uncomment for forced std::cout log, bypassing normal library debug logger. 46// useful perhaps when perf is decoding w/o printing. 47// #define FORCE_STD_COUT 48 49#else 50#define LOG_POP(A,O,I) 51#define LOG_PUSH(A,O,I) 52#define LOG_FLUSH() 53#endif 54 55TrcAddrReturnStack::TrcAddrReturnStack() : 56 m_active(false), 57 m_pop_pending(false), 58 head_idx(0), 59 num_entries(0) 60{ 61#ifdef TRC_RET_STACK_DEBUG 62 m_p_debug_logger = 0; 63#endif 64} 65 66void TrcAddrReturnStack::push(const ocsd_vaddr_t addr, const ocsd_isa isa) 67{ 68 if (is_active()) 69 { 70 head_idx++; 71 head_idx &= 0xF; 72 m_stack[head_idx].ret_addr = addr; 73 m_stack[head_idx].ret_isa = isa; 74 num_entries++; 75 if (num_entries > 16) 76 num_entries = 16; 77 LOG_PUSH(addr,0,isa); 78 m_pop_pending = false; 79 } 80} 81 82ocsd_vaddr_t TrcAddrReturnStack::pop(ocsd_isa &isa) 83{ 84 ocsd_vaddr_t addr = (ocsd_vaddr_t)-1; 85 if (is_active()) 86 { 87 if (num_entries > 0) 88 { 89 addr = m_stack[head_idx].ret_addr; 90 isa = m_stack[head_idx].ret_isa; 91 head_idx--; 92 head_idx &= 0xF; 93 } 94 num_entries--; 95 LOG_POP(addr,1,isa); 96 m_pop_pending = false; 97 } 98 return addr; 99} 100 101 102void TrcAddrReturnStack::flush() 103{ 104 num_entries = 0; 105 m_pop_pending = false; 106 LOG_FLUSH(); 107} 108 109#ifdef TRC_RET_STACK_DEBUG 110void TrcAddrReturnStack::LogOp(const char * pszOpString, ocsd_vaddr_t addr, int head_off, ocsd_isa isa) 111{ 112 static const char *isa_names[] = 113 { 114 "A32", /**< V7 ARM 32, V8 AArch32 */ 115 "T32", /**< Thumb2 -> 16/32 bit instructions */ 116 "A64", /**< V8 AArch64 */ 117 "TEE", /**< Thumb EE - unsupported */ 118 "JZL", /**< Jazelle - unsupported in trace */ 119 "custom", /**< Instruction set - custom arch decoder */ 120 "unknown" /**< ISA not yet known */ 121 }; 122 123 if (m_p_debug_logger) 124 { 125 std::ostringstream oss; 126 if(head_off == -1000) 127 { 128 oss << "Return stack " << pszOpString << "\n"; 129 } 130 else 131 { 132 int name_idx = (int)isa; 133 if (name_idx > 6) 134 name_idx = 6; 135 oss << "Return stack " << pszOpString << "[" << std::dec << (head_idx+head_off) << "](0x" << std::hex << addr << "), " << isa_names[name_idx] << ";"; 136 oss << "current entries = " << std::dec << num_entries << ";"; 137 oss << "new head idx = " << head_idx << ";"; 138 oss << "pop pend (pre op) = " << (m_pop_pending ? "true\n" : "false\n"); 139 } 140#ifdef FORCE_STD_COUT 141 std::cout << oss.str(); 142 std::cout.flush(); 143#endif 144 m_p_debug_logger->LogDefMessage(oss.str()); 145 } 146} 147#endif 148 149/* End of File trc_ret_stack.cpp */ 150