1 /*
2 * \file       trc_etmv4_stack_elem.cpp
3 * \brief      OpenCSD : ETMv4 decoder
4 *
5 * \copyright  Copyright (c) 2017, ARM Limited. All Rights Reserved.
6 */
7 
8 
9 /*
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of the copyright holder nor the names of its contributors
21 * may be used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 
36 #include "opencsd/etmv4/trc_etmv4_stack_elem.h"
37 
38 /* implementation of P0 element stack in ETM v4 trace*/
39 TrcStackElem *EtmV4P0Stack::createParamElemNoParam(const p0_elem_t p0_type, const bool isP0, const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, bool back /*= false*/)
40 {
41     TrcStackElem *pElem = new (std::nothrow) TrcStackElem(p0_type, isP0, root_pkt, root_index);
42     if (pElem)
43     {
44         if (back)
45             push_back(pElem);
46         else
47             push_front(pElem);
48     }
49     return pElem;
50 }
51 
52 TrcStackElemParam *EtmV4P0Stack::createParamElem(const p0_elem_t p0_type, const bool isP0, const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const std::vector<uint32_t> &params)
53 {
54     TrcStackElemParam *pElem = new (std::nothrow) TrcStackElemParam(p0_type, isP0, root_pkt, root_index);
55     if (pElem)
56     {
57         int param_idx = 0;
58         int params_to_fill = params.size();
59         while ((param_idx < 4) && params_to_fill)
60         {
61             pElem->setParam(params[param_idx], param_idx);
62             param_idx++;
63             params_to_fill--;
64         }
65         push_front(pElem);
66     }
67     return pElem;
68 }
69 
70 TrcStackElemAtom *EtmV4P0Stack::createAtomElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const ocsd_pkt_atom &atom)
71 {
72     TrcStackElemAtom *pElem = new (std::nothrow) TrcStackElemAtom(root_pkt, root_index);
73     if (pElem)
74     {
75         pElem->setAtom(atom);
76         push_front(pElem);
77     }
78     return pElem;
79 }
80 
81 TrcStackElemExcept *EtmV4P0Stack::createExceptElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const bool bSame, const uint16_t excepNum)
82 {
83     TrcStackElemExcept *pElem = new (std::nothrow) TrcStackElemExcept(root_pkt, root_index);
84     if (pElem)
85     {
86         pElem->setExcepNum(excepNum);
87         pElem->setPrevSame(bSame);
88         push_front(pElem);
89     }
90     return pElem;
91 }
92 
93 TrcStackElemCtxt *EtmV4P0Stack::createContextElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const etmv4_context_t &context, const uint8_t IS, const bool back /*= false*/)
94 {
95     TrcStackElemCtxt *pElem = new (std::nothrow) TrcStackElemCtxt(root_pkt, root_index);
96     if (pElem)
97     {
98         pElem->setContext(context);
99         pElem->setIS(IS);
100         if (back)
101             push_back(pElem);
102         else
103             push_front(pElem);
104     }
105     return pElem;
106 
107 }
108 
109 TrcStackElemAddr *EtmV4P0Stack::createAddrElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const etmv4_addr_val_t &addr_val)
110 {
111     TrcStackElemAddr *pElem = new (std::nothrow) TrcStackElemAddr(root_pkt, root_index);
112     if (pElem)
113     {
114         pElem->setAddr(addr_val);
115         push_front(pElem);
116     }
117     return pElem;
118 }
119 
120 TrcStackQElem *EtmV4P0Stack::createQElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const int count)
121 {
122     TrcStackQElem *pElem = new (std::nothrow) TrcStackQElem(root_pkt, root_index);
123     if (pElem)
124     {
125         pElem->setInstrCount(count);
126         push_front(pElem);
127     }
128     return pElem;
129 }
130 
131 TrcStackElemMarker *EtmV4P0Stack::createMarkerElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const trace_marker_payload_t &marker)
132 {
133     TrcStackElemMarker *pElem = new (std::nothrow) TrcStackElemMarker(root_pkt, root_index);
134     if (pElem)
135     {
136         pElem->setMarker(marker);
137         push_front(pElem);
138     }
139     return pElem;
140 }
141 
142 TrcStackElemAddr *EtmV4P0Stack::createSrcAddrElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const etmv4_addr_val_t &addr_val)
143 {
144     TrcStackElemAddr *pElem = new (std::nothrow) TrcStackElemAddr(root_pkt, root_index, true);
145     if (pElem)
146     {
147         pElem->setAddr(addr_val);
148         push_front(pElem);
149     }
150     return pElem;
151 }
152 
153 TrcStackElemITE *EtmV4P0Stack::createITEElem(const ocsd_etmv4_i_pkt_type root_pkt, const ocsd_trc_index_t root_index, const trace_sw_ite_t &ite)
154 {
155     TrcStackElemITE *pElem = new (std::nothrow) TrcStackElemITE(root_pkt, root_index);
156     if (pElem)
157     {
158         pElem->setITE(ite);
159         push_front(pElem);
160     }
161     return pElem;
162 }
163 
164 
165 // iteration functions
166 void EtmV4P0Stack::from_front_init()
167 {
168     m_iter = m_P0_stack.begin();
169 }
170 
171 TrcStackElem *EtmV4P0Stack::from_front_next()
172 {
173     TrcStackElem *pElem = 0;
174     if (m_iter != m_P0_stack.end())
175     {
176         pElem = *m_iter++;
177     }
178     return pElem;
179 }
180 
181 void EtmV4P0Stack::erase_curr_from_front()
182 {
183     std::deque<TrcStackElem *>::iterator erase_iter;
184     erase_iter = m_iter;
185     erase_iter--;
186     m_P0_stack.erase(erase_iter);
187 
188     // explicitly delete the item here as the caller can no longer reference it.
189     // fixes memory leak from github issue #52
190     delete *erase_iter;
191 }
192 
193 
194 /* End of file trc_etmv4_stack_elem.cpp */
195