Skip to content

Commit 143435d

Browse files
committed
Show single line source
1 parent 8bebf7e commit 143435d

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

src/MIDebugEngine/AD7.Impl/AD7Disassembly.cs

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ internal class AD7DisassemblyStream : IDebugDisassemblyStream2
1515
private ulong _addr;
1616
private enum_DISASSEMBLY_STREAM_SCOPE _scope;
1717
private string _pLastDocument;
18-
private int m_dwLastSourceLine;
19-
private bool m_lastSourceInfoStale;
20-
private bool m_skipNextInstruction;
18+
private uint _dwLastSourceLine;
19+
private bool _lastSourceInfoStale;
20+
private bool _skipNextInstruction;
2121

2222
internal AD7DisassemblyStream(AD7Engine engine, enum_DISASSEMBLY_STREAM_SCOPE scope, IDebugCodeContext2 pCodeContext)
2323
{
@@ -26,8 +26,8 @@ internal AD7DisassemblyStream(AD7Engine engine, enum_DISASSEMBLY_STREAM_SCOPE sc
2626
AD7MemoryAddress addr = pCodeContext as AD7MemoryAddress;
2727
_addr = addr.Address;
2828
_pLastDocument = null;
29-
m_dwLastSourceLine = -1;
30-
m_lastSourceInfoStale = true;
29+
_dwLastSourceLine = 0;
30+
_lastSourceInfoStale = true;
3131
}
3232

3333
#region IDebugDisassemblyStream2 Members
@@ -102,12 +102,12 @@ private DisassemblyData FetchBadInstruction(enum_DISASSEMBLY_STREAM_FIELDS dwFie
102102

103103
public int Read(uint dwInstructions, enum_DISASSEMBLY_STREAM_FIELDS dwFields, out uint pdwInstructionsRead, DisassemblyData[] prgDisassembly)
104104
{
105-
if (m_lastSourceInfoStale && !m_skipNextInstruction)
105+
if (_lastSourceInfoStale && !_skipNextInstruction)
106106
{
107107
SeekBack(1);
108108
}
109109

110-
if (m_skipNextInstruction)
110+
if (_skipNextInstruction)
111111
{
112112
dwInstructions += 1;
113113
}
@@ -124,8 +124,17 @@ public int Read(uint dwInstructions, enum_DISASSEMBLY_STREAM_FIELDS dwFields, ou
124124
// bad address range, return '??'
125125
for (iOp = 0; iOp < dwInstructions; _addr++, ++iOp)
126126
{
127+
if (_skipNextInstruction)
128+
{
129+
_addr++;
130+
dwInstructions--;
131+
_skipNextInstruction = false;
132+
}
127133
prgDisassembly[iOp] = FetchBadInstruction(dwFields);
128134
}
135+
_lastSourceInfoStale = false;
136+
_dwLastSourceLine = 0;
137+
_pLastDocument = null;
129138
pdwInstructionsRead = iOp;
130139
return Constants.S_OK;
131140
}
@@ -139,26 +148,25 @@ public int Read(uint dwInstructions, enum_DISASSEMBLY_STREAM_FIELDS dwFields, ou
139148
using (IEnumerator<DisasmInstruction> instructionEnumerator = instructions.GetEnumerator())
140149
{
141150
int idx = 0;
142-
if (m_skipNextInstruction)
151+
if (_skipNextInstruction)
143152
{
153+
_skipNextInstruction = false;
154+
144155
instructionEnumerator.MoveNext();
145156
DisasmInstruction instruction = instructionEnumerator.Current;
146157

147-
m_dwLastSourceLine = (int)instruction.Line - 1;
148-
_pLastDocument = string.Concat("file://", instruction.File);
158+
_dwLastSourceLine = instruction.Line != 0 ? instruction.Line - 1 : 0;
159+
_pLastDocument = instruction.File;
149160

161+
dwInstructions--;
150162
idx++;
151163
}
152164

153-
for (; idx < dwInstructions; idx++)
165+
for (; idx < dwInstructions + 1 && iOp < dwInstructions; idx++)
154166
{
155167
instructionEnumerator.MoveNext();
156168
DisasmInstruction instruction = instructionEnumerator.Current;
157169

158-
if (iOp >= dwInstructions)
159-
{
160-
break;
161-
}
162170
_addr = instruction.Addr;
163171

164172
if ((dwFields & enum_DISASSEMBLY_STREAM_FIELDS.DSF_ADDRESS) != 0)
@@ -197,45 +205,59 @@ public int Read(uint dwInstructions, enum_DISASSEMBLY_STREAM_FIELDS dwFields, ou
197205
}
198206
}
199207

200-
if ((dwFields & enum_DISASSEMBLY_STREAM_FIELDS.DSF_POSITION) != 0 && instruction.Line != 0)
208+
209+
string currentFile = instruction.File;
210+
uint currentLine = instruction.Line - 1;
211+
bool isNewDocument = string.IsNullOrEmpty(_pLastDocument) || !_pLastDocument.Equals(currentFile, StringComparison.Ordinal);
212+
bool isNewLine = currentLine != _dwLastSourceLine;
213+
214+
if ((dwFields & enum_DISASSEMBLY_STREAM_FIELDS.DSF_POSITION) != 0 && currentLine != 0)
201215
{
202-
uint startLine = instruction.Line - 1;
203216

217+
prgDisassembly[iOp].dwFields |= enum_DISASSEMBLY_STREAM_FIELDS.DSF_POSITION;
204218
prgDisassembly[iOp].posBeg = new TEXT_POSITION()
205219
{
206-
dwLine = startLine,
220+
dwLine = currentLine,
207221
dwColumn = 0
208222
};
209223
prgDisassembly[iOp].posEnd = new TEXT_POSITION()
210224
{
211-
dwLine = instruction.Line,
225+
dwLine = currentLine,
212226
dwColumn = 0
213227
};
214228

215-
prgDisassembly[iOp].dwFields |= enum_DISASSEMBLY_STREAM_FIELDS.DSF_POSITION;
216-
217-
if (m_dwLastSourceLine != instruction.Line && instruction.Line != 0)
218-
{
219-
prgDisassembly[iOp].dwFlags |= enum_DISASSEMBLY_FLAGS.DF_HASSOURCE;
220-
}
229+
// Update last seen source line.
230+
_dwLastSourceLine = currentLine;
221231
}
222232

223-
bool newDocument = string.IsNullOrEmpty(_pLastDocument) || !_pLastDocument.Equals(instruction.File, StringComparison.Ordinal);
233+
// Show source if we have line and file information and if there is a new line.
234+
if (currentLine != 0 && currentFile != null && isNewLine)
235+
{
236+
prgDisassembly[iOp].dwFlags |= enum_DISASSEMBLY_FLAGS.DF_HASSOURCE;
237+
}
224238

225-
if (!string.IsNullOrWhiteSpace(instruction.File))
239+
if (!string.IsNullOrWhiteSpace(currentFile))
226240
{
227241
if ((dwFields & enum_DISASSEMBLY_STREAM_FIELDS.DSF_DOCUMENTURL) != 0)
228242
{
229-
if (newDocument || idx == 0)
243+
// idx 0 is the previous instruction. The first requested instruction
244+
// is at idx 1.
245+
if (isNewDocument || idx == 1)
230246
{
231247
prgDisassembly[iOp].dwFields |= enum_DISASSEMBLY_STREAM_FIELDS.DSF_DOCUMENTURL;
232-
prgDisassembly[iOp].bstrDocumentUrl = string.Concat("file://", instruction.File);
248+
prgDisassembly[iOp].bstrDocumentUrl = string.Concat("file://", currentFile);
233249
}
234250
}
235251
}
236252

237-
m_dwLastSourceLine = (int)instruction.Line;
238-
if (newDocument)
253+
if ((dwFields & enum_DISASSEMBLY_STREAM_FIELDS.DSF_BYTEOFFSET) != 0)
254+
{
255+
prgDisassembly[iOp].dwFields |= enum_DISASSEMBLY_STREAM_FIELDS.DSF_BYTEOFFSET;
256+
// For the disassembly window, offset should be set to 0 to show source and non-zero to hide it.
257+
prgDisassembly[iOp].dwByteOffset = isNewLine ? 0 : uint.MaxValue;
258+
}
259+
260+
if (isNewDocument)
239261
{
240262
prgDisassembly[iOp].dwFlags |= enum_DISASSEMBLY_FLAGS.DF_DOCUMENTCHANGE;
241263
_pLastDocument = instruction.File;
@@ -288,10 +310,10 @@ private int SeekForward(long iInstructions)
288310

289311
private int SeekBack(long iInstructions)
290312
{
291-
if (m_lastSourceInfoStale)
313+
if (_lastSourceInfoStale)
292314
{
293315
iInstructions += 1;
294-
m_skipNextInstruction = true;
316+
_skipNextInstruction = true;
295317
}
296318
_engine.DebuggedProcess.WorkerThread.RunOperation(async () =>
297319
{
@@ -303,8 +325,8 @@ private int SeekBack(long iInstructions)
303325
public int Seek(enum_SEEK_START dwSeekStart, IDebugCodeContext2 pCodeContext, ulong uCodeLocationId, long iInstructions)
304326
{
305327
_pLastDocument = null;
306-
m_lastSourceInfoStale = true;
307-
m_dwLastSourceLine = -1;
328+
_lastSourceInfoStale = true;
329+
_dwLastSourceLine = 0;
308330

309331
if (dwSeekStart == enum_SEEK_START.SEEK_START_CODECONTEXT)
310332
{

0 commit comments

Comments
 (0)