Debian Codegraph @

src/debug/debug.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: debug.cpp,v 1.97 2009-04-11 19:49:52 c2woody Exp $ */
20
21 #include "dosbox.h"
22 #if C_DEBUG
23
24 #include <string.h>
25 #include <list>
26 #include <ctype.h>
27 #include <fstream>
28 #include <iomanip>
29 #include <string>
30 #include <sstream>
31 using namespace std;
32
33 #include "debug.h"
34 #include "cross.h" //snprintf
35 #include "cpu.h"
36 #include "video.h"
37 #include "pic.h"
38 #include "mapper.h"
39 #include "cpu.h"
40 #include "callback.h"
41 #include "inout.h"
42 #include "mixer.h"
43 #include "timer.h"
44 #include "paging.h"
45 #include "support.h"
46 #include "shell.h"
47 #include "programs.h"
48 #include "debug_inc.h"
49 #include "../cpu/lazyflags.h"
50 #include "keyboard.h"
51 #include "setup.h"
52
53 #ifdef WIN32
54 void WIN32_Console();
55 #else
56 #include <termios.h>
57 #include <unistd.h>
58 static struct termios consolesettings;
59 #endif
60 int old_cursor_state;
61
62 // Forwards
63 static void DrawCode(void);
64 static void DEBUG_RaiseTimerIrq(void);
65 static void SaveMemory(Bit16u seg, Bit32u ofs1, Bit32u num);
66 static void SaveMemoryBin(Bit16u seg, Bit32u ofs1, Bit32u num);
67 static void LogMCBS(void);
68 static void LogGDT(void);
69 static void LogLDT(void);
70 static void LogIDT(void);
71 static void LogPages(char* selname);
72 static void LogCPUInfo(void);
73 static void OutputVecTable(char* filename);
74 static void DrawVariables(void);
75
76 char* AnalyzeInstruction(char* inst, bool saveSelector);
77 Bit32u GetHexValue(char* str, char*& hex);
78
79 class DebugPageHandler : public PageHandler {
80 public:
81 Bitu readb(PhysPt /*addr*/) {
82 }
83 Bitu readw(PhysPt /*addr*/) {
84 }
85 Bitu readd(PhysPt /*addr*/) {
86 }
87 void writeb(PhysPt /*addr*/,Bitu /*val*/) {
88 }
89 void writew(PhysPt /*addr*/,Bitu /*val*/) {
90 }
91 void writed(PhysPt /*addr*/,Bitu /*val*/) {
92 }
93
94
95
96 };
97
98
99 class DEBUG;
100
101 DEBUG* pDebugcom = 0;
102 bool exitLoop = false;
103
104
105 // Heavy Debugging Vars for logging
106 #if C_HEAVY_DEBUG
107 static ofstream cpuLogFile;
108 static bool cpuLog = false;
109 static int cpuLogCounter = 0;
110 static int cpuLogType = 1; // log detail
111 static bool zeroProtect = false;
112 bool logHeavy = false;
113 #endif
114
115
116
117 static struct {
118 Bit32u eax,ebx,ecx,edx,esi,edi,ebp,esp,eip;
119 } oldregs;
120
121 static char curSelectorName[3] = { 0,0,0 };
122
123 static Segment oldsegs[6];
124 static Bitu oldflags,oldcpucpl;
125 DBGBlock dbg;
126 static Bitu input_count;
127 Bitu cycle_count;
128 static bool debugging;
129
130
131 static void SetColor(Bitu test) {
132 if (test) {
133 if (has_colors()) { wattrset(dbg.win_reg,COLOR_PAIR(PAIR_BYELLOW_BLACK));}
134 } else {
135 if (has_colors()) { wattrset(dbg.win_reg,0);}
136 }
137 }
138
139 struct SCodeViewData {
140 int cursorPos;
141 Bit16u firstInstSize;
142 Bit16u useCS;
143 Bit32u useEIPlast, useEIPmid;
144 Bit32u useEIP;
145 Bit16u cursorSeg;
146 Bit32u cursorOfs;
147 bool inputMode;
148 char inputStr[255];
149 char prevInputStr[255];
150
151 } codeViewData;
152
153 static Bit16u dataSeg;
154 static Bit32u dataOfs;
155 static bool showExtend = true;
156
157 /***********/
158 /* Helpers */
159 /***********/
160
161 Bit32u PhysMakeProt(Bit16u selector, Bit32u offset)
162 {
163 Descriptor desc;
164 if (cpu.gdt.GetDescriptor(selector,desc)) return desc.GetBase()+offset;
165 return 0;
166 };
167
168 Bit32u GetAddress(Bit16u seg, Bit32u offset)
169 {
170 if (seg==SegValue(cs)) return SegPhys(cs)+offset;
171 if (cpu.pmode && !(reg_flags & FLAG_VM)) {
172 Descriptor desc;
173 if (cpu.gdt.GetDescriptor(seg,desc)) return PhysMakeProt(seg,offset);
174 }
175 return (seg<<4)+offset;
176 }
177
178 static char empty_sel[] = { ' ',' ',0 };
179
180 bool GetDescriptorInfo(char* selname, char* out1, char* out2)
181 {
182 Bitu sel;
183 Descriptor desc;
184
185 if (strstr(selname,"cs") || strstr(selname,"CS")) sel = SegValue(cs);
186 else if (strstr(selname,"ds") || strstr(selname,"DS")) sel = SegValue(ds);
187 else if (strstr(selname,"es") || strstr(selname,"ES")) sel = SegValue(es);
188 else if (strstr(selname,"fs") || strstr(selname,"FS")) sel = SegValue(fs);
189 else if (strstr(selname,"gs") || strstr(selname,"GS")) sel = SegValue(gs);
190 else if (strstr(selname,"ss") || strstr(selname,"SS")) sel = SegValue(ss);
191 else {
192 sel = GetHexValue(selname,selname);
193 if (*selname==0) selname=empty_sel;
194 }
195 if (cpu.gdt.GetDescriptor(sel,desc)) {
196 switch (desc.Type()) {
197 case DESC_TASK_GATE:
198 sprintf(out1,"%s: s:%08X type:%02X p",selname,desc.GetSelector(),desc.saved.gate.type);
199 sprintf(out2," TaskGate dpl : %01X %1X",desc.saved.gate.dpl,desc.saved.gate.p);
200 return true;
201 case DESC_LDT:
202 case DESC_286_TSS_A:
203 case DESC_286_TSS_B:
204 case DESC_386_TSS_A:
205 case DESC_386_TSS_B:
206 sprintf(out1,"%s: b:%08X type:%02X pag",selname,desc.GetBase(),desc.saved.seg.type);
207 sprintf(out2," l:%08X dpl : %01X %1X%1X%1X",desc.GetLimit(),desc.saved.seg.dpl,desc.saved.seg.p,desc.saved.seg.avl,desc.saved.seg.g);
208 return true;
209 case DESC_286_CALL_GATE:
210 case DESC_386_CALL_GATE:
211 sprintf(out1,"%s: s:%08X type:%02X p params: %02X",selname,desc.GetSelector(),desc.saved.gate.type,desc.saved.gate.paramcount);
212 sprintf(out2," o:%08X dpl : %01X %1X",desc.GetOffset(),desc.saved.gate.dpl,desc.saved.gate.p);
213 return true;
214 case DESC_286_INT_GATE:
215 case DESC_286_TRAP_GATE:
216 case DESC_386_INT_GATE:
217 case DESC_386_TRAP_GATE:
218 sprintf(out1,"%s: s:%08X type:%02X p",selname,desc.GetSelector(),desc.saved.gate.type);
219 sprintf(out2," o:%08X dpl : %01X %1X",desc.GetOffset(),desc.saved.gate.dpl,desc.saved.gate.p);
220 return true;
221 }
222 sprintf(out1,"%s: b:%08X type:%02X parbg",selname,desc.GetBase(),desc.saved.seg.type);
223 sprintf(out2," l:%08X dpl : %01X %1X%1X%1X%1X%1X",desc.GetLimit(),desc.saved.seg.dpl,desc.saved.seg.p,desc.saved.seg.avl,desc.saved.seg.r,desc.saved.seg.big,desc.saved.seg.g);
224 return true;
225 } else {
226 strcpy(out1," ");
227 strcpy(out2," ");
228 }
229 return false;
230 };
231
232 /********************/
233 /* DebugVar stuff */
234 /********************/
235
236 class CDebugVar
237 {
238 public:
239 CDebugVar(char* _name, PhysPt _adr) { adr=_adr; safe_strncpy(name,_name,16); };
240
241 char* GetName(void) { return name; };
242 PhysPt GetAdr (void) { return adr; };
243
244 private:
245 PhysPt adr;
246 char name[16];
247
248 public:
249 static void InsertVariable (char* name, PhysPt adr);
250 static CDebugVar* FindVar (PhysPt adr);
251 static void DeleteAll ();
252 static bool SaveVars (char* name);
253 static bool LoadVars (char* name);
254
255 static std::list<CDebugVar*> varList;
256 };
257
258 std::list<CDebugVar*> CDebugVar::varList;
259
260
261 /********************/
262 /* Breakpoint stuff */
263 /********************/
264
265 bool skipFirstInstruction = false;
266
267 enum EBreakpoint { BKPNT_UNKNOWN, BKPNT_PHYSICAL, BKPNT_INTERRUPT, BKPNT_MEMORY, BKPNT_MEMORY_PROT, BKPNT_MEMORY_LINEAR };
268
269 #define BPINT_ALL 0x100
270
271 class CBreakpoint
272 {
273 public:
274
275 CBreakpoint(void);
276 void SetAddress (Bit16u seg, Bit32u off) { location = GetAddress(seg,off); type = BKPNT_PHYSICAL; segment = seg; offset = off; };
277 void SetAddress (PhysPt adr) { location = adr; type = BKPNT_PHYSICAL; };
278 void SetInt (Bit8u _intNr, Bit16u ah) { intNr = _intNr, ahValue = ah; type = BKPNT_INTERRUPT; };
279 void SetOnce (bool _once) { once = _once; };
280 void SetType (EBreakpoint _type) { type = _type; };
281 void SetValue (Bit8u value) { ahValue = value; };
282
283 bool IsActive (void) { return active; };
284 void Activate (bool _active);
285
286 EBreakpoint GetType (void) { return type; };
287 bool GetOnce (void) { return once; };
288 PhysPt GetLocation (void) { if (GetType()!=BKPNT_INTERRUPT) return location; else return 0; };
289 Bit16u GetSegment (void) { return segment; };
290 Bit32u GetOffset (void) { return offset; };
291 Bit8u GetIntNr (void) { if (GetType()==BKPNT_INTERRUPT) return intNr; else return 0; };
292 Bit16u GetValue (void) { if (GetType()!=BKPNT_PHYSICAL) return ahValue; else return 0; };
293
294 // statics
295 static CBreakpoint* AddBreakpoint (Bit16u seg, Bit32u off, bool once);
296 static CBreakpoint* AddIntBreakpoint (Bit8u intNum, Bit16u ah, bool once);
297 static CBreakpoint* AddMemBreakpoint (Bit16u seg, Bit32u off);
298 static void ActivateBreakpoints (PhysPt adr, bool activate);
299 static bool CheckBreakpoint (PhysPt adr);
300 static bool CheckBreakpoint (Bitu seg, Bitu off);
301 static bool CheckIntBreakpoint (PhysPt adr, Bit8u intNr, Bit16u ahValue);
302 static bool IsBreakpoint (PhysPt where);
303 static bool IsBreakpointDrawn (PhysPt where);
304 static bool DeleteBreakpoint (PhysPt where);
305 static bool DeleteByIndex (Bit16u index);
306 static void DeleteAll (void);
307 static void ShowList (void);
308
309
310 private:
311 EBreakpoint type;
312 // Physical
313 PhysPt location;
314 Bit8u oldData;
315 Bit16u segment;
316 Bit32u offset;
317 // Int
318 Bit8u intNr;
319 Bit16u ahValue;
320 // Shared
321 bool active;
322 bool once;
323
324 static std::list<CBreakpoint*> BPoints;
325 public:
326 static CBreakpoint* ignoreOnce;
327 };
328
329 CBreakpoint::CBreakpoint(void):
330 location(0),
331 active(false),once(false),
332 segment(0),offset(0),intNr(0),ahValue(0),
333 type(BKPNT_UNKNOWN) { };
334
335 void CBreakpoint::Activate(bool _active)
336 {
337 #if !C_HEAVY_DEBUG
338 if (GetType()==BKPNT_PHYSICAL) {
339 if (_active) {
340 // Set 0xCC and save old value
341 Bit8u data = mem_readb(location);
342 if (data!=0xCC) {
343 oldData = data;
344 mem_writeb(location,0xCC);
345 };
346 } else {
347 // Remove 0xCC and set old value
348 if (mem_readb (location)==0xCC) {
349 mem_writeb(location,oldData);
350 };
351 }
352 }
353 #endif
354 active = _active;
355 };
356
357 // Statics
358 std::list<CBreakpoint*> CBreakpoint::BPoints;
359 CBreakpoint* CBreakpoint::ignoreOnce = 0;
360 Bitu ignoreAddressOnce = 0;
361
362 CBreakpoint* CBreakpoint::AddBreakpoint(Bit16u seg, Bit32u off, bool once)
363 {
364 CBreakpoint* bp = new CBreakpoint();
365 bp->SetAddress (seg,off);
366 bp->SetOnce (once);
367 BPoints.push_front (bp);
368 return bp;
369 };
370
371 CBreakpoint* CBreakpoint::AddIntBreakpoint(Bit8u intNum, Bit16u ah, bool once)
372 {
373 CBreakpoint* bp = new CBreakpoint();
374 bp->SetInt (intNum,ah);
375 bp->SetOnce (once);
376 BPoints.push_front (bp);
377 return bp;
378 };
379
380 CBreakpoint* CBreakpoint::AddMemBreakpoint(Bit16u seg, Bit32u off)
381 {
382 CBreakpoint* bp = new CBreakpoint();
383 bp->SetAddress (seg,off);
384 bp->SetOnce (false);
385 bp->SetType (BKPNT_MEMORY);
386 BPoints.push_front (bp);
387 return bp;
388 };
389
390 void CBreakpoint::ActivateBreakpoints(PhysPt adr, bool activate)
391 {
392 // activate all breakpoints
393 std::list<CBreakpoint*>::iterator i;
394 CBreakpoint* bp;
395 for(i=BPoints.begin(); i != BPoints.end(); i++) {
396 bp = (*i);
397 // Do not activate, when bp is an actual adress
398 if (activate && (bp->GetType()==BKPNT_PHYSICAL) && (bp->GetLocation()==adr)) {
399 // Do not activate :)
400 continue;
401 }
402 bp->Activate(activate);
403 };
404 };
405
406 bool CBreakpoint::CheckBreakpoint(Bitu seg, Bitu off)
407 // Checks if breakpoint is valid an should stop execution
408 {
409 if ((ignoreAddressOnce!=0) && (GetAddress(seg,off)==ignoreAddressOnce)) {
410 ignoreAddressOnce = 0;
411 return false;
412 } else
413 ignoreAddressOnce = 0;
414
415 // Search matching breakpoint
416 std::list<CBreakpoint*>::iterator i;
417 CBreakpoint* bp;
418 for(i=BPoints.begin(); i != BPoints.end(); i++) {
419 bp = (*i);
420 if ((bp->GetType()==BKPNT_PHYSICAL) && bp->IsActive() && (bp->GetSegment()==seg) && (bp->GetOffset()==off)) {
421 // Ignore Once ?
422 if (ignoreOnce==bp) {
423 ignoreOnce=0;
424 bp->Activate(true);
425 return false;
426 };
427 // Found,
428 if (bp->GetOnce()) {
429 // delete it, if it should only be used once
430 (BPoints.erase)(i);
431 bp->Activate(false);
432 delete bp;
433 } else {
434 ignoreOnce = bp;
435 };
436 return true;
437 }
438 #if C_HEAVY_DEBUG
439 // Memory breakpoint support
440 else if (bp->IsActive()) {
441 if ((bp->GetType()==BKPNT_MEMORY) || (bp->GetType()==BKPNT_MEMORY_PROT) || (bp->GetType()==BKPNT_MEMORY_LINEAR)) {
442 // Watch Protected Mode Memoryonly in pmode
443 if (bp->GetType()==BKPNT_MEMORY_PROT) {
444 // Check if pmode is active
445 if (!cpu.pmode) return false;
446 // Check if descriptor is valid
447 Descriptor desc;
448 if (!cpu.gdt.GetDescriptor(bp->GetSegment(),desc)) return false;
449 if (desc.GetLimit()==0) return false;
450 }
451
452 Bitu address;
453 if (bp->GetType()==BKPNT_MEMORY_LINEAR) address = bp->GetOffset();
454 else address = GetAddress(bp->GetSegment(),bp->GetOffset());
455 Bit8u value=0;
456 if (mem_readb_checked(address,&value)) return false;
457 if (bp->GetValue() != value) {
458 // Yup, memory value changed
459 DEBUG_ShowMsg("DEBUG: Memory breakpoint %s: %04X:%04X - %02X -> %02X\n",(bp->GetType()==BKPNT_MEMORY_PROT)?"(Prot)":"",bp->GetSegment(),bp->GetOffset(),bp->GetValue(),value);
460 bp->SetValue(value);
461 return true;
462 };
463 }
464 };
465 #endif
466 };
467 return false;
468 };
469
470 bool CBreakpoint::CheckIntBreakpoint(PhysPt adr, Bit8u intNr, Bit16u ahValue)
471 // Checks if interrupt breakpoint is valid an should stop execution
472 {
473 if ((ignoreAddressOnce!=0) && (adr==ignoreAddressOnce)) {
474 ignoreAddressOnce = 0;
475 return false;
476 } else
477 ignoreAddressOnce = 0;
478
479 // Search matching breakpoint
480 std::list<CBreakpoint*>::iterator i;
481 CBreakpoint* bp;
482 for(i=BPoints.begin(); i != BPoints.end(); i++) {
483 bp = (*i);
484 if ((bp->GetType()==BKPNT_INTERRUPT) && bp->IsActive() && (bp->GetIntNr()==intNr)) {
485 if ((bp->GetValue()==BPINT_ALL) || (bp->GetValue()==ahValue)) {
486 // Ignoie it once ?
487 if (ignoreOnce==bp) {
488 ignoreOnce=0;
489 bp->Activate(true);
490 return false;
491 };
492 // Found
493 if (bp->GetOnce()) {
494 // delete it, if it should only be used once
495 (BPoints.erase)(i);
496 bp->Activate(false);
497 delete bp;
498 } else {
499 ignoreOnce = bp;
500 }
501 return true;
502 }
503 };
504 };
505 return false;
506 };
507
508 void CBreakpoint::DeleteAll()
509 {
510 std::list<CBreakpoint*>::iterator i;
511 CBreakpoint* bp;
512 for(i=BPoints.begin(); i != BPoints.end(); i++) {
513 bp = (*i);
514 bp->Activate(false);
515 delete bp;
516 };
517 (BPoints.clear)();
518 };
519
520
521 bool CBreakpoint::DeleteByIndex(Bit16u index)
522 {
523 // Search matching breakpoint
524 int nr = 0;
525 std::list<CBreakpoint*>::iterator i;
526 CBreakpoint* bp;
527 for(i=BPoints.begin(); i != BPoints.end(); i++) {
528 if (nr==index) {
529 bp = (*i);
530 (BPoints.erase)(i);
531 bp->Activate(false);
532 delete bp;
533 return true;
534 }
535 nr++;
536 };
537 return false;
538 };
539
540 bool CBreakpoint::DeleteBreakpoint(PhysPt where)
541 {
542 // Search matching breakpoint
543 std::list<CBreakpoint*>::iterator i;
544 CBreakpoint* bp;
545 for(i=BPoints.begin(); i != BPoints.end(); i++) {
546 bp = (*i);
547 if ((bp->GetType()==BKPNT_PHYSICAL) && (bp->GetLocation()==where)) {
548 (BPoints.erase)(i);
549 bp->Activate(false);
550 delete bp;
551 return true;
552 }
553 };
554 return false;
555 };
556
557 bool CBreakpoint::IsBreakpoint(PhysPt adr)
558 // is there a breakpoint at address ?
559 {
560 // Search matching breakpoint
561 std::list<CBreakpoint*>::iterator i;
562 CBreakpoint* bp;
563 for(i=BPoints.begin(); i != BPoints.end(); i++) {
564 bp = (*i);
565 if ((bp->GetType()==BKPNT_PHYSICAL) && (bp->GetSegment()==adr)) {
566 return true;
567 };
568 if ((bp->GetType()==BKPNT_PHYSICAL) && (bp->GetLocation()==adr)) {
569 return true;
570 };
571 };
572 return false;
573 };
574
575 bool CBreakpoint::IsBreakpointDrawn(PhysPt adr)
576 // valid breakpoint, that should be drawn ?
577 {
578 // Search matching breakpoint
579 std::list<CBreakpoint*>::iterator i;
580 CBreakpoint* bp;
581 for(i=BPoints.begin(); i != BPoints.end(); i++) {
582 bp = (*i);
583 if ((bp->GetType()==BKPNT_PHYSICAL) && (bp->GetLocation()==adr)) {
584 // Only draw, if breakpoint is not only once,
585 return !bp->GetOnce();
586 };
587 };
588 return false;
589 };
590
591 void CBreakpoint::ShowList(void)
592 {
593 // iterate list
594 int nr = 0;
595 std::list<CBreakpoint*>::iterator i;
596 for(i=BPoints.begin(); i != BPoints.end(); i++) {
597 CBreakpoint* bp = (*i);
598 if (bp->GetType()==BKPNT_PHYSICAL) {
599 DEBUG_ShowMsg("%02X. BP %04X:%04X\n",nr,bp->GetSegment(),bp->GetOffset());
600 } else if (bp->GetType()==BKPNT_INTERRUPT) {
601 if (bp->GetValue()==BPINT_ALL) DEBUG_ShowMsg("%02X. BPINT %02X\n",nr,bp->GetIntNr());
602 else DEBUG_ShowMsg("%02X. BPINT %02X AH=%02X\n",nr,bp->GetIntNr(),bp->GetValue());
603 } else if (bp->GetType()==BKPNT_MEMORY) {
604 DEBUG_ShowMsg("%02X. BPMEM %04X:%04X (%02X)\n",nr,bp->GetSegment(),bp->GetOffset(),bp->GetValue());
605 } else if (bp->GetType()==BKPNT_MEMORY_PROT) {
606 DEBUG_ShowMsg("%02X. BPPM %04X:%08X (%02X)\n",nr,bp->GetSegment(),bp->GetOffset(),bp->GetValue());
607 } else if (bp->GetType()==BKPNT_MEMORY_LINEAR ) {
608 DEBUG_ShowMsg("%02X. BPLM %08X (%02X)\n",nr,bp->GetOffset(),bp->GetValue());
609 };
610 nr++;
611 }
612 };
613
614 bool DEBUG_Breakpoint(void)
615 {
616 /* First get the phyiscal address and check for a set Breakpoint */
617 if (!CBreakpoint::CheckBreakpoint(SegValue(cs),reg_eip)) return false;
618 // Found. Breakpoint is valid
619 PhysPt where=GetAddress(SegValue(cs),reg_eip);
620 CBreakpoint::ActivateBreakpoints(where,false); // Deactivate all breakpoints
621 return true;
622 };
623
624 bool DEBUG_IntBreakpoint(Bit8u intNum)
625 {
626 /* First get the phyiscal address and check for a set Breakpoint */
627 PhysPt where=GetAddress(SegValue(cs),reg_eip);
628 if (!CBreakpoint::CheckIntBreakpoint(where,intNum,reg_ah)) return false;
629 // Found. Breakpoint is valid
630 CBreakpoint::ActivateBreakpoints(where,false); // Deactivate all breakpoints
631 return true;
632 };
633
634 static bool StepOver()
635 {
636 exitLoop = false;
637 PhysPt start=GetAddress(SegValue(cs),reg_eip);
638 char dline[200];Bitu size;
639 size=DasmI386(dline, start, reg_eip, cpu.code.big);
640
641 if (strstr(dline,"call") || strstr(dline,"int") || strstr(dline,"loop") || strstr(dline,"rep")) {
642 CBreakpoint::AddBreakpoint (SegValue(cs),reg_eip+size, true);
643 CBreakpoint::ActivateBreakpoints(start, true);
644 debugging=false;
645 DrawCode();
646 DOSBOX_SetNormalLoop();
647 return true;
648 }
649 return false;
650 };
651
652 bool DEBUG_ExitLoop(void)
653 {
654 #if C_HEAVY_DEBUG
655 DrawVariables();
656 #endif
657
658 if (exitLoop) {
659 exitLoop = false;
660 return true;
661 }
662 return false;
663 };
664
665 /********************/
666 /* Draw windows */
667 /********************/
668
669 static void DrawData(void) {
670
671 Bit8u ch;
672 Bit32u add = dataOfs;
673 Bit32u address;
674 /* Data win */
675 for (int y=0; y<8; y++) {
676 // Adress
677 if (add<0x10000) mvwprintw (dbg.win_data,1+y,0,"%04X:%04X ",dataSeg,add);
678 else mvwprintw (dbg.win_data,1+y,0,"%04X:%08X ",dataSeg,add);
679 for (int x=0; x<16; x++) {
680 address = GetAddress(dataSeg,add);
681 if (mem_readb_checked(address,&ch)) ch=0;
682 mvwprintw (dbg.win_data,1+y,14+3*x,"%02X",ch);
683 if (ch<32 || !isprint(*reinterpret_cast<unsigned char*>(&ch))) ch='.';
684 mvwprintw (dbg.win_data,1+y,63+x,"%c",ch);
685 add++;
686 };
687 }
688 wrefresh(dbg.win_data);
689 };
690
691 static void DrawRegisters(void) {
692 /* Main Registers */
693 SetColor(reg_eax!=oldregs.eax);oldregs.eax=reg_eax;mvwprintw (dbg.win_reg,0,4,"%08X",reg_eax);
694 SetColor(reg_ebx!=oldregs.ebx);oldregs.ebx=reg_ebx;mvwprintw (dbg.win_reg,1,4,"%08X",reg_ebx);
695 SetColor(reg_ecx!=oldregs.ecx);oldregs.ecx=reg_ecx;mvwprintw (dbg.win_reg,2,4,"%08X",reg_ecx);
696 SetColor(reg_edx!=oldregs.edx);oldregs.edx=reg_edx;mvwprintw (dbg.win_reg,3,4,"%08X",reg_edx);
697
698 SetColor(reg_esi!=oldregs.esi);oldregs.esi=reg_esi;mvwprintw (dbg.win_reg,0,18,"%08X",reg_esi);
699 SetColor(reg_edi!=oldregs.edi);oldregs.edi=reg_edi;mvwprintw (dbg.win_reg,1,18,"%08X",reg_edi);
700 SetColor(reg_ebp!=oldregs.ebp);oldregs.ebp=reg_ebp;mvwprintw (dbg.win_reg,2,18,"%08X",reg_ebp);
701 SetColor(reg_esp!=oldregs.esp);oldregs.esp=reg_esp;mvwprintw (dbg.win_reg,3,18,"%08X",reg_esp);
702 SetColor(reg_eip!=oldregs.eip);oldregs.eip=reg_eip;mvwprintw (dbg.win_reg,1,42,"%08X",reg_eip);
703
704 SetColor(SegValue(ds)!=oldsegs[ds].val);oldsegs[ds].val=SegValue(ds);mvwprintw (dbg.win_reg,0,31,"%04X",SegValue(ds));
705 SetColor(SegValue(es)!=oldsegs[es].val);oldsegs[es].val=SegValue(es);mvwprintw (dbg.win_reg,0,41,"%04X",SegValue(es));
706 SetColor(SegValue(fs)!=oldsegs[fs].val);oldsegs[fs].val=SegValue(fs);mvwprintw (dbg.win_reg,0,51,"%04X",SegValue(fs));
707 SetColor(SegValue(gs)!=oldsegs[gs].val);oldsegs[gs].val=SegValue(gs);mvwprintw (dbg.win_reg,0,61,"%04X",SegValue(gs));
708 SetColor(SegValue(ss)!=oldsegs[ss].val);oldsegs[ss].val=SegValue(ss);mvwprintw (dbg.win_reg,0,71,"%04X",SegValue(ss));
709 SetColor(SegValue(cs)!=oldsegs[cs].val);oldsegs[cs].val=SegValue(cs);mvwprintw (dbg.win_reg,1,31,"%04X",SegValue(cs));
710
711 /*Individual flags*/
712 Bitu changed_flags = reg_flags ^ oldflags;
713 oldflags = reg_flags;
714
715 SetColor(changed_flags&FLAG_CF);
716 mvwprintw (dbg.win_reg,1,53,"%01X",GETFLAG(CF) ? 1:0);
717 SetColor(changed_flags&FLAG_ZF);
718 mvwprintw (dbg.win_reg,1,56,"%01X",GETFLAG(ZF) ? 1:0);
719 SetColor(changed_flags&FLAG_SF);
720 mvwprintw (dbg.win_reg,1,59,"%01X",GETFLAG(SF) ? 1:0);
721 SetColor(changed_flags&FLAG_OF);
722 mvwprintw (dbg.win_reg,1,62,"%01X",GETFLAG(OF) ? 1:0);
723 SetColor(changed_flags&FLAG_AF);
724 mvwprintw (dbg.win_reg,1,65,"%01X",GETFLAG(AF) ? 1:0);
725 SetColor(changed_flags&FLAG_PF);
726 mvwprintw (dbg.win_reg,1,68,"%01X",GETFLAG(PF) ? 1:0);
727
728
729 SetColor(changed_flags&FLAG_DF);
730 mvwprintw (dbg.win_reg,1,71,"%01X",GETFLAG(DF) ? 1:0);
731 SetColor(changed_flags&FLAG_IF);
732 mvwprintw (dbg.win_reg,1,74,"%01X",GETFLAG(IF) ? 1:0);
733 SetColor(changed_flags&FLAG_TF);
734 mvwprintw (dbg.win_reg,1,77,"%01X",GETFLAG(TF) ? 1:0);
735
736 SetColor(changed_flags&FLAG_IOPL);
737 mvwprintw (dbg.win_reg,2,72,"%01X",GETFLAG(IOPL)>>12);
738
739
740 SetColor(cpu.cpl ^ oldcpucpl);
741 mvwprintw (dbg.win_reg,2,78,"%01X",cpu.cpl);
742 oldcpucpl=cpu.cpl;
743
744 if (cpu.pmode) {
745 if (reg_flags & FLAG_VM) mvwprintw(dbg.win_reg,0,76,"VM86");
746 else if (cpu.code.big) mvwprintw(dbg.win_reg,0,76,"Pr32");
747 else mvwprintw(dbg.win_reg,0,76,"Pr16");
748 } else
749 mvwprintw(dbg.win_reg,0,76,"Real");
750
751 // Selector info, if available
752 if ((cpu.pmode) && curSelectorName[0]) {
753 char out1[200], out2[200];
754 GetDescriptorInfo(curSelectorName,out1,out2);
755 mvwprintw(dbg.win_reg,2,28,out1);
756 mvwprintw(dbg.win_reg,3,28,out2);
757 }
758
759 wattrset(dbg.win_reg,0);
760 mvwprintw(dbg.win_reg,3,60,"%u ",cycle_count);
761 wrefresh(dbg.win_reg);
762 };
763
764 static void DrawCode(void) {
765 bool saveSel;
766 Bit32u disEIP = codeViewData.useEIP;
767 PhysPt start = GetAddress(codeViewData.useCS,codeViewData.useEIP);
768 char dline[200];Bitu size;Bitu c;
769 static char line20[21] = " ";
770
771 for (int i=0;i<10;i++) {
772 saveSel = false;
773 if (has_colors()) {
774 if ((codeViewData.useCS==SegValue(cs)) && (disEIP == reg_eip)) {
775 wattrset(dbg.win_code,COLOR_PAIR(PAIR_GREEN_BLACK));
776 if (codeViewData.cursorPos==-1) {
777 codeViewData.cursorPos = i; // Set Cursor
778 codeViewData.cursorSeg = SegValue(cs);
779 codeViewData.cursorOfs = disEIP;
780 }
781 saveSel = (i == codeViewData.cursorPos);
782 } else if (i == codeViewData.cursorPos) {
783 wattrset(dbg.win_code,COLOR_PAIR(PAIR_BLACK_GREY));
784 codeViewData.cursorSeg = codeViewData.useCS;
785 codeViewData.cursorOfs = disEIP;
786 saveSel = true;
787 } else if (CBreakpoint::IsBreakpointDrawn(start)) {
788 wattrset(dbg.win_code,COLOR_PAIR(PAIR_GREY_RED));
789 } else {
790 wattrset(dbg.win_code,0);
791 }
792 }
793
794
795 Bitu drawsize=size=DasmI386(dline, start, disEIP, cpu.code.big);
796 bool toolarge = false;
797 mvwprintw(dbg.win_code,i,0,"%04X:%04X ",codeViewData.useCS,disEIP);
798
799 if (drawsize>10) { toolarge = true; drawsize = 9; };
800 for (c=0;c<drawsize;c++) {
801 Bit8u value;
802 if (mem_readb_checked(start+c,&value)) value=0;
803 wprintw(dbg.win_code,"%02X",value);
804 }
805 if (toolarge) { waddstr(dbg.win_code,".."); drawsize++; };
806 // Spacepad up to 20 characters
807 if(drawsize && (drawsize < 11)) {
808 line20[20 - drawsize*2] = 0;
809 waddstr(dbg.win_code,line20);
810 line20[20 - drawsize*2] = ' ';
811 } else waddstr(dbg.win_code,line20);
812
813 char empty_res[] = { 0 };
814 char* res = empty_res;
815 if (showExtend) res = AnalyzeInstruction(dline, saveSel);
816 // Spacepad it up to 28 characters
817 size_t dline_len = strlen(dline);
818 if(dline_len < 28) for (c = dline_len; c < 28;c++) dline[c] = ' '; dline[28] = 0;
819 waddstr(dbg.win_code,dline);
820 // Spacepad it up to 20 characters
821 size_t res_len = strlen(res);
822 if(res_len && (res_len < 21)) {
823 waddstr(dbg.win_code,res);
824 line20[20-res_len] = 0;
825 waddstr(dbg.win_code,line20);
826 line20[20-res_len] = ' ';
827 } else waddstr(dbg.win_code,line20);
828
829 start+=size;
830 disEIP+=size;
831
832 if (i==0) codeViewData.firstInstSize = size;
833 if (i==4) codeViewData.useEIPmid = disEIP;
834 }
835
836 codeViewData.useEIPlast = disEIP;
837
838 wattrset(dbg.win_code,0);
839 if (!debugging) {
840 mvwprintw(dbg.win_code,10,0,"(Running)",codeViewData.inputStr);
841 } else {
842 if(!*codeViewData.inputStr) { //Clear old commands
843 mvwprintw(dbg.win_code,10,0," ");
844 }
845 mvwprintw(dbg.win_code,10,0,"-> %s_ ",codeViewData.inputStr);
846 }
847
848 wrefresh(dbg.win_code);
849 }
850
851 static void SetCodeWinStart()
852 {
853 if ((SegValue(cs)==codeViewData.useCS) && (reg_eip>=codeViewData.useEIP) && (reg_eip<=codeViewData.useEIPlast)) {
854 // in valid window - scroll ?
855 if (reg_eip>=codeViewData.useEIPmid) codeViewData.useEIP += codeViewData.firstInstSize;
856
857 } else {
858 // totally out of range.
859 codeViewData.useCS = SegValue(cs);
860 codeViewData.useEIP = reg_eip;
861 }
862 codeViewData.cursorPos = -1; // Recalc Cursor position
863 };
864
865 /********************/
866 /* User input */
867 /********************/
868
869 Bit32u GetHexValue(char* str, char*& hex)
870 {
871 Bit32u value = 0;
872 Bit32u regval = 0;
873 hex = str;
874 while (*hex==' ') hex++;
875 if (strstr(hex,"EAX")==hex) { hex+=3; regval = reg_eax; };
876 if (strstr(hex,"EBX")==hex) { hex+=3; regval = reg_ebx; };
877 if (strstr(hex,"ECX")==hex) { hex+=3; regval = reg_ecx; };
878 if (strstr(hex,"EDX")==hex) { hex+=3; regval = reg_edx; };
879 if (strstr(hex,"ESI")==hex) { hex+=3; regval = reg_esi; };
880 if (strstr(hex,"EDI")==hex) { hex+=3; regval = reg_edi; };
881 if (strstr(hex,"EBP")==hex) { hex+=3; regval = reg_ebp; };
882 if (strstr(hex,"ESP")==hex) { hex+=3; regval = reg_esp; };
883 if (strstr(hex,"EIP")==hex) { hex+=3; regval = reg_eip; };
884 if (strstr(hex,"AX")==hex) { hex+=2; regval = reg_ax; };
885 if (strstr(hex,"BX")==hex) { hex+=2; regval = reg_bx; };
886 if (strstr(hex,"CX")==hex) { hex+=2; regval = reg_cx; };
887 if (strstr(hex,"DX")==hex) { hex+=2; regval = reg_dx; };
888 if (strstr(hex,"SI")==hex) { hex+=2; regval = reg_si; };
889 if (strstr(hex,"DI")==hex) { hex+=2; regval = reg_di; };
890 if (strstr(hex,"BP")==hex) { hex+=2; regval = reg_bp; };
891 if (strstr(hex,"SP")==hex) { hex+=2; regval = reg_sp; };
892 if (strstr(hex,"IP")==hex) { hex+=2; regval = reg_ip; };
893 if (strstr(hex,"CS")==hex) { hex+=2; regval = SegValue(cs); };
894 if (strstr(hex,"DS")==hex) { hex+=2; regval = SegValue(ds); };
895 if (strstr(hex,"ES")==hex) { hex+=2; regval = SegValue(es); };
896 if (strstr(hex,"FS")==hex) { hex+=2; regval = SegValue(fs); };
897 if (strstr(hex,"GS")==hex) { hex+=2; regval = SegValue(gs); };
898 if (strstr(hex,"SS")==hex) { hex+=2; regval = SegValue(ss); };
899
900 while (*hex) {
901 if ((*hex>='0') && (*hex<='9')) value = (value<<4)+*hex-'0';
902 else if ((*hex>='A') && (*hex<='F')) value = (value<<4)+*hex-'A'+10;
903 else {
904 if(*hex == '+') {hex++;return regval + value + GetHexValue(hex,hex); };
905 if(*hex == '-') {hex++;return regval + value - GetHexValue(hex,hex); };
906 break; // No valid char
907 }
908 hex++;
909 };
910 return regval + value;
911 };
912
913 bool ChangeRegister(char* str)
914 {
915 char* hex = str;
916 while (*hex==' ') hex++;
917 if (strstr(hex,"EAX")==hex) { hex+=3; reg_eax = GetHexValue(hex,hex); } else
918 if (strstr(hex,"EBX")==hex) { hex+=3; reg_ebx = GetHexValue(hex,hex); } else
919 if (strstr(hex,"ECX")==hex) { hex+=3; reg_ecx = GetHexValue(hex,hex); } else
920 if (strstr(hex,"EDX")==hex) { hex+=3; reg_edx = GetHexValue(hex,hex); } else
921 if (strstr(hex,"ESI")==hex) { hex+=3; reg_esi = GetHexValue(hex,hex); } else
922 if (strstr(hex,"EDI")==hex) { hex+=3; reg_edi = GetHexValue(hex,hex); } else
923 if (strstr(hex,"EBP")==hex) { hex+=3; reg_ebp = GetHexValue(hex,hex); } else
924 if (strstr(hex,"ESP")==hex) { hex+=3; reg_esp = GetHexValue(hex,hex); } else
925 if (strstr(hex,"EIP")==hex) { hex+=3; reg_eip = GetHexValue(hex,hex); } else
926 if (strstr(hex,"AX")==hex) { hex+=2; reg_ax = (Bit16u)GetHexValue(hex,hex); } else
927 if (strstr(hex,"BX")==hex) { hex+=2; reg_bx = (Bit16u)GetHexValue(hex,hex); } else
928 if (strstr(hex,"CX")==hex) { hex+=2; reg_cx = (Bit16u)GetHexValue(hex,hex); } else
929 if (strstr(hex,"DX")==hex) { hex+=2; reg_dx = (Bit16u)GetHexValue(hex,hex); } else
930 if (strstr(hex,"SI")==hex) { hex+=2; reg_si = (Bit16u)GetHexValue(hex,hex); } else
931 if (strstr(hex,"DI")==hex) { hex+=2; reg_di = (Bit16u)GetHexValue(hex,hex); } else
932 if (strstr(hex,"BP")==hex) { hex+=2; reg_bp = (Bit16u)GetHexValue(hex,hex); } else
933 if (strstr(hex,"SP")==hex) { hex+=2; reg_sp = (Bit16u)GetHexValue(hex,hex); } else
934 if (strstr(hex,"IP")==hex) { hex+=2; reg_ip = (Bit16u)GetHexValue(hex,hex); } else
935 if (strstr(hex,"CS")==hex) { hex+=2; SegSet16(cs,(Bit16u)GetHexValue(hex,hex)); } else
936 if (strstr(hex,"DS")==hex) { hex+=2; SegSet16(ds,(Bit16u)GetHexValue(hex,hex)); } else
937 if (strstr(hex,"ES")==hex) { hex+=2; SegSet16(es,(Bit16u)GetHexValue(hex,hex)); } else
938 if (strstr(hex,"FS")==hex) { hex+=2; SegSet16(fs,(Bit16u)GetHexValue(hex,hex)); } else
939 if (strstr(hex,"GS")==hex) { hex+=2; SegSet16(gs,(Bit16u)GetHexValue(hex,hex)); } else
940 if (strstr(hex,"SS")==hex) { hex+=2; SegSet16(ss,(Bit16u)GetHexValue(hex,hex)); } else
941 if (strstr(hex,"AF")==hex) { hex+=2; SETFLAGBIT(AF,GetHexValue(hex,hex)); } else
942 if (strstr(hex,"CF")==hex) { hex+=2; SETFLAGBIT(CF,GetHexValue(hex,hex)); } else
943 if (strstr(hex,"DF")==hex) { hex+=2; SETFLAGBIT(DF,GetHexValue(hex,hex)); } else
944 if (strstr(hex,"IF")==hex) { hex+=2; SETFLAGBIT(IF,GetHexValue(hex,hex)); } else
945 if (strstr(hex,"OF")==hex) { hex+=2; SETFLAGBIT(OF,GetHexValue(hex,hex)); } else
946 if (strstr(hex,"ZF")==hex) { hex+=2; SETFLAGBIT(ZF,GetHexValue(hex,hex)); } else
947 if (strstr(hex,"PF")==hex) { hex+=2; SETFLAGBIT(PF,GetHexValue(hex,hex)); } else
948 if (strstr(hex,"SF")==hex) { hex+=2; SETFLAGBIT(SF,GetHexValue(hex,hex)); } else
949 { return false; };
950 return true;
951 };
952
953 bool ParseCommand(char* str) {
954 char* found = str;
955 for(char* idx = found;*idx != 0; idx++)
956 *idx = toupper(*idx);
957
958 found = trim(found);
959 string s_found(found);
960 istringstream stream(s_found);
961 string command;
962 stream >> command;
963 string::size_type next = s_found.find_first_not_of(' ',command.size());
964 if(next == string::npos) next = command.size();
965 (s_found.erase)(0,next);
966 found = const_cast<char*>(s_found.c_str());
967
968 if (command == "MEMDUMP") { // Dump memory to file
969 Bit16u seg = (Bit16u)GetHexValue(found,found); found++;
970 Bit32u ofs = GetHexValue(found,found); found++;
971 Bit32u num = GetHexValue(found,found); found++;
972 SaveMemory(seg,ofs,num);
973 return true;
974 };
975
976 if (command == "MEMDUMPBIN") { // Dump memory to file bineary
977 Bit16u seg = (Bit16u)GetHexValue(found,found); found++;
978 Bit32u ofs = GetHexValue(found,found); found++;
979 Bit32u num = GetHexValue(found,found); found++;
980 SaveMemoryBin(seg,ofs,num);
981 return true;
982 };
983
984 if (command == "IV") { // Insert variable
985 Bit16u seg = (Bit16u)GetHexValue(found,found); found++;
986 Bit32u ofs = (Bit16u)GetHexValue(found,found); found++;
987 char name[16];
988 for (int i=0; i<16; i++) {
989 if (found[i] && (found[i]!=' ')) name[i] = found[i];
990 else { name[i] = 0; break; };
991 };
992 name[15] = 0;
993
994 if(!name[0]) return false;
995 DEBUG_ShowMsg("DEBUG: Created debug var %s at %04X:%04X\n",name,seg,ofs);
996 CDebugVar::InsertVariable(name,GetAddress(seg,ofs));
997 return true;
998 };
999
1000 if (command == "SV") { // Save variables
1001 char name[13];
1002 for (int i=0; i<12; i++) {
1003 if (found[i] && (found[i]!=' ')) name[i] = found[i];
1004 else { name[i] = 0; break; };
1005 };
1006 name[12] = 0;
1007 if(!name[0]) return false;
1008 DEBUG_ShowMsg("DEBUG: Variable list save (%s) : %s.\n",name,(CDebugVar::SaveVars(name)?"ok":"failure"));
1009 return true;
1010 };
1011
1012 if (command == "LV") { // load variables
1013 char name[13];
1014 for (int i=0; i<12; i++) {
1015 if (found[i] && (found[i]!=' ')) name[i] = found[i];
1016 else { name[i] = 0; break; };
1017 };
1018 name[12] = 0;
1019 if(!name[0]) return false;
1020 DEBUG_ShowMsg("DEBUG: Variable list load (%s) : %s.\n",name,(CDebugVar::LoadVars(name)?"ok":"failure"));
1021 return true;
1022 };
1023
1024 if (command == "SR") { // Set register value
1025 DEBUG_ShowMsg("DEBUG: Set Register %s.\n",(ChangeRegister(found)?"success":"failure"));
1026 return true;
1027 };
1028
1029 if (command == "SM") { // Set memory with following values
1030 Bit16u seg = (Bit16u)GetHexValue(found,found); found++;
1031 Bit32u ofs = GetHexValue(found,found); found++;
1032 Bit16u count = 0;
1033 while (*found) {
1034 while (*found==' ') found++;
1035 if (*found) {
1036 Bit8u value = (Bit8u)GetHexValue(found,found);
1037 if(*found) found++;
1038 mem_writeb_checked(GetAddress(seg,ofs+count),value);
1039 count++;
1040 }
1041 };
1042 DEBUG_ShowMsg("DEBUG: Memory changed.\n");
1043 return true;
1044 };
1045
1046 if (command == "BP") { // Add new breakpoint
1047 Bit16u seg = (Bit16u)GetHexValue(found,found);found++; // skip ":"
1048 Bit32u ofs = GetHexValue(found,found);
1049 CBreakpoint::AddBreakpoint(seg,ofs,false);
1050 DEBUG_ShowMsg("DEBUG: Set breakpoint at %04X:%04X\n",seg,ofs);
1051 return true;
1052 };
1053
1054 #if C_HEAVY_DEBUG
1055
1056 if (command == "BPM") { // Add new breakpoint
1057 Bit16u seg = (Bit16u)GetHexValue(found,found);found++; // skip ":"
1058 Bit32u ofs = GetHexValue(found,found);
1059 CBreakpoint::AddMemBreakpoint(seg,ofs);
1060 DEBUG_ShowMsg("DEBUG: Set memory breakpoint at %04X:%04X\n",seg,ofs);
1061 return true;
1062 };
1063
1064 if (command == "BPPM") { // Add new breakpoint
1065 Bit16u seg = (Bit16u)GetHexValue(found,found);found++; // skip ":"
1066 Bit32u ofs = GetHexValue(found,found);
1067 CBreakpoint* bp = CBreakpoint::AddMemBreakpoint(seg,ofs);
1068 if (bp) {
1069 bp->SetType(BKPNT_MEMORY_PROT);
1070 DEBUG_ShowMsg("DEBUG: Set prot-mode memory breakpoint at %04X:%08X\n",seg,ofs);
1071 }
1072 return true;
1073 };
1074
1075 if (command == "BPLM") { // Add new breakpoint
1076 Bit32u ofs = GetHexValue(found,found);
1077 CBreakpoint* bp = CBreakpoint::AddMemBreakpoint(0,ofs);
1078 if (bp) bp->SetType(BKPNT_MEMORY_LINEAR);
1079 DEBUG_ShowMsg("DEBUG: Set linear memory breakpoint at %08X\n",ofs);
1080 return true;
1081 };
1082
1083 #endif
1084
1085 if (command == "BPINT") { // Add Interrupt Breakpoint
1086 Bit8u intNr = (Bit8u)GetHexValue(found,found);
1087 bool all = !(*found);found++;
1088 Bit8u valAH = (Bit8u)GetHexValue(found,found);
1089 if ((valAH==0x00) && (*found=='*' || all)) {
1090 CBreakpoint::AddIntBreakpoint(intNr,BPINT_ALL,false);
1091 DEBUG_ShowMsg("DEBUG: Set interrupt breakpoint at INT %02X\n",intNr);
1092 } else {
1093 CBreakpoint::AddIntBreakpoint(intNr,valAH,false);
1094 DEBUG_ShowMsg("DEBUG: Set interrupt breakpoint at INT %02X AH=%02X\n",intNr,valAH);
1095 }
1096 return true;
1097 };
1098
1099 if (command == "BPLIST") {
1100 DEBUG_ShowMsg("Breakpoint list:\n");
1101 DEBUG_ShowMsg("-------------------------------------------------------------------------\n");
1102 CBreakpoint::ShowList();
1103 return true;
1104 };
1105
1106 if (command == "BPDEL") { // Delete Breakpoints
1107 Bit8u bpNr = (Bit8u)GetHexValue(found,found);
1108 if ((bpNr==0x00) && (*found=='*')) { // Delete all
1109 CBreakpoint::DeleteAll();
1110 DEBUG_ShowMsg("DEBUG: Breakpoints deleted.\n");
1111 } else {
1112 // delete single breakpoint
1113 DEBUG_ShowMsg("DEBUG: Breakpoint deletion %s.\n",(CBreakpoint::DeleteByIndex(bpNr)?"success":"failure"));
1114 }
1115 return true;
1116 };
1117
1118 if (command == "C") { // Set code overview
1119 Bit16u codeSeg = (Bit16u)GetHexValue(found,found); found++;
1120 Bit32u codeOfs = GetHexValue(found,found);
1121 DEBUG_ShowMsg("DEBUG: Set code overview to %04X:%04X\n",codeSeg,codeOfs);
1122 codeViewData.useCS = codeSeg;
1123 codeViewData.useEIP = codeOfs;
1124 return true;
1125 };
1126
1127 if (command == "D") { // Set data overview
1128 dataSeg = (Bit16u)GetHexValue(found,found); found++;
1129 dataOfs = GetHexValue(found,found);
1130 DEBUG_ShowMsg("DEBUG: Set data overview to %04X:%04X\n",dataSeg,dataOfs);
1131 return true;
1132 };
1133
1134 #if C_HEAVY_DEBUG
1135
1136 if (command == "LOG") { // Create Cpu normal log file
1137 cpuLogType = 1;
1138 command = "logcode";
1139 }
1140
1141 if (command == "LOGS") { // Create Cpu short log file
1142 cpuLogType = 0;
1143 command = "logcode";
1144 }
1145
1146 if (command == "LOGL") { // Create Cpu long log file
1147 cpuLogType = 2;
1148 command = "logcode";
1149 }
1150
1151 if (command == "logcode") { //Shared code between all logs
1152 DEBUG_ShowMsg("DEBUG: Starting log\n");
1153 cpuLogFile.open("LOGCPU.TXT");
1154 if (!cpuLogFile.is_open()) {
1155 DEBUG_ShowMsg("DEBUG: Logfile couldn't be created.\n");
1156 return false;
1157 }
1158 //Initialize log object
1159 cpuLogFile << hex << noshowbase << setfill('0') << uppercase;
1160 cpuLog = true;
1161 cpuLogCounter = GetHexValue(found,found);
1162
1163 debugging = false;
1164 CBreakpoint::ActivateBreakpoints(SegPhys(cs)+reg_eip,true);
1165 ignoreAddressOnce = SegPhys(cs)+reg_eip;
1166 DOSBOX_SetNormalLoop();
1167 return true;
1168 };
1169
1170 #endif
1171
1172 if (command == "INTT") { //trace int.
1173 Bit8u intNr = (Bit8u)GetHexValue(found,found);
1174 DEBUG_ShowMsg("DEBUG: Tracing INT %02X\n",intNr);
1175 CPU_HW_Interrupt(intNr);
1176 SetCodeWinStart();
1177 return true;
1178 };
1179
1180 if (command == "INT") { // start int.
1181 Bit8u intNr = (Bit8u)GetHexValue(found,found);
1182 DEBUG_ShowMsg("DEBUG: Starting INT %02X\n",intNr);
1183 CBreakpoint::AddBreakpoint(SegValue(cs),reg_eip, true);
1184 CBreakpoint::ActivateBreakpoints(SegPhys(cs)+reg_eip-1,true);
1185 debugging = false;
1186 DrawCode();
1187 DOSBOX_SetNormalLoop();
1188 CPU_HW_Interrupt(intNr);
1189 return true;
1190 };
1191
1192 if (command == "SELINFO") {
1193 while (found[0] == ' ') found++;
1194 char out1[200],out2[200];
1195 GetDescriptorInfo(found,out1,out2);
1196 DEBUG_ShowMsg("SelectorInfo %s:\n%s\n%s\n",found,out1,out2);
1197 return true;
1198 };
1199
1200 if (command == "DOS") {
1201 stream >> command;
1202 if (command == "MCBS") LogMCBS();
1203 return true;
1204 }
1205
1206 if (command == "GDT") {LogGDT(); return true;}
1207
1208 if (command == "LDT") {LogLDT(); return true;}
1209
1210 if (command == "IDT") {LogIDT(); return true;}
1211
1212 if (command == "PAGING") {LogPages(found); return true;}
1213
1214 if (command == "CPU") {LogCPUInfo(); return true;}
1215
1216 if (command == "INTVEC") {
1217 if (found[0] != 0) {
1218 OutputVecTable(found);
1219 return true;
1220 }
1221 };
1222
1223 if (command == "INTHAND") {
1224 if (found[0] != 0) {
1225 Bit8u intNr = (Bit8u)GetHexValue(found,found);
1226 DEBUG_ShowMsg("DEBUG: Set code overview to interrupt handler %X\n",intNr);
1227 codeViewData.useCS = mem_readw(intNr*4+2);
1228 codeViewData.useEIP = mem_readw(intNr*4);
1229 return true;
1230 }
1231 };
1232
1233 if(command == "EXTEND") { //Toggle additional data.
1234 showExtend = !showExtend;
1235 return true;
1236 };
1237
1238 if(command == "TIMERIRQ") { //Start a timer irq
1239 DEBUG_RaiseTimerIrq();
1240 DEBUG_ShowMsg("Debug: Timer Int started.\n");
1241 return true;
1242 };
1243
1244
1245 #if C_HEAVY_DEBUG
1246 if (command == "HEAVYLOG") { // Create Cpu log file
1247 logHeavy = !logHeavy;
1248 DEBUG_ShowMsg("DEBUG: Heavy cpu logging %s.\n",logHeavy?"on":"off");
1249 return true;
1250 };
1251
1252 if (command == "ZEROPROTECT") { //toggle zero protection
1253 zeroProtect = !zeroProtect;
1254 DEBUG_ShowMsg("DEBUG: Zero code execution protection %s.\n",zeroProtect?"on":"off");
1255 return true;
1256 };
1257
1258 #endif
1259 if (command == "HELP" || command == "?") {
1260 DEBUG_ShowMsg("Debugger commands (enter all values in hex or as register):\n");
1261 DEBUG_ShowMsg("--------------------------------------------------------------------------\n");
1262 DEBUG_ShowMsg("F3/F6 - Re-enter previous command.\n");
1263 DEBUG_ShowMsg("F5 - Run.\n");
1264 DEBUG_ShowMsg("F9 - Set/Remove breakpoint.\n");
1265 DEBUG_ShowMsg("F10/F11 - Step over / trace into instruction.\n");
1266 DEBUG_ShowMsg("ALT + D/E/S/X/B - Set data view to DS:SI/ES:DI/SS:SP/DS:DX/ES:BX.\n");
1267 DEBUG_ShowMsg("Escape - Clear input line.");
1268 DEBUG_ShowMsg("Up/Down - Move code view cursor.\n");
1269 DEBUG_ShowMsg("Page Up/Down - Scroll data view.\n");
1270 DEBUG_ShowMsg("Home/End - Scroll log messages.\n");
1271 DEBUG_ShowMsg("BP [segment]:[offset] - Set breakpoint.\n");
1272 DEBUG_ShowMsg("BPINT [intNr] * - Set interrupt breakpoint.\n");
1273 DEBUG_ShowMsg("BPINT [intNr] [ah] - Set interrupt breakpoint with ah.\n");
1274 #if C_HEAVY_DEBUG
1275 DEBUG_ShowMsg("BPM [segment]:[offset] - Set memory breakpoint (memory change).\n");
1276 DEBUG_ShowMsg("BPPM [selector]:[offset]- Set pmode-memory breakpoint (memory change).\n");
1277 DEBUG_ShowMsg("BPLM [linear address] - Set linear memory breakpoint (memory change).\n");
1278 #endif
1279 DEBUG_ShowMsg("BPLIST - List breakpoints.\n");
1280 DEBUG_ShowMsg("BPDEL [bpNr] / * - Delete breakpoint nr / all.\n");
1281 DEBUG_ShowMsg("C / D [segment]:[offset] - Set code / data view address.\n");
1282 DEBUG_ShowMsg("DOS MCBS - Show Memory Control Block chain.\n");
1283 DEBUG_ShowMsg("INT [nr] / INTT [nr] - Execute / Trace into interrupt.\n");
1284 #if C_HEAVY_DEBUG
1285 DEBUG_ShowMsg("LOG [num] - Write cpu log file.\n");
1286 DEBUG_ShowMsg("LOGS/LOGL [num] - Write short/long cpu log file.\n");
1287 DEBUG_ShowMsg("HEAVYLOG - Enable/Disable automatic cpu log when dosbox exits.\n");
1288 DEBUG_ShowMsg("ZEROPROTECT - Enable/Disable zero code execution detecion.\n");
1289 #endif
1290 DEBUG_ShowMsg("SR [reg] [value] - Set register value.\n");
1291 DEBUG_ShowMsg("SM [seg]:[off] [val] [.]..- Set memory with following values.\n");
1292
1293 DEBUG_ShowMsg("IV [seg]:[off] [name] - Create var name for memory address.\n");
1294 DEBUG_ShowMsg("SV [filename] - Save var list in file.\n");
1295 DEBUG_ShowMsg("LV [filename] - Load var list from file.\n");
1296
1297 DEBUG_ShowMsg("MEMDUMP [seg]:[off] [len] - Write memory to file memdump.txt.\n");
1298 DEBUG_ShowMsg("MEMDUMPBIN [s]:[o] [len] - Write memory to file memdump.bin.\n");
1299 DEBUG_ShowMsg("SELINFO [segName] - Show selector info.\n");
1300
1301 DEBUG_ShowMsg("INTVEC [filename] - Writes interrupt vector table to file.\n");
1302 DEBUG_ShowMsg("INTHAND [intNum] - Set code view to interrupt handler.\n");
1303
1304 DEBUG_ShowMsg("CPU - Display CPU status information.\n");
1305 DEBUG_ShowMsg("GDT - Lists descriptors of the GDT.\n");
1306 DEBUG_ShowMsg("LDT - Lists descriptors of the LDT.\n");
1307 DEBUG_ShowMsg("IDT - Lists descriptors of the IDT.\n");
1308 DEBUG_ShowMsg("PAGING [page] - Display content of page table.\n");
1309 DEBUG_ShowMsg("EXTEND - Toggle additional info.\n");
1310 DEBUG_ShowMsg("TIMERIRQ - Run the system timer.\n");
1311
1312 DEBUG_ShowMsg("HELP - Help\n");
1313
1314 return true;
1315 };
1316 return false;
1317 };
1318
1319 char* AnalyzeInstruction(char* inst, bool saveSelector) {
1320 static char result[256];
1321
1322 char instu[256];
1323 char prefix[3];
1324 Bit16u seg;
1325
1326 strcpy(instu,inst);
1327 upcase(instu);
1328
1329 result[0] = 0;
1330 char* pos = strchr(instu,'[');
1331 if (pos) {
1332 // Segment prefix ?
1333 if (*(pos-1)==':') {
1334 char* segpos = pos-3;
1335 prefix[0] = tolower(*segpos);
1336 prefix[1] = tolower(*(segpos+1));
1337 prefix[2] = 0;
1338 seg = (Bit16u)GetHexValue(segpos,segpos);
1339 } else {
1340 if (strstr(pos,"SP") || strstr(pos,"BP")) {
1341 seg = SegValue(ss);
1342 strcpy(prefix,"ss");
1343 } else {
1344 seg = SegValue(ds);
1345 strcpy(prefix,"ds");
1346 };
1347 };
1348
1349 pos++;
1350 Bit32u adr = GetHexValue(pos,pos);
1351 while (*pos!=']') {
1352 if (*pos=='+') {
1353 pos++;
1354 adr += GetHexValue(pos,pos);
1355 } else if (*pos=='-') {
1356 pos++;
1357 adr -= GetHexValue(pos,pos);
1358 } else
1359 pos++;
1360 };
1361 Bit32u address = GetAddress(seg,adr);
1362 if (!(get_tlb_readhandler(address)->flags & PFLAG_INIT)) {
1363 static char outmask[] = "%s:[%04X]=%02X";
1364
1365 if (cpu.pmode) outmask[6] = '8';
1366 switch (DasmLastOperandSize()) {
1367 case 8 : { Bit8u val = mem_readb(address);
1368 outmask[12] = '2';
1369 sprintf(result,outmask,prefix,adr,val);
1370 } break;
1371 case 16: { Bit16u val = mem_readw(address);
1372 outmask[12] = '4';
1373 sprintf(result,outmask,prefix,adr,val);
1374 } break;
1375 case 32: { Bit32u val = mem_readd(address);
1376 outmask[12] = '8';
1377 sprintf(result,outmask,prefix,adr,val);
1378 } break;
1379 }
1380 } else {
1381 sprintf(result,"[illegal]");
1382 }
1383 // Variable found ?
1384 CDebugVar* var = CDebugVar::FindVar(address);
1385 if (var) {
1386 // Replace occurance
1387 char* pos1 = strchr(inst,'[');
1388 char* pos2 = strchr(inst,']');
1389 if (pos1 && pos2) {
1390 char temp[256];
1391 strcpy(temp,pos2); // save end
1392 pos1++; *pos1 = 0; // cut after '['
1393 strcat(inst,var->GetName()); // add var name
1394 strcat(inst,temp); // add end
1395 };
1396 };
1397 // show descriptor info, if available
1398 if ((cpu.pmode) && saveSelector) {
1399 strcpy(curSelectorName,prefix);
1400 };
1401 };
1402 // If it is a callback add additional info
1403 pos = strstr(inst,"callback");
1404 if (pos) {
1405 pos += 9;
1406 Bitu nr = GetHexValue(pos,pos);
1407 const char* descr = CALLBACK_GetDescription(nr);
1408 if (descr) {
1409 strcat(inst," ("); strcat(inst,descr); strcat(inst,")");
1410 }
1411 };
1412 // Must be a jump
1413 if (instu[0] == 'J')
1414 {
1415 bool jmp = false;
1416 switch (instu[1]) {
1417 case 'A' : { jmp = (get_CF()?false:true) && (get_ZF()?false:true); // JA
1418 } break;
1419 case 'B' : { if (instu[2] == 'E') {
1420 jmp = (get_CF()?true:false) || (get_ZF()?true:false); // JBE
1421 } else {
1422 jmp = get_CF()?true:false; // JB
1423 }
1424 } break;
1425 case 'C' : { if (instu[2] == 'X') {
1426 jmp = reg_cx == 0; // JCXZ
1427 } else {
1428 jmp = get_CF()?true:false; // JC
1429 }
1430 } break;
1431 case 'E' : { jmp = get_ZF()?true:false; // JE
1432 } break;
1433 case 'G' : { if (instu[2] == 'E') {
1434 jmp = (get_SF()?true:false)==(get_OF()?true:false); // JGE
1435 } else {
1436 jmp = (get_ZF()?false:true) && ((get_SF()?true:false)==(get_OF()?true:false)); // JG
1437 }
1438 } break;
1439 case 'L' : { if (instu[2] == 'E') {
1440 jmp = (get_ZF()?true:false) || ((get_SF()?true:false)!=(get_OF()?true:false)); // JLE
1441 } else {
1442 jmp = (get_SF()?true:false)!=(get_OF()?true:false); // JL
1443 }
1444 } break;
1445 case 'M' : { jmp = true; // JMP
1446 } break;
1447 case 'N' : { switch (instu[2]) {
1448 case 'B' :
1449 case 'C' : { jmp = get_CF()?false:true; // JNB / JNC
1450 } break;
1451 case 'E' : { jmp = get_ZF()?false:true; // JNE
1452 } break;
1453 case 'O' : { jmp = get_OF()?false:true; // JNO
1454 } break;
1455 case 'P' : { jmp = get_PF()?false:true; // JNP
1456 } break;
1457 case 'S' : { jmp = get_SF()?false:true; // JNS
1458 } break;
1459 case 'Z' : { jmp = get_ZF()?false:true; // JNZ
1460 } break;
1461 }
1462 } break;
1463 case 'O' : { jmp = get_OF()?true:false; // JO
1464 } break;
1465 case 'P' : { if (instu[2] == 'O') {
1466 jmp = get_PF()?false:true; // JPO
1467 } else {
1468 jmp = get_SF()?true:false; // JP / JPE
1469 }
1470 } break;
1471 case 'S' : { jmp = get_SF()?true:false; // JS
1472 } break;
1473 case 'Z' : { jmp = get_ZF()?true:false; // JZ
1474 } break;
1475 }
1476 if (jmp) {
1477 pos = strchr(instu,'$');
1478 if (pos) {
1479 pos = strchr(instu,'+');
1480 if (pos) {
1481 strcpy(result,"(down)");
1482 } else {
1483 strcpy(result,"(up)");
1484 }
1485 }
1486 } else {
1487 sprintf(result,"(no jmp)");
1488 }
1489 }
1490 return result;
1491 };
1492
1493
1494 Bit32u DEBUG_CheckKeys(void) {
1495 Bits ret=0;
1496 int key=getch();
1497 if (key>0) {
1498 #if defined(WIN32) && defined(__PDCURSES__)
1499 switch (key) {
1500 case ALT_D:
1501 if (ungetch('D') != ERR) key=27;
1502 break;
1503 case ALT_E:
1504 if (ungetch('E') != ERR) key=27;
1505 break;
1506 case ALT_X:
1507 if (ungetch('X') != ERR) key=27;
1508 break;
1509 case ALT_B:
1510 if (ungetch('B') != ERR) key=27;
1511 break;
1512 case ALT_S:
1513 if (ungetch('S') != ERR) key=27;
1514 break;
1515 }
1516 #endif
1517 switch (toupper(key)) {
1518 case 27: // escape (a bit slow): Clears line. and processes alt commands.
1519 key=getch();
1520 if(key < 0) { //Purely escape Clear line
1521 codeViewData.inputStr[0] = 0;
1522 break;
1523 }
1524
1525 switch(toupper(key)) {
1526 case 'D' : // ALT - D: DS:SI
1527 dataSeg = SegValue(ds);
1528 if (cpu.pmode && !(reg_flags & FLAG_VM)) dataOfs = reg_esi;
1529 else dataOfs = reg_si;
1530 break;
1531 case 'E' : //ALT - E: es:di
1532 dataSeg = SegValue(es);
1533 if (cpu.pmode && !(reg_flags & FLAG_VM)) dataOfs = reg_edi;
1534 else dataOfs = reg_di;
1535 break;
1536 case 'X': //ALT - X: ds:dx
1537 dataSeg = SegValue(ds);
1538 if (cpu.pmode && !(reg_flags & FLAG_VM)) dataOfs = reg_edx;
1539 else dataOfs = reg_dx;
1540 break;
1541 case 'B' : //ALT -B: es:bx
1542 dataSeg = SegValue(es);
1543 if (cpu.pmode && !(reg_flags & FLAG_VM)) dataOfs = reg_ebx;
1544 else dataOfs = reg_bx;
1545 break;
1546 case 'S': //ALT - S: ss:sp
1547 dataSeg = SegValue(ss);
1548 if (cpu.pmode && !(reg_flags & FLAG_VM)) dataOfs = reg_esp;
1549 else dataOfs = reg_sp;
1550 break;
1551 default:
1552 break;
1553 }
1554 break;
1555 case KEY_PPAGE : dataOfs -= 16; break;
1556 case KEY_NPAGE : dataOfs += 16; break;
1557
1558 case KEY_DOWN: // down
1559 if (codeViewData.cursorPos<9) codeViewData.cursorPos++;
1560 else codeViewData.useEIP += codeViewData.firstInstSize;
1561 break;
1562 case KEY_UP: // up
1563 if (codeViewData.cursorPos>0) codeViewData.cursorPos--;
1564 else {
1565 Bitu bytes = 0;
1566 char dline[200];
1567 Bitu size = 0;
1568 Bit32u newEIP = codeViewData.useEIP - 1;
1569 if(codeViewData.useEIP) {
1570 for (; bytes < 10; bytes++) {
1571 PhysPt start = GetAddress(codeViewData.useCS,newEIP);
1572 size = DasmI386(dline, start, newEIP, cpu.code.big);
1573 if(codeViewData.useEIP == newEIP+size) break;
1574 newEIP--;
1575 }
1576 if (bytes>=10) newEIP = codeViewData.useEIP - 1;
1577 }
1578 codeViewData.useEIP = newEIP;
1579 }
1580 break;
1581 case KEY_HOME: // Home: scroll log page up
1582 DEBUG_RefreshPage(-1);
1583 break;
1584 case KEY_END: // End: scroll log page down
1585 DEBUG_RefreshPage(1);
1586 break;
1587 case KEY_F(6): // Re-enter previous command (f1-f4 generate rubbish at my place)
1588 case KEY_F(3): // Re-enter previous command
1589 // copy prevInputStr back into inputStr
1590 safe_strncpy(codeViewData.inputStr, codeViewData.prevInputStr, sizeof(codeViewData.inputStr));
1591 break;
1592 case KEY_F(5): // Run Program
1593 debugging=false;
1594 CBreakpoint::ActivateBreakpoints(SegPhys(cs)+reg_eip,true);
1595 ignoreAddressOnce = SegPhys(cs)+reg_eip;
1596 DOSBOX_SetNormalLoop();
1597 break;
1598 case KEY_F(9): // Set/Remove TBreakpoint
1599 { PhysPt ptr = GetAddress(codeViewData.cursorSeg,codeViewData.cursorOfs);
1600 if (CBreakpoint::IsBreakpoint(ptr)) CBreakpoint::DeleteBreakpoint(ptr);
1601 else CBreakpoint::AddBreakpoint(codeViewData.cursorSeg, codeViewData.cursorOfs, false);
1602 }
1603 break;
1604 case KEY_F(10): // Step over inst
1605 if (StepOver()) return 0;
1606 else {
1607 exitLoop = false;
1608 skipFirstInstruction = true; // for heavy debugger
1609 CPU_Cycles = 1;
1610 ret=(*cpudecoder)();
1611 SetCodeWinStart();
1612 CBreakpoint::ignoreOnce = 0;
1613 }
1614 break;
1615 case KEY_F(11): // trace into
1616 exitLoop = false;
1617 skipFirstInstruction = true; // for heavy debugger
1618 CPU_Cycles = 1;
1619 ret = (*cpudecoder)();
1620 SetCodeWinStart();
1621 CBreakpoint::ignoreOnce = 0;
1622 break;
1623 case 0x0A: //Parse typed Command
1624 codeViewData.inputMode = true;
1625 if(ParseCommand(codeViewData.inputStr)) {
1626 // copy inputStr to prevInputStr so we can restore it if the user hits F3
1627 safe_strncpy(codeViewData.prevInputStr, codeViewData.inputStr, sizeof(codeViewData.prevInputStr));
1628 // clear input line ready for next command
1629 codeViewData.inputStr[0] = 0;
1630 }
1631 break;
1632 case 0x107: //backspace (linux)
1633 case 0x7f: //backspace in some terminal emulators (linux)
1634 case 0x08: // delete
1635 if (strlen(codeViewData.inputStr)>0) codeViewData.inputStr[strlen(codeViewData.inputStr)-1] = 0;
1636 break;
1637 default : if ((key>=32) && (key<=128) && (strlen(codeViewData.inputStr)<253)) {
1638 Bit32u len = strlen(codeViewData.inputStr);
1639 codeViewData.inputStr[len] = char(key);
1640 codeViewData.inputStr[len+1] = 0;
1641 }
1642 break;
1643
1644 }
1645 if (ret<0) return ret;
1646 if (ret>0) {
1647 ret=(*CallBack_Handlers[ret])();
1648 if (ret) {
1649 exitLoop=true;
1650 CPU_Cycles=CPU_CycleLeft=0;
1651 return ret;
1652 }
1653 }
1654 ret=0;
1655 DEBUG_DrawScreen();
1656 }
1657 return ret;
1658 };
1659
1660 Bitu DEBUG_Loop(void) {
1661 //TODO Disable sound
1662 GFX_Events();
1663 // Interrupt started ? - then skip it
1664 Bit16u oldCS = SegValue(cs);
1665 Bit32u oldEIP = reg_eip;
1666 PIC_runIRQs();
1667 SDL_Delay(1);
1668 if ((oldCS!=SegValue(cs)) || (oldEIP!=reg_eip)) {
1669 CBreakpoint::AddBreakpoint(oldCS,oldEIP,true);
1670 CBreakpoint::ActivateBreakpoints(SegPhys(cs)+reg_eip,true);
1671 debugging=false;
1672 DOSBOX_SetNormalLoop();
1673 return 0;
1674 }
1675 return DEBUG_CheckKeys();
1676 }
1677
1678 void DEBUG_Enable(bool pressed) {
1679 if (!pressed)
1680 return;
1681 static bool showhelp=false;
1682 debugging=true;
1683 SetCodeWinStart();
1684 DEBUG_DrawScreen();
1685 DOSBOX_SetLoop(&DEBUG_Loop);
1686 if(!showhelp) {
1687 showhelp=true;
1688 DEBUG_ShowMsg("***| TYPE HELP (+ENTER) TO GET AN OVERVIEW OF ALL COMMANDS |***\n");
1689 }
1690 KEYBOARD_ClrBuffer();
1691 }
1692
1693 void DEBUG_DrawScreen(void) {
1694 DrawData();
1695 DrawCode();
1696 DrawRegisters();
1697 DrawVariables();
1698 }
1699
1700 static void DEBUG_RaiseTimerIrq(void) {
1701 PIC_ActivateIRQ(0);
1702 }
1703
1704 // Display the content of the MCB chain starting with the MCB at the specified segment.
1705 static void LogMCBChain(Bit16u mcb_segment) {
1706 DOS_MCB mcb(mcb_segment);
1707 char filename[9]; // 8 characters plus a terminating NUL
1708 const char *psp_seg_note;
1709 PhysPt dataAddr = PhysMake(dataSeg,dataOfs);// location being viewed in the "Data Overview"
1710
1711 // loop forever, breaking out of the loop once we've processed the last MCB
1712 while (true) {
1713 // verify that the type field is valid
1714 if (mcb.GetType()!=0x4d && mcb.GetType()!=0x5a) {
1715 LOG(LOG_MISC,LOG_ERROR)("MCB chain broken at %04X:0000!",mcb_segment);
1716 return;
1717 }
1718
1719 mcb.GetFileName(filename);
1720
1721 // some PSP segment values have special meanings
1722 switch (mcb.GetPSPSeg()) {
1723 case MCB_FREE:
1724 psp_seg_note = "(free)";
1725 break;
1726 case MCB_DOS:
1727 psp_seg_note = "(DOS)";
1728 break;
1729 default:
1730 psp_seg_note = "";
1731 }
1732
1733 LOG(LOG_MISC,LOG_ERROR)(" %04X %12u %04X %-7s %s",mcb_segment,mcb.GetSize() << 4,mcb.GetPSPSeg(), psp_seg_note, filename);
1734
1735 // print a message if dataAddr is within this MCB's memory range
1736 PhysPt mcbStartAddr = PhysMake(mcb_segment+1,0);
1737 PhysPt mcbEndAddr = PhysMake(mcb_segment+1+mcb.GetSize(),0);
1738 if (dataAddr >= mcbStartAddr && dataAddr < mcbEndAddr) {
1739 LOG(LOG_MISC,LOG_ERROR)(" (data addr %04hX:%04X is %u bytes past this MCB)",dataSeg,dataOfs,dataAddr - mcbStartAddr);
1740 }
1741
1742 // if we've just processed the last MCB in the chain, break out of the loop
1743 if (mcb.GetType()==0x5a) {
1744 break;
1745 }
1746 // else, move to the next MCB in the chain
1747 mcb_segment+=mcb.GetSize()+1;
1748 mcb.SetPt(mcb_segment);
1749 }
1750 }
1751
1752 // Display the content of all Memory Control Blocks.
1753 static void LogMCBS(void)
1754 {
1755 LOG(LOG_MISC,LOG_ERROR)("MCB Seg Size (bytes) PSP Seg (notes) Filename");
1756 LOG(LOG_MISC,LOG_ERROR)("Conventional memory:");
1757 LogMCBChain(dos.firstMCB);
1758
1759 LOG(LOG_MISC,LOG_ERROR)("Upper memory:");
1760 LogMCBChain(dos_infoblock.GetStartOfUMBChain());
1761 }
1762
1763 static void LogGDT(void)
1764 {
1765 char out1[512];
1766 Descriptor desc;
1767 Bitu length = cpu.gdt.GetLimit();
1768 PhysPt address = cpu.gdt.GetBase();
1769 PhysPt max = address + length;
1770 Bitu i = 0;
1771 LOG(LOG_MISC,LOG_ERROR)("GDT Base:%08X Limit:%08X",address,length);
1772 while (address<max) {
1773 desc.Load(address);
1774 sprintf(out1,"%04X: b:%08X type: %02X parbg",(i<<3),desc.GetBase(),desc.saved.seg.type);
1775 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1776 sprintf(out1," l:%08X dpl : %01X %1X%1X%1X%1X%1X",desc.GetLimit(),desc.saved.seg.dpl,desc.saved.seg.p,desc.saved.seg.avl,desc.saved.seg.r,desc.saved.seg.big,desc.saved.seg.g);
1777 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1778 address+=8; i++;
1779 };
1780 };
1781
1782 static void LogLDT(void) {
1783 char out1[512];
1784 Descriptor desc;
1785 Bitu ldtSelector = cpu.gdt.SLDT();
1786 if (!cpu.gdt.GetDescriptor(ldtSelector,desc)) return;
1787 Bitu length = desc.GetLimit();
1788 PhysPt address = desc.GetBase();
1789 PhysPt max = address + length;
1790 Bitu i = 0;
1791 LOG(LOG_MISC,LOG_ERROR)("LDT Base:%08X Limit:%08X",address,length);
1792 while (address<max) {
1793 desc.Load(address);
1794 sprintf(out1,"%04X: b:%08X type: %02X parbg",(i<<3)|4,desc.GetBase(),desc.saved.seg.type);
1795 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1796 sprintf(out1," l:%08X dpl : %01X %1X%1X%1X%1X%1X",desc.GetLimit(),desc.saved.seg.dpl,desc.saved.seg.p,desc.saved.seg.avl,desc.saved.seg.r,desc.saved.seg.big,desc.saved.seg.g);
1797 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1798 address+=8; i++;
1799 };
1800 };
1801
1802 static void LogIDT(void) {
1803 char out1[512];
1804 Descriptor desc;
1805 Bitu address = 0;
1806 while (address<256*8) {
1807 if (cpu.idt.GetDescriptor(address,desc)) {
1808 sprintf(out1,"%04X: sel:%04X off:%02X",address/8,desc.GetSelector(),desc.GetOffset());
1809 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1810 }
1811 address+=8;
1812 };
1813 };
1814
1815 void LogPages(char* selname) {
1816 char out1[512];
1817 if (paging.enabled) {
1818 Bitu sel = GetHexValue(selname,selname);
1819 if ((sel==0x00) && ((*selname==0) || (*selname=='*'))) {
1820 for (int i=0; i<0xfffff; i++) {
1821 Bitu table_addr=(paging.base.page<<12)+(i >> 10)*4;
1822 X86PageEntry table;
1823 table.load=phys_readd(table_addr);
1824 if (table.block.p) {
1825 X86PageEntry entry;
1826 Bitu entry_addr=(table.block.base<<12)+(i & 0x3ff)*4;
1827 entry.load=phys_readd(entry_addr);
1828 if (entry.block.p) {
1829 sprintf(out1,"page %05Xxxx -> %04Xxxx flags [uw] %x:%x::%x:%x [d=%x|a=%x]",
1830 i,entry.block.base,entry.block.us,table.block.us,
1831 entry.block.wr,table.block.wr,entry.block.d,entry.block.a);
1832 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1833 }
1834 }
1835 }
1836 } else {
1837 Bitu table_addr=(paging.base.page<<12)+(sel >> 10)*4;
1838 X86PageEntry table;
1839 table.load=phys_readd(table_addr);
1840 if (table.block.p) {
1841 X86PageEntry entry;
1842 Bitu entry_addr=(table.block.base<<12)+(sel & 0x3ff)*4;
1843 entry.load=phys_readd(entry_addr);
1844 sprintf(out1,"page %05Xxxx -> %04Xxxx flags [puw] %x:%x::%x:%x::%x:%x",sel,entry.block.base,entry.block.p,table.block.p,entry.block.us,table.block.us,entry.block.wr,table.block.wr);
1845 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1846 } else {
1847 sprintf(out1,"pagetable %03X not present, flags [puw] %x::%x::%x",(sel >> 10),table.block.p,table.block.us,table.block.wr);
1848 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1849 }
1850 }
1851 }
1852 };
1853
1854 static void LogCPUInfo(void) {
1855 char out1[512];
1856 sprintf(out1,"cr0:%08X cr2:%08X cr3:%08X cpl=%x",cpu.cr0,paging.cr2,paging.cr3,cpu.cpl);
1857 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1858 sprintf(out1,"eflags:%08X [vm=%x iopl=%x nt=%x]",reg_flags,GETFLAG(VM)>>17,GETFLAG(IOPL)>>12,GETFLAG(NT)>>14);
1859 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1860 sprintf(out1,"GDT base=%08X limit=%08X",cpu.gdt.GetBase(),cpu.gdt.GetLimit());
1861 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1862 sprintf(out1,"IDT base=%08X limit=%08X",cpu.idt.GetBase(),cpu.idt.GetLimit());
1863 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1864
1865 Bitu sel=CPU_STR();
1866 Descriptor desc;
1867 if (cpu.gdt.GetDescriptor(sel,desc)) {
1868 sprintf(out1,"TR selector=%04X, base=%08X limit=%08X*%X",sel,desc.GetBase(),desc.GetLimit(),desc.saved.seg.g?0x4000:1);
1869 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1870 }
1871 sel=CPU_SLDT();
1872 if (cpu.gdt.GetDescriptor(sel,desc)) {
1873 sprintf(out1,"LDT selector=%04X, base=%08X limit=%08X*%X",sel,desc.GetBase(),desc.GetLimit(),desc.saved.seg.g?0x4000:1);
1874 LOG(LOG_MISC,LOG_ERROR)("%s",out1);
1875 }
1876 };
1877
1878 #if C_HEAVY_DEBUG
1879 static void LogInstruction(Bit16u segValue, Bit32u eipValue, ofstream& out) {
1880 static char empty[23] = { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,0 };
1881
1882 PhysPt start = GetAddress(segValue,eipValue);
1883 char dline[200];Bitu size;
1884 size = DasmI386(dline, start, reg_eip, cpu.code.big);
1885 char* res = empty;
1886 if (showExtend && (cpuLogType > 0) ) {
1887 res = AnalyzeInstruction(dline,false);
1888 if (!res || !(*res)) res = empty;
1889 Bitu reslen = strlen(res);
1890 if (reslen<22) for (Bitu i=0; i<22-reslen; i++) res[reslen+i] = ' '; res[22] = 0;
1891 };
1892 Bitu len = strlen(dline);
1893 if (len<30) for (Bitu i=0; i<30-len; i++) dline[len + i] = ' '; dline[30] = 0;
1894
1895 // Get register values
1896
1897 if(cpuLogType == 0) {
1898 out << setw(4) << SegValue(cs) << ":" << setw(4) << reg_eip << " " << dline;
1899 } else if (cpuLogType == 1) {
1900 out << setw(4) << SegValue(cs) << ":" << setw(8) << reg_eip << " " << dline << " " << res;
1901 } else if (cpuLogType == 2) {
1902 char ibytes[200]=""; char tmpc[200];
1903 for (Bitu i=0; i<size; i++) {
1904 Bit8u value;
1905 if (mem_readb_checked(start+i,&value)) sprintf(tmpc,"?? ",value);
1906 else sprintf(tmpc,"%02X ",value);
1907 strcat(ibytes,tmpc);
1908 }
1909 len = strlen(ibytes);
1910 if (len<21) { for (Bitu i=0; i<21-len; i++) ibytes[len + i] =' '; ibytes[21]=0;} //NOTE THE BRACKETS
1911 out << setw(4) << SegValue(cs) << ":" << setw(8) << reg_eip << " " << dline << " " << res << " " << ibytes;
1912 }
1913
1914 out << " EAX:" << setw(8) << reg_eax << " EBX:" << setw(8) << reg_ebx
1915 << " ECX:" << setw(8) << reg_ecx << " EDX:" << setw(8) << reg_edx
1916 << " ESI:" << setw(8) << reg_esi << " EDI:" << setw(8) << reg_edi
1917 << " EBP:" << setw(8) << reg_ebp << " ESP:" << setw(8) << reg_esp
1918 << " DS:" << setw(4) << SegValue(ds)<< " ES:" << setw(4) << SegValue(es);
1919
1920 if(cpuLogType == 0) {
1921 out << " SS:" << setw(4) << SegValue(ss) << " C" << (get_CF()>0) << " Z" << (get_ZF()>0)
1922 << " S" << (get_SF()>0) << " O" << (get_OF()>0) << " I" << GETFLAGBOOL(IF);
1923 } else {
1924 out << " FS:" << setw(4) << SegValue(fs) << " GS:" << setw(4) << SegValue(gs)
1925 << " SS:" << setw(4) << SegValue(ss)
1926 << " CF:" << (get_CF()>0) << " ZF:" << (get_ZF()>0) << " SF:" << (get_SF()>0)
1927 << " OF:" << (get_OF()>0) << " AF:" << (get_AF()>0) << " PF:" << (get_PF()>0)
1928 << " IF:" << GETFLAGBOOL(IF);
1929 }
1930 if(cpuLogType == 2) {
1931 out << " TF:" << GETFLAGBOOL(TF) << " VM:" << GETFLAGBOOL(VM) <<" FLG:" << setw(8) << reg_flags
1932 << " CR0:" << setw(8) << cpu.cr0;
1933 }
1934 out << endl;
1935 };
1936 #endif
1937
1938 // DEBUG.COM stuff
1939
1940 class DEBUG : public Program {
1941 public:
1942 DEBUG() { pDebugcom = this; active = false; };
1943 ~DEBUG() { pDebugcom = 0; };
1944
1945 bool IsActive() { return active; };
1946
1947 void Run(void)
1948 {
1949 if(cmd->FindExist("/NOMOUSE",false)) {
1950 real_writed(0,0x33<<2,0);
1951 return;
1952 }
1953
1954 char filename[128];
1955 char args[256+1];
1956
1957 cmd->FindCommand(1,temp_line);
1958 safe_strncpy(filename,temp_line.c_str(),128);
1959 // Read commandline
1960 Bit16u i =2;
1961 bool ok = false;
1962 args[0] = 0;
1963 for (;cmd->FindCommand(i++,temp_line)==true;) {
1964 strncat(args,temp_line.c_str(),256);
1965 strncat(args," ",256);
1966 }
1967 // Start new shell and execute prog
1968 active = true;
1969 // Save cpu state....
1970 Bit16u oldcs = SegValue(cs);
1971 Bit32u oldeip = reg_eip;
1972 Bit16u oldss = SegValue(ss);
1973 Bit32u oldesp = reg_esp;
1974
1975 // Start shell
1976 DOS_Shell shell;
1977 shell.Execute(filename,args);
1978
1979 // set old reg values
1980 SegSet16(ss,oldss);
1981 reg_esp = oldesp;
1982 SegSet16(cs,oldcs);
1983 reg_eip = oldeip;
1984 };
1985
1986 private:
1987 bool active;
1988 };
1989
1990 void DEBUG_CheckExecuteBreakpoint(Bit16u seg, Bit32u off)
1991 {
1992 if (pDebugcom && pDebugcom->IsActive()) {
1993 CBreakpoint::AddBreakpoint(seg,off,true);
1994 CBreakpoint::ActivateBreakpoints(SegPhys(cs)+reg_eip,true);
1995 pDebugcom = 0;
1996 };
1997 };
1998
1999 Bitu DEBUG_EnableDebugger(void)
2000 {
2001 exitLoop = true;
2002 DEBUG_Enable(true);
2003 CPU_Cycles=CPU_CycleLeft=0;
2004 return 0;
2005 };
2006
2007 static void DEBUG_ProgramStart(Program * * make) {
2008 *make=new DEBUG;
2009 }
2010
2011 // INIT
2012
2013 void DEBUG_SetupConsole(void) {
2014 #ifdef WIN32
2015 WIN32_Console();
2016 #else
2017 tcgetattr(0,&consolesettings);
2018 printf("\e[8;50;80t"); //resize terminal
2019 fflush(NULL);
2020 #endif
2021 memset((void *)&dbg,0,sizeof(dbg));
2022 debugging=false;
2023 dbg.active_win=3;
2024 input_count=0;
2025 /* Start the Debug Gui */
2026 DBGUI_StartUp();
2027 }
2028
2029 static void DEBUG_ShutDown(Section * /*sec*/) {
2030 CBreakpoint::DeleteAll();
2031 CDebugVar::DeleteAll();
2032 curs_set(old_cursor_state);
2033 #ifndef WIN32
2034 tcsetattr(0, TCSANOW,&consolesettings);
2035 // printf("\e[0m\e[2J"); //Seems to destroy scrolling
2036 printf("\ec");
2037 fflush(NULL);
2038 #endif
2039 }
2040
2041 Bitu debugCallback;
2042
2043 void DEBUG_Init(Section* sec) {
2044
2045 MSG_Add("DEBUG_CONFIGFILE_HELP","Debugger related options.\n");
2046 DEBUG_DrawScreen();
2047 /* Add some keyhandlers */
2048 MAPPER_AddHandler(DEBUG_Enable,MK_pause,MMOD2,"debugger","Debugger");
2049 /* Clear the TBreakpoint list */
2050 memset((void*)&codeViewData,0,sizeof(codeViewData));
2051 /* setup debug.com */
2052 PROGRAMS_MakeFile("DEBUG.COM",DEBUG_ProgramStart);
2053 /* Setup callback */
2054 debugCallback=CALLBACK_Allocate();
2055 CALLBACK_Setup(debugCallback,DEBUG_EnableDebugger,CB_RETF,"debugger");
2056 /* shutdown function */
2057 sec->AddDestroyFunction(&DEBUG_ShutDown);
2058 }
2059
2060 // DEBUGGING VAR STUFF
2061
2062 void CDebugVar::InsertVariable(char* name, PhysPt adr)
2063 {
2064 varList.push_back(new CDebugVar(name,adr));
2065 };
2066
2067 void CDebugVar::DeleteAll(void)
2068 {
2069 std::list<CDebugVar*>::iterator i;
2070 CDebugVar* bp;
2071 for(i=varList.begin(); i != varList.end(); i++) {
2072 bp = static_cast<CDebugVar*>(*i);
2073 delete bp;
2074 };
2075 (varList.clear)();
2076 };
2077
2078 CDebugVar* CDebugVar::FindVar(PhysPt pt)
2079 {
2080 std::list<CDebugVar*>::iterator i;
2081 CDebugVar* bp;
2082 for(i=varList.begin(); i != varList.end(); i++) {
2083 bp = static_cast<CDebugVar*>(*i);
2084 if (bp->GetAdr()==pt) return bp;
2085 };
2086 return 0;
2087 };
2088
2089 bool CDebugVar::SaveVars(char* name)
2090 {
2091 FILE* f = fopen(name,"wb+");
2092 if (!f) return false;
2093 if (varList.size()>65535) return false;
2094
2095 // write number of vars
2096 Bit16u num = (Bit16u)varList.size();
2097 fwrite(&num,1,sizeof(num),f);
2098
2099 std::list<CDebugVar*>::iterator i;
2100 CDebugVar* bp;
2101 for(i=varList.begin(); i != varList.end(); i++) {
2102 bp = static_cast<CDebugVar*>(*i);
2103 // name
2104 fwrite(bp->GetName(),1,16,f);
2105 // adr
2106 PhysPt adr = bp->GetAdr();
2107 fwrite(&adr,1,sizeof(adr),f);
2108 };
2109 fclose(f);
2110 return true;
2111 };
2112
2113 bool CDebugVar::LoadVars(char* name)
2114 {
2115 FILE* f = fopen(name,"rb");
2116 if (!f) return false;
2117
2118 // read number of vars
2119 Bit16u num;
2120 if (fread(&num,sizeof(num),1,f) != 1) return false;
2121
2122 for (Bit16u i=0; i<num; i++) {
2123 char name[16];
2124 // name
2125 if (fread(name,16,1,f) != 1) break;
2126 // adr
2127 PhysPt adr;
2128 if (fread(&adr,sizeof(adr),1,f) != 1) break;
2129 // insert
2130 InsertVariable(name,adr);
2131 };
2132 fclose(f);
2133 return true;
2134 };
2135
2136 static void SaveMemory(Bit16u seg, Bit32u ofs1, Bit32u num) {
2137 FILE* f = fopen("MEMDUMP.TXT","wt");
2138 if (!f) {
2139 DEBUG_ShowMsg("DEBUG: Memory dump failed.\n");
2140 return;
2141 }
2142
2143 char buffer[128];
2144 char temp[16];
2145
2146 while (num>16) {
2147 sprintf(buffer,"%04X:%04X ",seg,ofs1);
2148 for (Bit16u x=0; x<16; x++) {
2149 Bit8u value;
2150 if (mem_readb_checked(GetAddress(seg,ofs1+x),&value)) sprintf(temp,"?? ",value);
2151 else sprintf(temp,"%02X ",value);
2152 strcat(buffer,temp);
2153 }
2154 ofs1+=16;
2155 num-=16;
2156
2157 fprintf(f,"%s\n",buffer);
2158 }
2159 if (num>0) {
2160 sprintf(buffer,"%04X:%04X ",seg,ofs1);
2161 for (Bit16u x=0; x<num; x++) {
2162 Bit8u value;
2163 if (mem_readb_checked(GetAddress(seg,ofs1+x),&value)) sprintf(temp,"?? ",value);
2164 else sprintf(temp,"%02X ",value);
2165 strcat(buffer,temp);
2166 }
2167 fprintf(f,"%s\n",buffer);
2168 }
2169 fclose(f);
2170 DEBUG_ShowMsg("DEBUG: Memory dump success.\n");
2171 }
2172
2173 static void SaveMemoryBin(Bit16u seg, Bit32u ofs1, Bit32u num) {
2174 FILE* f = fopen("MEMDUMP.BIN","wb");
2175 if (!f) {
2176 DEBUG_ShowMsg("DEBUG: Memory binary dump failed.\n");
2177 return;
2178 }
2179
2180 for (Bitu x = 0; x < num;x++) {
2181 Bit8u val;
2182 if (mem_readb_checked(GetAddress(seg,ofs1+x),&val)) val=0;
2183 fwrite(&val,1,1,f);
2184 }
2185
2186 fclose(f);
2187 DEBUG_ShowMsg("DEBUG: Memory dump binary success.\n");
2188 }
2189
2190 static void OutputVecTable(char* filename) {
2191 FILE* f = fopen(filename, "wt");
2192 if (!f)
2193 {
2194 DEBUG_ShowMsg("DEBUG: Output of interrupt vector table failed.\n");
2195 return;
2196 }
2197
2198 for (int i=0; i<256; i++)
2199 fprintf(f,"INT %02X: %04X:%04X\n", i, mem_readw(i*4+2), mem_readw(i*4));
2200
2201 fclose(f);
2202 DEBUG_ShowMsg("DEBUG: Interrupt vector table written to %s.\n", filename);
2203 }
2204
2205 #define DEBUG_VAR_BUF_LEN 16
2206 static void DrawVariables(void) {
2207 if (CDebugVar::varList.empty()) return;
2208
2209 std::list<CDebugVar*>::iterator i;
2210 CDebugVar *dv;
2211 char buffer[DEBUG_VAR_BUF_LEN];
2212
2213 int idx = 0;
2214 for(i=CDebugVar::varList.begin(); i != CDebugVar::varList.end(); i++, idx++) {
2215
2216 if (idx == 4*3) {
2217 /* too many variables */
2218 break;
2219 }
2220
2221 dv = static_cast<CDebugVar*>(*i);
2222
2223 Bit16u value;
2224 if (mem_readw_checked(dv->GetAdr(),&value))
2225 snprintf(buffer,DEBUG_VAR_BUF_LEN, "??????", value);
2226 else
2227 snprintf(buffer,DEBUG_VAR_BUF_LEN, "0x%04x", value);
2228
2229 int y = idx / 3;
2230 int x = (idx % 3) * 26;
2231 mvwprintw(dbg.win_var, y, x, dv->GetName());
2232 mvwprintw(dbg.win_var, y, (x + DEBUG_VAR_BUF_LEN + 1) , buffer);
2233 }
2234
2235 wrefresh(dbg.win_var);
2236 };
2237 #undef DEBUG_VAR_BUF_LEN
2238 // HEAVY DEBUGGING STUFF
2239
2240 #if C_HEAVY_DEBUG
2241
2242 const Bit32u LOGCPUMAX = 20000;
2243
2244 static Bit16u logCpuCS [LOGCPUMAX];
2245 static Bit32u logCpuEIP[LOGCPUMAX];
2246 static Bit32u logCount = 0;
2247
2248 struct TLogInst {
2249 Bit16u s_cs;
2250 Bit32u eip;
2251 Bit32u eax;
2252 Bit32u ebx;
2253 Bit32u ecx;
2254 Bit32u edx;
2255 Bit32u esi;
2256 Bit32u edi;
2257 Bit32u ebp;
2258 Bit32u esp;
2259 Bit16u s_ds;
2260 Bit16u s_es;
2261 Bit16u s_fs;
2262 Bit16u s_gs;
2263 Bit16u s_ss;
2264 bool c;
2265 bool z;
2266 bool s;
2267 bool o;
2268 bool a;
2269 bool p;
2270 bool i;
2271 char dline[31];
2272 char res[23];
2273 };
2274
2275 TLogInst logInst[LOGCPUMAX];
2276
2277 void DEBUG_HeavyLogInstruction(void) {
2278
2279 static char empty[23] = { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,0 };
2280
2281 PhysPt start = GetAddress(SegValue(cs),reg_eip);
2282 char dline[200];
2283 Bitu size = DasmI386(dline, start, reg_eip, cpu.code.big);
2284 char* res = empty;
2285 if (showExtend) {
2286 res = AnalyzeInstruction(dline,false);
2287 if (!res || !(*res)) res = empty;
2288 Bitu reslen = strlen(res);
2289 if (reslen<22) for (Bitu i=0; i<22-reslen; i++) res[reslen+i] = ' '; res[22] = 0;
2290 };
2291
2292 Bitu len = strlen(dline);
2293 if (len < 30) for (Bitu i=0; i < 30-len; i++) dline[len+i] = ' ';
2294 dline[30] = 0;
2295
2296 TLogInst & inst = logInst[logCount];
2297 strcpy(inst.dline,dline);
2298 inst.s_cs = SegValue(cs);
2299 inst.eip = reg_eip;
2300 strcpy(inst.res,res);
2301 inst.eax = reg_eax;
2302 inst.ebx = reg_ebx;
2303 inst.ecx = reg_ecx;
2304 inst.edx = reg_edx;
2305 inst.esi = reg_esi;
2306 inst.edi = reg_edi;
2307 inst.ebp = reg_ebp;
2308 inst.esp = reg_esp;
2309 inst.s_ds = SegValue(ds);
2310 inst.s_es = SegValue(es);
2311 inst.s_fs = SegValue(fs);
2312 inst.s_gs = SegValue(gs);
2313 inst.s_ss = SegValue(ss);
2314 inst.c = get_CF()>0;
2315 inst.z = get_ZF()>0;
2316 inst.s = get_SF()>0;
2317 inst.o = get_OF()>0;
2318 inst.a = get_AF()>0;
2319 inst.p = get_PF()>0;
2320 inst.i = GETFLAGBOOL(IF);
2321
2322 if (++logCount >= LOGCPUMAX) logCount = 0;
2323 };
2324
2325 void DEBUG_HeavyWriteLogInstruction(void) {
2326 if (!logHeavy) return;
2327 logHeavy = false;
2328
2329 DEBUG_ShowMsg("DEBUG: Creating cpu log LOGCPU_INT_CD.TXT\n");
2330
2331 ofstream out("LOGCPU_INT_CD.TXT");
2332 if (!out.is_open()) {
2333 DEBUG_ShowMsg("DEBUG: Failed.\n");
2334 return;
2335 }
2336 out << hex << noshowbase << setfill('0') << uppercase;
2337 Bit32u startLog = logCount;
2338 do {
2339 // Write Intructions
2340 TLogInst & inst = logInst[startLog];
2341 out << setw(4) << inst.s_cs << ":" << setw(8) << inst.eip << " "
2342 << inst.dline << " " << inst.res << " EAX:" << setw(8)<< inst.eax
2343 << " EBX:" << setw(8) << inst.ebx << " ECX:" << setw(8) << inst.ecx
2344 << " EDX:" << setw(8) << inst.edx << " ESI:" << setw(8) << inst.esi
2345 << " EDI:" << setw(8) << inst.edi << " EBP:" << setw(8) << inst.ebp
2346 << " ESP:" << setw(8) << inst.esp << " DS:" << setw(4) << inst.s_ds
2347 << " ES:" << setw(4) << inst.s_es<< " FS:" << setw(4) << inst.s_fs
2348 << " GS:" << setw(4) << inst.s_gs<< " SS:" << setw(4) << inst.s_ss
2349 << " CF:" << inst.c << " ZF:" << inst.z << " SF:" << inst.s
2350 << " OF:" << inst.o << " AF:" << inst.a << " PF:" << inst.p
2351 << " IF:" << inst.i << endl;
2352
2353 /* fprintf(f,"%04X:%08X %s %s EAX:%08X EBX:%08X ECX:%08X EDX:%08X ESI:%08X EDI:%08X EBP:%08X ESP:%08X DS:%04X ES:%04X FS:%04X GS:%04X SS:%04X CF:%01X ZF:%01X SF:%01X OF:%01X AF:%01X PF:%01X IF:%01X\n",
2354 logInst[startLog].s_cs,logInst[startLog].eip,logInst[startLog].dline,logInst[startLog].res,logInst[startLog].eax,logInst[startLog].ebx,logInst[startLog].ecx,logInst[startLog].edx,logInst[startLog].esi,logInst[startLog].edi,logInst[startLog].ebp,logInst[startLog].esp,
2355 logInst[startLog].s_ds,logInst[startLog].s_es,logInst[startLog].s_fs,logInst[startLog].s_gs,logInst[startLog].s_ss,
2356 logInst[startLog].c,logInst[startLog].z,logInst[startLog].s,logInst[startLog].o,logInst[startLog].a,logInst[startLog].p,logInst[startLog].i);*/
2357 if (++startLog >= LOGCPUMAX) startLog = 0;
2358 } while (startLog != logCount);
2359
2360 out.close();
2361 DEBUG_ShowMsg("DEBUG: Done.\n");
2362 };
2363
2364 bool DEBUG_HeavyIsBreakpoint(void) {
2365 static Bitu zero_count = 0;
2366 if (cpuLog) {
2367 if (cpuLogCounter>0) {
2368 LogInstruction(SegValue(cs),reg_eip,cpuLogFile);
2369 cpuLogCounter--;
2370 }
2371 if (cpuLogCounter<=0) {
2372 cpuLogFile.close();
2373 DEBUG_ShowMsg("DEBUG: cpu log LOGCPU.TXT created\n");
2374 cpuLog = false;
2375 DEBUG_EnableDebugger();
2376 return true;
2377 }
2378 }
2379 // LogInstruction
2380 if (logHeavy) DEBUG_HeavyLogInstruction();
2381 if (zeroProtect) {
2382 Bit32u value=0;
2383 if (!mem_readd_checked(SegPhys(cs)+reg_eip,&value)) {
2384 if (value == 0) zero_count++;
2385 else zero_count = 0;
2386 }
2387 if (GCC_UNLIKELY(zero_count == 10)) E_Exit("running zeroed code");
2388 }
2389
2390 if (skipFirstInstruction) {
2391 skipFirstInstruction = false;
2392 return false;
2393 }
2394 if (CBreakpoint::CheckBreakpoint(SegValue(cs),reg_eip)) {
2395 return true;
2396 }
2397 return false;
2398 }
2399
2400 #endif // HEAVY DEBUG
2401
2402
2403 #endif // DEBUG
2404
2405