// cl /Zi /D_NO_WINDOWS /D_NO_RAPI /EHsc /I ..\common /DW32TST atcmdmon.cpp ..\common\debug.cpp ..\common\stringutils.cpp ..\common\vectorutils.cpp /link /subsystem:console // #include #ifndef W32TST #include "cenk.h" #include "kernelmisc.h" #endif #include "vectorutils.h" #include "stringutils.h" #include "debug.h" #include void read_mem(DWORD ofs, DWORD size, ByteVector &buf) { #ifndef W32TST buf.resize(size); memcpy(vectorptr(buf), (void*)ofs, size); #else buf.clear(); static int state= 0; switch(state++) { case 0: BV_AppendString(buf, "aaa0\naa1\naa2\naa3\naa4\n"); break; case 1: BV_AppendString(buf, "bbb0\nbb1\nbb2\nbb3\nbb4\n"); break; case 2: BV_AppendString(buf, "ccc0\naa1\naa2\naa3\nccc\n"); break; case 3: BV_AppendString(buf, "bbb0\nzz1\nyyy\nqq3\nbb4\n"); break; case 4: BV_AppendString(buf, "ccc0\naa1\naa2\naa3\nccc\n"); break; case 5: BV_AppendString(buf, "bbb0\nzz1\nyyy\nqq3\nbb4\n"); break; default:exit(1); } #endif } void dump_region(const ByteVector& mem, DWORD start, DWORD end) { debug("%s", ascdump(ByteVector(mem.begin()+start, mem.begin()+end), "\n\r", true).c_str()); } void write_diffs(const ByteVector& oldmem, const ByteVector& newmem) { //debug("old:(%d)\n%s\nnew:(%d)\n%s\n", oldmem.size(), ascdump(oldmem).c_str(), newmem.size(), ascdump(newmem).c_str()); typedef std::map OfsSizeMap; OfsSizeMap ofssize; DWORD maxofs=0; DWORD maxval=0; DWORD start=0; bool prev_eq= false; size_t i; for (i=0 ; i<=oldmem.size() ; i++) { bool eq= imaxval) { maxval= i-start; maxofs= start; } } start= i; } prev_eq= eq; } if (ofssize.size()<=1) { //debug("no change\n"); return; } DWORD firsteq= (*ofssize.begin()).first; DWORD lasteq= (*ofssize.rbegin()).first; debug("fffbbc00: %s\n", hexdump((BYTE*)0xb80bbc00, 0x40, 4).c_str()); if (lasteq+ofssize[lasteq]==oldmem.size() && (maxofs==lasteq || maxofs==0)) { // equal region wraps - n-eq region is in the middle debugt("----- %08lx-%08lx: %d new\n", ofssize[firsteq], lasteq, lasteq-ofssize[firsteq]); ByteVector diff; diff.insert(diff.end(), newmem.begin()+ofssize[firsteq], newmem.begin()+lasteq); debug("%s\n", ascdump(diff, "\n\r", true).c_str()); } else { debugt("----- %08lx-%08lx, %08lx-%08lx: %d new\n", maxofs+maxval, oldmem.size(), 0, maxofs, oldmem.size()-maxval); ByteVector diff; diff.insert(diff.end(), newmem.begin()+maxofs+maxval, newmem.end()); diff.insert(diff.end(), newmem.begin(), newmem.begin()+maxofs); debug("%s\n", ascdump(diff, "\n\r", true).c_str()); //dump_region(newmem, maxofs+maxval, oldmem.size()); //dump_region(newmem, 0, maxofs); } } #ifndef W32TST int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) #else int main(char**,int) #endif { DebugSetLogfile("circularbufmon.log"); #ifndef W32TST KernelMode _km; #else DebugStdOut(); #endif ByteVector prev_cmd; ByteVector prev_reply; while (1) { ByteVector cmd; ByteVector reply; read_mem(0x8a401000, 0x1000, cmd); read_mem(0x8a400000, 0x1000, reply); if (prev_cmd.size()) { write_diffs(prev_cmd, cmd); write_diffs(prev_reply, reply); } prev_cmd.swap(cmd); prev_reply.swap(reply); Sleep(500); } return 0; }