From d9c9dad4b853b83c6544a22a715c0805f40163bc Mon Sep 17 00:00:00 2001 From: Cheat Engine Date: Tue, 9 May 2023 00:27:01 +0200 Subject: [PATCH] some small fix for linux --- Cheat Engine/ceserver/api.c | 57 +++++++++++++++++++++++++++----- Cheat Engine/ceserver/ceserver.c | 3 ++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Cheat Engine/ceserver/api.c b/Cheat Engine/ceserver/api.c index 057671994d..3e54149c67 100755 --- a/Cheat Engine/ceserver/api.c +++ b/Cheat Engine/ceserver/api.c @@ -20,6 +20,7 @@ #define _LARGEFILE64_SOURCE #endif + #include #include @@ -3299,14 +3300,31 @@ int ReadProcessMemory(HANDLE hProcess, void *lpAddress, void *buffer, int size) remote.iov_base=lpAddress; remote.iov_len=size; + int canreadnow=1; + pid_t pid; + + if (ATTACH_TO_ACCESS_MEMORY) + { + canreadnow=0; + pid=ptrace_attach_andwait(p->pid); + if (pid>0) + canreadnow=1; + + } - bread=process_vm_readv(p->pid,&local,1,&remote,1,0); - if (bread==-1) + if (canreadnow) { - // debug_log("process_vm_readv(%x, %d) failed: %s\n", lpAddress, size, strerror(errno)); - bread=0; + bread=process_vm_readv(p->pid,&local,1,&remote,1,0); + if (bread==-1) + { + // debug_log("process_vm_readv(%x, %d) failed: %s\n", lpAddress, size, strerror(errno)); + bread=0; + } } + if (ATTACH_TO_ACCESS_MEMORY) + safe_ptrace(PTRACE_DETACH, pid,0,0); + return bread; } else @@ -3418,7 +3436,7 @@ int ReadProcessMemory(HANDLE hProcess, void *lpAddress, void *buffer, int size) if (ATTACH_TO_ACCESS_MEMORY) { int r=safe_ptrace(PTRACE_DETACH, pid,0,0); - debug_log("PTRACE_DETACH returned %d\n", r); + //debug_log("PTRACE_DETACH returned %d\n", r); } @@ -4101,7 +4119,10 @@ HANDLE OpenProcess(DWORD pid) return result; } else + { + debug_log("Failure opening the process"); return 0; //could not find the process + } } @@ -4183,7 +4204,7 @@ BOOL Module32Next(HANDLE hSnapshot, PModuleListEntry moduleentry) } else { - //debug_log("Module32First/Next: GetHandleType(hSnapshot)=%d\n",GetHandleType(hSnapshot)); + debug_log("Module32First/Next failed: Handle is not a htHTSModule handle: %d\n",GetHandleType(hSnapshot)); return FALSE; } } @@ -4337,7 +4358,7 @@ HANDLE CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID) PModuleList ml=(PModuleList)malloc(sizeof(ModuleList)); if (dwFlags & TH32CS_SNAPFIRSTMODULE) - debug_log("Creating module list for process %d\n", th32ProcessID); + debug_log("Creating 1-entry module list for process %d\n", th32ProcessID); ml->ReferenceCount=1; @@ -4380,6 +4401,10 @@ HANDLE CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID) if (strcmp(modulepath, "[heap]")==0) //not static enough to mark as a 'module' continue; + if ((modulepath[0]=='/') && (modulepath[1]=='d') && (modulepath[2]=='e') && (modulepath[3]=='v') && (modulepath[4]=='/')) + continue; //no /dev/ + + // debug_log("Checking if %s is a module\n", modulepath); if (strcmp(modulepath, "[vdso]")!=0) //temporary patch as to not rename vdso, because it is treated differently by the ce symbol loader @@ -4397,13 +4422,20 @@ HANDLE CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID) //check if it's readable + + if (start==0x7f9f9144e000) + { + debug_log("break"); + } + i=ReadProcessMemory(phandle, (void *)start, elfident, 8); //only the first few bytes if (i==0) { - //printf("%s is unreadable(%llx)\n", modulepath, start); - // debug_log("unreadable so no"); + //debug_log("thread %d (%s): Failed to read the start of %s (address %llx)\n", getpid(), threadname, modulepath, start); continue; //unreadable } + //else + // debug_log("thread %d (%s): Successfully read the start of %s (address %llx)\n", getpid(), threadname, modulepath, start); //check if this module is in the list. If so, mark it with a part tag int part=0; @@ -4663,6 +4695,13 @@ void initAPI() process_vm_writev=dlsym(libc,"process_vm_writev"); } + + if (!process_vm_readv) + { + process_vm_readv=dlsym(0, "process_vm_readv"); + process_vm_writev=dlsym(0, "process_vm_writev"); + } + debug_log("process_vm_readv=%p\n",process_vm_readv); debug_log("process_vm_writev=%p\n",process_vm_writev); diff --git a/Cheat Engine/ceserver/ceserver.c b/Cheat Engine/ceserver/ceserver.c index 3d1575d072..bdabd59dc1 100755 --- a/Cheat Engine/ceserver/ceserver.c +++ b/Cheat Engine/ceserver/ceserver.c @@ -2039,6 +2039,7 @@ int main(int argc, char *argv[]) initCESERVERPATH(); debug_log("CESERVERPATH=%s\n", CESERVERPATH); + fflush(stdout); debug_log("MEMORY_SEARCH_OPTION=%d\n", MEMORY_SEARCH_OPTION); fflush(stdout); @@ -2054,6 +2055,8 @@ int main(int argc, char *argv[]) MEMORY_SEARCH_OPTION=0; //fallback to 0 } + + debug_log("MEMORY_SEARCH_OPTION=%d\n", MEMORY_SEARCH_OPTION);