Skip to content

Commit

Permalink
Logging improvement: improve the human-readable function names for jal/j
Browse files Browse the repository at this point in the history
instructions.
  • Loading branch information
gid15 committed Nov 9, 2018
1 parent 8c691dd commit 5e84c0a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/jpcsp/Allegrex/Common.java
Expand Up @@ -22,7 +22,6 @@
import org.apache.log4j.Logger;

import jpcsp.Emulator;
import jpcsp.Memory;
import jpcsp.Processor;
import jpcsp.Allegrex.compiler.ICompilerContext;
import jpcsp.HLE.HLEModuleFunction;
Expand Down Expand Up @@ -743,23 +742,16 @@ public static String disasmBREAK(int code) {

public static String disasmJUMP(String opname, int uimm26, int opcode_address) {
int jump = (opcode_address & 0xf0000000) | ((uimm26 & 0x3ffffff) << 2);
int jumpToSyscall = jump + 4;

// If we think the target is a stub, try and append the syscall name
if ((opname.equals("jal") || opname.equals("j")) && jump != 0 &&
jumpToSyscall != opcode_address && Memory.isAddressGood(jumpToSyscall)) {
String disasm = String.format("%1$-10s 0x%2$08X", opname, jump);
// Try to find a human-readable name for the address
if (opname.equals("jal") || opname.equals("j")) {
String functionName = Utilities.getFunctionNameByAddress(jump);
if (functionName != null) {
return String.format("%1$-10s 0x%2$08X [%3$s]", opname, jump, functionName);
disasm += String.format(" [%s]", functionName);
}
int nextOpcode = jpcsp.Memory.getInstance().read32(jumpToSyscall);
Instruction nextInsn = Decoder.instruction(nextOpcode);
String secondTarget = nextInsn.disasm(jumpToSyscall, nextOpcode);
if (secondTarget.startsWith("syscall") && !secondTarget.contains("[unknown]")) {
return String.format("%1$-10s 0x%2$08X %3$s", opname, jump, secondTarget.substring(19));
}
}
return String.format("%1$-10s 0x%2$08X", opname, jump);
return disasm;
}

public static String disasmRTIMM(String opname, int rt, int imm) {
Expand Down
9 changes: 9 additions & 0 deletions src/jpcsp/NIDMapper.java
Expand Up @@ -471,6 +471,15 @@ public int getNidByAddress(int address) {
return info.getNid();
}

public String getModuleNameByAddress(int address) {
NIDInfo info = getNIDInfoByAddress(address);
if (info == null) {
return null;
}

return info.getModuleName();
}

public void unloadNid(int nid) {
// Search for the NID in all the modules
for (String moduleName : moduleNidMap.keySet()) {
Expand Down
58 changes: 58 additions & 0 deletions src/jpcsp/util/Utilities.java
Expand Up @@ -53,8 +53,12 @@
import jpcsp.Emulator;
import jpcsp.Memory;
import jpcsp.MemoryMap;
import jpcsp.NIDMapper;
import jpcsp.Allegrex.Common;
import jpcsp.Allegrex.Common.Instruction;
import jpcsp.Allegrex.CpuState;
import jpcsp.Allegrex.Decoder;
import jpcsp.Allegrex.Instructions;
import jpcsp.Allegrex.compiler.RuntimeContext;
import jpcsp.Allegrex.compiler.RuntimeContextLLE;
import jpcsp.HLE.HLEModuleFunction;
Expand All @@ -66,6 +70,7 @@
import jpcsp.HLE.TPointer64;
import jpcsp.HLE.TPointer8;
import jpcsp.HLE.VFS.IVirtualFile;
import jpcsp.HLE.kernel.Managers;
import jpcsp.HLE.kernel.types.SceModule;
import jpcsp.filesystems.SeekableDataInput;
import jpcsp.filesystems.SeekableRandomFile;
Expand Down Expand Up @@ -1898,6 +1903,10 @@ public static HLEModuleFunction getHLEFunctionByAddress(int address) {
}

public static String getFunctionNameByAddress(int address) {
if (address == 0) {
return null;
}

String functionName = null;

HLEModuleFunction func = HLEModuleManager.getInstance().getFunctionFromAddress(address);
Expand All @@ -1909,6 +1918,55 @@ public static String getFunctionNameByAddress(int address) {
functionName = Modules.LoadCoreForKernelModule.getFunctionNameByAddress(address);
}

if (functionName == null) {
NIDMapper nidMapper = NIDMapper.getInstance();
int nid = nidMapper.getNidByAddress(address);
if (nid != 0) {
int syscall = nidMapper.getSyscallByNid(nid);
if (syscall >= 0) {
functionName = nidMapper.getNameBySyscall(syscall);
}

if (functionName == null) {
String moduleName = nidMapper.getModuleNameByAddress(address);
if (moduleName != null && moduleName.length() > 0) {
functionName = String.format("%s_%08X", moduleName, nid);
}
}
}
}

int nextOpcode = Memory.getInstance().read32(address + 4);
Instruction nextInsn = Decoder.instruction(nextOpcode);
if (nextInsn == Instructions.SYSCALL) {
int syscallCode = (nextOpcode >> 6) & 0xFFFFF;
func = HLEModuleManager.getInstance().getFunctionFromSyscallCode(syscallCode);
if (func != null) {
functionName = func.getFunctionName();
}
}

if (functionName == null) {
SceModule module = Managers.modules.getModuleByAddress(address);
if (module != null && module.modname != null && module.modname.length() > 0) {
if (address == module.module_start_func) {
functionName = String.format("%s.module_start", module.modname);
} else if (address == module.module_stop_func) {
functionName = String.format("%s.module_stop", module.modname);
} else if (address == module.module_bootstart_func) {
functionName = String.format("%s.module_bootstart", module.modname);
} else if (address == module.module_reboot_before_func) {
functionName = String.format("%s.module_reboot_before", module.modname);
} else if (address == module.module_reboot_phase_func) {
functionName = String.format("%s.module_reboot_phase", module.modname);
} else if (address == module.module_start_func) {
functionName = String.format("%s.module_start", module.modname);
} else {
functionName = String.format("%s.sub_%08X", module.modname, address - module.text_addr);
}
}
}

return functionName;
}

Expand Down

0 comments on commit 5e84c0a

Please sign in to comment.