|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Text; |
|
19 using SymbianDebugLib.Engine; |
|
20 using SymbianStackLib.AddressInfo; |
|
21 using SymbianStackLib.Algorithms; |
|
22 using SymbianStackLib.Data.Output; |
|
23 using SymbianStackLib.Data.Source; |
|
24 using SymbianStackLib.Data.Source.Primer; |
|
25 using SymbianStackLib.Prefixes; |
|
26 using SymbianStructuresLib.Arm.Registers; |
|
27 using SymbianStructuresLib.CodeSegments; |
|
28 using SymbianUtils; |
|
29 using SymbianUtils.Tracer; |
|
30 |
|
31 namespace SymbianStackLib.Engine |
|
32 { |
|
33 public sealed class StackEngine : ITracer |
|
34 { |
|
35 #region Delegates & events |
|
36 public enum TEvent |
|
37 { |
|
38 EStackBuildingStarted = 0, |
|
39 EStackBuildingProgress, |
|
40 EStackBuildingComplete |
|
41 } |
|
42 |
|
43 public enum TMessageType |
|
44 { |
|
45 ETypeWarning = 0, |
|
46 ETypeError |
|
47 } |
|
48 |
|
49 public delegate void StackEngineEventHandler( TEvent aEvent, StackEngine aEngine ); |
|
50 public event StackEngineEventHandler EventHandler; |
|
51 |
|
52 public delegate void StackEngineExceptionHandler( Exception aException, StackEngine aEngine ); |
|
53 public event StackEngineExceptionHandler ExceptionHandler; |
|
54 |
|
55 public delegate void StackEngineMessageHandler( TMessageType aType, string aMessage, StackEngine aEngine ); |
|
56 public event StackEngineMessageHandler MessageHandler; |
|
57 #endregion |
|
58 |
|
59 #region Constructors |
|
60 public StackEngine( DbgEngine aDebugEngine ) |
|
61 { |
|
62 // Construction order important - must make primer after data source |
|
63 iPrefixes = new StackPrefixManager( this ); |
|
64 iPrimer = new StackEnginePrimer( this ); |
|
65 // |
|
66 iDebugEngine = aDebugEngine; |
|
67 } |
|
68 #endregion |
|
69 |
|
70 #region API |
|
71 public void Reconstruct( TSynchronicity aSynchronicity ) |
|
72 { |
|
73 StackAlgorithmManager algorithmManager = new StackAlgorithmManager( this ); |
|
74 algorithmManager.EventHandler += new StackAlgorithmManager.AlgorithmEventHandler( AlgorithmManager_EventHandler ); |
|
75 algorithmManager.ExceptionHandler += new StackAlgorithmManager.AlgorithmExceptionHandler( AlgorithmManager_ExceptionHandler ); |
|
76 algorithmManager.Reconstruct( aSynchronicity ); |
|
77 } |
|
78 |
|
79 public void ReconstructAsync() |
|
80 { |
|
81 Reconstruct( TSynchronicity.EAsynchronous ); |
|
82 } |
|
83 |
|
84 public void ReconstructSync() |
|
85 { |
|
86 Reconstruct( TSynchronicity.ESynchronous ); |
|
87 } |
|
88 #endregion |
|
89 |
|
90 #region Properties |
|
91 public int Progress |
|
92 { |
|
93 get { return iProgress; } |
|
94 } |
|
95 |
|
96 public bool Verbose |
|
97 { |
|
98 get |
|
99 { |
|
100 bool ret = iVerbose; |
|
101 if ( System.Diagnostics.Debugger.IsAttached ) |
|
102 { |
|
103 ret = true; |
|
104 } |
|
105 return ret; |
|
106 } |
|
107 set { iVerbose = value; } |
|
108 } |
|
109 |
|
110 public DbgEngine DebugEngine |
|
111 { |
|
112 get { return iDebugEngine; } |
|
113 } |
|
114 |
|
115 public StackEnginePrimer Primer |
|
116 { |
|
117 get { return iPrimer; } |
|
118 } |
|
119 |
|
120 public StackPrefixManager Prefixes |
|
121 { |
|
122 get { return iPrefixes; } |
|
123 } |
|
124 |
|
125 public StackAddressInfo AddressInfo |
|
126 { |
|
127 get { return iAddressInfo; } |
|
128 } |
|
129 |
|
130 public StackSourceData DataSource |
|
131 { |
|
132 get { return iDataSource; } |
|
133 set { iDataSource = value; } |
|
134 } |
|
135 |
|
136 public StackOutputData DataOutput |
|
137 { |
|
138 get { return iDataOutput; } |
|
139 } |
|
140 |
|
141 public ArmRegisterCollection Registers |
|
142 { |
|
143 get { return iRegisters; } |
|
144 set |
|
145 { |
|
146 iRegisters = value; |
|
147 |
|
148 // Update SP in the address info area if not already set |
|
149 if ( iAddressInfo.HaveSetStackPointer == false && iRegisters.Contains( TArmRegisterType.EArmReg_SP ) ) |
|
150 { |
|
151 iAddressInfo.Pointer = iRegisters[ TArmRegisterType.EArmReg_SP ].Value; |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 public CodeSegDefinitionCollection CodeSegments |
|
157 { |
|
158 get { return iCodeSegments; } |
|
159 set { iCodeSegments = value; } |
|
160 } |
|
161 #endregion |
|
162 |
|
163 #region Event handlers |
|
164 private void AlgorithmManager_ExceptionHandler( StackAlgorithmManager aAlgManager, Exception aException ) |
|
165 { |
|
166 if ( ExceptionHandler != null ) |
|
167 { |
|
168 ExceptionHandler( aException, this ); |
|
169 } |
|
170 } |
|
171 |
|
172 private void AlgorithmManager_EventHandler( StackAlgorithmManager aAlgManager, StackAlgorithmManager.TEvent aEvent ) |
|
173 { |
|
174 if ( EventHandler != null ) |
|
175 { |
|
176 switch ( aEvent ) |
|
177 { |
|
178 case StackAlgorithmManager.TEvent.EAlgorithmStarted: |
|
179 EventHandler( TEvent.EStackBuildingStarted, this ); |
|
180 break; |
|
181 case StackAlgorithmManager.TEvent.EAlgorithmProgress: |
|
182 iProgress = aAlgManager.Progress; |
|
183 EventHandler( TEvent.EStackBuildingProgress, this ); |
|
184 break; |
|
185 case StackAlgorithmManager.TEvent.EAlgorithmComplete: |
|
186 // Stop listening to algorithm manager events now that the reconstruction |
|
187 // process is complete. |
|
188 aAlgManager.EventHandler -= new StackAlgorithmManager.AlgorithmEventHandler( AlgorithmManager_EventHandler ); |
|
189 aAlgManager.ExceptionHandler -= new StackAlgorithmManager.AlgorithmExceptionHandler( AlgorithmManager_ExceptionHandler ); |
|
190 |
|
191 // Get rid of alg manager |
|
192 aAlgManager.Dispose(); |
|
193 |
|
194 // Report that we're done! |
|
195 EventHandler( TEvent.EStackBuildingComplete, this ); |
|
196 break; |
|
197 } |
|
198 } |
|
199 } |
|
200 #endregion |
|
201 |
|
202 #region Internal properties |
|
203 #endregion |
|
204 |
|
205 #region Internal methods |
|
206 internal void ReportMessage( TMessageType aType, string aMessage ) |
|
207 { |
|
208 if ( MessageHandler != null ) |
|
209 { |
|
210 MessageHandler( aType, aMessage, this ); |
|
211 } |
|
212 } |
|
213 #endregion |
|
214 |
|
215 #region From ITracer |
|
216 public void Trace( string aMessage ) |
|
217 { |
|
218 iDebugEngine.Trace( aMessage ); |
|
219 } |
|
220 |
|
221 public void Trace( string aFormat, params object[] aParams ) |
|
222 { |
|
223 iDebugEngine.Trace( aFormat, aParams ); |
|
224 } |
|
225 #endregion |
|
226 |
|
227 #region From System.Object |
|
228 public override string ToString() |
|
229 { |
|
230 StringBuilder ret = new StringBuilder(); |
|
231 |
|
232 // CodeSegs |
|
233 foreach ( CodeSegDefinition entry in CodeSegments ) |
|
234 { |
|
235 ret.Append( Prefixes.CodeSegment ); |
|
236 ret.Append( entry.ToString() ); |
|
237 ret.Append( System.Environment.NewLine ); |
|
238 } |
|
239 |
|
240 // Current SP |
|
241 if ( AddressInfo.Pointer != 0 ) |
|
242 { |
|
243 ret.AppendLine( Prefixes.Pointer + "0x" + AddressInfo.Pointer.ToString("x8") ); |
|
244 } |
|
245 |
|
246 // Stack data |
|
247 ret.AppendLine( DataSource.ToString() ); |
|
248 |
|
249 // |
|
250 return ret.ToString(); |
|
251 } |
|
252 #endregion |
|
253 |
|
254 #region Data members |
|
255 private readonly StackEnginePrimer iPrimer; |
|
256 private readonly StackPrefixManager iPrefixes; |
|
257 private readonly DbgEngine iDebugEngine; |
|
258 private bool iVerbose = false; |
|
259 private int iProgress = 0; |
|
260 private StackOutputData iDataOutput = new StackOutputData(); |
|
261 private StackSourceData iDataSource = new StackSourceData(); |
|
262 private StackAddressInfo iAddressInfo = new StackAddressInfo(); |
|
263 private ArmRegisterCollection iRegisters = new ArmRegisterCollection(); |
|
264 private CodeSegDefinitionCollection iCodeSegments = new CodeSegDefinitionCollection(); |
|
265 #endregion |
|
266 } |
|
267 } |