void allocBreakPoint(HANDLE hProcess, BREAKPOINT *pt) { unsigned char bf; if (!pt->active && pt->addresses) { int i =0,j; int *p; for (p = pt->addresses; *p !=0; ++p) i++; if (!pt->tempvals) { pt->tempvals = calloc(i, sizeof(int)); } strcpy(pt->name, "unknown"); for (i=0, p = pt->addresses; *p !=0; ++p, ++i) { DWORD len; if (ReadProcessMemory(hProcess, (LPVOID)pt->addresses[i], (LPVOID) &bf, 1, &len)) { if (!len) return ; if (bf == 0xcc) { pt->tempvals[i] = -1; } else { WriteBreakPoint(hProcess, pt->addresses[i], 0xcc); pt->active = TRUE; pt->tempvals[i] = bf; } } if (i == 0) { char name[256]; DWORD n = FindFunctionName(name, pt->addresses[i], NULL, NULL); if (n) sprintf(pt->name, "%s + 0x%x", name, pt->addresses[i]-n); } } } }
static void CopyText(HWND hwnd) { int count; THREAD *list = activeProcess->threads; char *p; while (list) { list = list->next; count++; } p = malloc(256 * count); if (p) { int i; p[0] = 0; list = activeProcess->threads; while (list) { char name[256]; int eip = list->regs.Eip; int n; n = FindFunctionName(name, eip, NULL, NULL); if (!n) name[0] = 0; if (list->idThread == activeThread->idThread) strcat(name, "*"); sprintf(p + strlen(p), "%d\t%s + 0x%x\n", list->idThread, name, eip - n); list = list->next; } TextToClipBoard(hwnd, p); free(p); } }
void SetStackArea(HWND hwnd) { int ebp = activeThread->regs.Ebp; int eip = activeThread->regs.Eip; SCOPE *stackbase = 0, *stackptr, *newStack; if (uState != atBreakpoint && uState != atException) return ; while (1) { eip = eipReal(eip); newStack = calloc(1,sizeof(SCOPE)); if (!newStack) return ; newStack->next = 0; FindFunctionName(newStack->name, eip); GetBreakpointLine(eip, &newStack->fileName, &newStack->lineno, stackbase != NULL); newStack->address = eip; newStack->basePtr = ebp; if (stackbase) stackptr = stackptr->next = newStack; else stackbase = stackptr = newStack; eip = readStackedData(ebp, &ebp); if (!eip) break; } StackList = stackbase; }
/* ================= AddFunctionName ================= */ void AddFunctionName (char *funcname, char *filename, tokenList_t *head) { replacefunc_t *f; tokenList_t *list; if ( FindFunctionName(funcname) ) { return; } #if defined( __linux__ ) || defined( __FreeBSD__ ) // the bad thing is, this doesn't preprocess .. on __linux__ this // function is not implemented (q_math.c) if ( !Q_stricmp( funcname, "BoxOnPlaneSide" ) ) { return; } #endif // NERVE - SMF - workaround for Graeme's predifined MACOSX functions // TTimo - looks like linux version needs to escape those too #if defined( _WIN32 ) || defined( __linux__ ) || defined( __FreeBSD__ ) if ( !Q_stricmp(funcname, "qmax") ) { return; } else if ( !Q_stricmp(funcname, "qmin") ) { return; } #endif // -NERVE - SMF f = (replacefunc_t *) GetMemory( sizeof( replacefunc_t ) + strlen( funcname ) + 1 + 6 + strlen( filename ) + 1 ); f->name = (char *) f + sizeof( replacefunc_t ); strcpy( f->name, funcname ); f->newname = (char *) f + sizeof( replacefunc_t ) + strlen( funcname ) + 1; sprintf( f->newname, "F%d", numfuncs++ ); f->filename = (char *) f + sizeof( replacefunc_t ) + strlen( funcname ) + 1 + strlen( f->newname ) + 1; strcpy( f->filename, filename ); f->next = replacefuncs; replacefuncs = f; // construct the declaration list = head; f->dec[0] = '\0'; ConcatDec( list, f->dec, 0 ); } //end of the function AddFunctionName
/* ================= ScrewUpFile ================= */ void ScrewUpFile (char *oldfile, char *newfile) { FILE *fp; script_t *script; token_t token; replacefunc_t *f; char *ptr; printf( "screwing up file %s\n", oldfile ); script = LoadScriptFile( oldfile ); if ( !script ) { Error( "error opening %s\n", oldfile ); } fp = fopen( newfile, "wb" ); if ( !fp ) { Error( "error opening %s\n", newfile ); } // while ( PS_ReadToken( script, &token ) ) { WriteWhiteSpace( fp, script ); if ( token.type == TT_NAME ) { f = FindFunctionName( token.string ); if ( f ) { ptr = f->newname; } else { ptr = token.string;} while ( *ptr ) { fputc( *ptr, fp ); ptr++; } //end while } //end if else { WriteString( fp, script ); } //end else } //end while WriteWhiteSpace( fp, script ); FreeMemory( script ); fclose( fp ); } //end of the function ScrewUpFile
LRESULT CALLBACK ThreadProc(HWND hwnd, UINT iMessage, WPARAM wParam, LPARAM lParam) { LV_ITEM item; LV_COLUMN lvC; RECT r; int i; char module[256]; THREAD *sl; LPNMHDR nmh; switch (iMessage) { case WM_CTLCOLORSTATIC: { return (LRESULT)(HBRUSH)(COLOR_WINDOW + 1); } case WM_TIMER: KillTimer(hwnd, 100); ListView_SetItemState(hwndLV, curSel, 0, LVIS_SELECTED); break; case WM_NOTIFY: nmh = (LPNMHDR)lParam; if (nmh->code == NM_SETFOCUS) { PostMessage(hwndFrame, WM_REDRAWTOOLBAR, 0, 0); SendMessage(GetParent(hwnd), WM_ACTIVATEME, 0, 0); } else if (nmh->code == LVN_GETDISPINFO) { LV_DISPINFO *p = (LV_DISPINFO *)lParam; THREAD *x = (THREAD *)p->item.lParam; char name[256], name1[256]; if (p->item.iSubItem == 2) { int eip = x->regs.Eip; int n; n = FindFunctionName(name1, eip, NULL, NULL); if (!n) name1[0] = 0; sprintf(name, "%s + 0x%x", name1, eip - n); } else { sprintf(name, "%d", x->idThread); } p->item.mask |= LVIF_TEXT | LVIF_DI_SETITEM; p->item.mask &= ~LVIF_STATE; p->item.pszText = name; } else if (nmh->code == LVN_ITEMCHANGED) { LPNMLISTVIEW p = (LPNMLISTVIEW)lParam; if (p->uChanged & LVIF_STATE) { if (p->uNewState & LVIS_SELECTED) { i = 0; PostMessage(hwnd, WM_USER, p->iItem, 0); SetTimer(hwnd, 100, 400, 0); } } } else if (nmh->code == LVN_KEYDOWN) { switch (((LPNMLVKEYDOWN)lParam)->wVKey) { case 'C': if (GetKeyState(VK_CONTROL) &0x80000000) { CopyText(hwnd); } break; case VK_UP: if (curSel > 0) SendMessage(hwnd, WM_USER, curSel-1, 0); break; case VK_DOWN: if (curSel < ListView_GetItemCount(hwndLV) - 1) SendMessage(hwnd, WM_USER, curSel + 1, 0); break; } } break; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_TBTHREADS: if (HIWORD(wParam) == CBN_SELENDOK) { int i = SendMessage(hwndTbThreads, CB_GETCURSEL, 0, 0); if (i != CB_ERR) { SendMessage(hwnd, WM_USER, i, 0); curSel = i; } } break; } break; case WM_USER: { memset(&item, 0, sizeof(item)); item.iItem = curSel; item.iSubItem = 0; item.mask = LVIF_IMAGE; item.iImage = 8; ListView_SetItem(hwndLV, &item); curSel = wParam; sl = activeProcess->threads; while (sl && wParam--) sl = sl->next; activeThread = sl; item.iItem = curSel; item.mask = LVIF_IMAGE; item.iImage = 4; ListView_SetItem(hwndLV, &item); PostDIDMessage(DID_REGWND, WM_COMMAND, ID_SETADDRESS, (LPARAM) activeThread->hThread); PostDIDMessage(DID_WATCHWND, WM_COMMAND, ID_SETADDRESS, 0); PostDIDMessage(DID_WATCHWND+1, WM_COMMAND, ID_SETADDRESS, 0); PostDIDMessage(DID_WATCHWND+2, WM_COMMAND, ID_SETADDRESS, 0); PostDIDMessage(DID_WATCHWND+3, WM_COMMAND, ID_SETADDRESS, 0); PostDIDMessage(DID_LOCALSWND, WM_COMMAND, ID_SETADDRESS, 0); PostDIDMessage(DID_STACKWND, WM_RESTACK, (WPARAM)1, 0); PostDIDMessage(DID_MEMWND, WM_RESTACK, 0, 0); PostDIDMessage(DID_MEMWND+1, WM_RESTACK, 0, 0); PostDIDMessage(DID_MEMWND+2, WM_RESTACK, 0, 0); PostDIDMessage(DID_MEMWND+3, WM_RESTACK, 0, 0); SendMessage(hwndASM, WM_COMMAND, ID_SETADDRESS, (LPARAM) activeThread->regs.Eip); SendMessage(hwndTbThreads, CB_SETCURSEL, curSel, 0); } break; case WM_CREATE: hwndThread = hwnd; GetClientRect(hwnd, &r); hwndLV = CreateWindowEx(0, WC_LISTVIEW, "", LVS_REPORT | LVS_SINGLESEL | WS_CHILD | WS_VISIBLE | WS_BORDER, 0,0,r.right-r.left, r.bottom - r.top, hwnd, 0, hInstance, 0); ListView_SetExtendedListViewStyle(hwndLV, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER); ApplyDialogFont(hwndLV); lvC.mask = LVCF_WIDTH | LVCF_SUBITEM ; lvC.cx = 20; lvC.iSubItem = 0; ListView_InsertColumn(hwndLV, 0, &lvC); lvC.mask = LVCF_WIDTH | LVCF_SUBITEM | LVCF_TEXT; lvC.cx = 60; lvC.iSubItem = 1; lvC.pszText = "Id"; ListView_InsertColumn(hwndLV, 1, &lvC); lvC.mask = LVCF_WIDTH | LVCF_SUBITEM | LVCF_TEXT; lvC.cx = 120; lvC.iSubItem = 2; lvC.pszText = "Location"; ListView_InsertColumn(hwndLV, 2, &lvC); ListView_SetImageList(hwndLV, tagImageList, LVSIL_SMALL); break; case WM_SIZE: r.left = r.top = 0; r.right = LOWORD(lParam); r.bottom = HIWORD(lParam); MoveWindow(hwndLV, r.left, r.top, r.right - r.left, r.bottom - r.top, 1); break; case WM_DESTROY: hwndThread = 0; break; case WM_RESTACK: EnableWindow(hwndLV, uState != notDebugging && wParam); EnableWindow(hwndTbThreads, uState != notDebugging && wParam); if (uState != notDebugging && wParam) { int i = 0; THREAD *list = activeProcess->threads; ListView_DeleteAllItems(hwndLV); memset(&item, 0, sizeof(item)); SendMessage(hwndTbThreads, CB_RESETCONTENT, 0, 0); while (list) { char buf[260]; item.iItem = i; item.iSubItem = 0; item.mask = LVIF_IMAGE | LVIF_PARAM; if (list->idThread == activeThread->idThread) { item.iImage = 4; curSel = i; } else { item.iImage = 8; } item.lParam = (LPARAM)list; ListView_InsertItem(hwndLV, &item); item.iSubItem = 1; item.mask = LVIF_PARAM | LVIF_TEXT; item.lParam = (LPARAM)list; item.pszText = ""; ListView_InsertItem(hwndLV, &item); item.iSubItem = 2; item.mask = LVIF_PARAM | LVIF_TEXT; item.lParam = (LPARAM)list; item.pszText = ""; ListView_InsertItem(hwndLV, &item); sprintf(buf, "%d %s", list->idThread, list->name); SendMessage(hwndTbThreads, CB_ADDSTRING, 0, (LPARAM)buf); i++, list = list->next; } SendMessage(hwndTbThreads, CB_SETCURSEL, curSel, 0); } break; case WM_SETFOCUS: break; case WM_KILLFOCUS: break; } return DefWindowProc(hwnd, iMessage, wParam, lParam); }
int dbgSetBreakPoint(char *name, int linenum, char *extra) { BREAKPOINT **p = &activeProcess->breakpoints.next; if (uState == notDebugging) return 1; // will be checked when entering debugger if (!name) { // it was from the assembly window int addr = linenum; while (*p) { int *q; for (q = (*p)->addresses; q && *q !=0; ++q) { if (addr == *q) return 1; } p = &(*p)->next; } *p = calloc(1,sizeof(BREAKPOINT)); if (*p) { (*p)->addresses = calloc(2, sizeof(int )); (*p)->addresses[0] = addr; (*p)->extra = extra; GetBreakpointLine(addr, &(*p)->module[0], &(*p)->linenum, FALSE); if (hwndASM) InvalidateRect(hwndASM, 0, 0); // Tag(TAG_BP, (*p)->module, (*p)->linenum, 0, 0, 0, 0); if (uState == Running) { allocBreakPoint(activeProcess->hProcess, *p); } else { char name[256]; DWORD n = FindFunctionName(name, (*p)->addresses[0], NULL,NULL); if (n) sprintf((*p)->name, "%s + 0x%x", name, (*p)->addresses[0]-n); } } SendDIDMessage(DID_BREAKWND, WM_RESTACK, 0, 0); return 1; } else { // it was from a source module int *addresses = GetBreakpointAddresses(name, &linenum); if (addresses) { int *t; for (t = addresses; *t !=0; ++t) { p = &activeProcess->breakpoints.next; while (*p) { int *q; for (q = (*p)->addresses; q && *q !=0; ++q) { if (*t == *q) return 1; } p = &(*p)->next; } } *p = calloc(1,sizeof(BREAKPOINT)); if (*p) { (*p)->addresses = addresses; (*p)->extra = extra; strcpy((*p)->module, name); (*p)->linenum = linenum; if (hwndASM) InvalidateRect(hwndASM, 0, 0); if (uState == Running) { allocBreakPoint(activeProcess->hProcess, *p); } else { char name[256]; DWORD n = FindFunctionName(name, (*p)->addresses[0], NULL, NULL); if (n) sprintf((*p)->name, "%s + 0x%x", name, (*p)->addresses[0]-n); } } else { free(addresses); } return 1; } else return 0; // couldn't locate line, invalid line... } }