Skip to content

Commit

Permalink
chore(debugger): improve code and add details to error messages (PR s…
Browse files Browse the repository at this point in the history
…kylot#1982)

* chore: created list entry getter getRegListEntry for problematic code;
use specific containers/fields instead of SimpleEntry;
* chore: include details on the used ArtAdapter in error message
* add device info to registers exception

---------

Co-authored-by: Skylot <[email protected]>
  • Loading branch information
jpstotz and skylot authored Aug 30, 2023
1 parent f6ab105 commit f695faf
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

public class ArtAdapter {

public interface Debugger {
public interface IArtAdapter {
int getRuntimeRegNum(int smaliNum, int regCount, int paramStart);

boolean readNullObject();

String typeForNull();
}

public static Debugger getAdapter(int androidReleaseVer) {
public static IArtAdapter getAdapter(int androidReleaseVer) {
if (androidReleaseVer <= 8) {
return new AndroidOreoAndBelow();
} else {
return new AndroidPieAndAbove();
}
}

public static class AndroidOreoAndBelow implements Debugger {
public static class AndroidOreoAndBelow implements IArtAdapter {
@Override
public int getRuntimeRegNum(int smaliNum, int regCount, int paramStart) {
int localRegCount = regCount - paramStart;
Expand All @@ -36,7 +36,7 @@ public String typeForNull() {
}
}

public static class AndroidPieAndAbove implements Debugger {
public static class AndroidPieAndAbove implements IArtAdapter {
@Override
public int getRuntimeRegNum(int smaliNum, int regCount, int paramStart) {
return smaliNum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class DebugController implements SmaliDebugger.SuspendListener, IDe

private JDebuggerPanel debuggerPanel;
private SmaliDebugger debugger;
private ArtAdapter.Debugger art;
private ArtAdapter.IArtAdapter art;
private final CurrentInfo cur = new CurrentInfo();

private BreakpointStore bpStore;
Expand Down Expand Up @@ -409,7 +409,7 @@ private void refreshRegInfo(long codeOffset) {
for (RegisterObserver.Info info : list) {
RegTreeNode reg = cur.frame.getRegNodes().get(info.getSmaliRegNum());
if (info.isLoad()) {
applyDbgInfo(reg, info.getInfo());
applyDbgInfo(reg, info.getName(), info.getType());
} else {
reg.setAlias("");
reg.setAbsoluteType(false);
Expand Down Expand Up @@ -552,7 +552,7 @@ private FrameNode updateAllStackFrames(long threadID) {
} catch (SmaliDebuggerException e) {
logErr(e);
}
if (frames.size() == 0) {
if (frames.isEmpty()) {
return null;
}
List<FrameNode> frameEleList = new ArrayList<>(frames.size());
Expand Down Expand Up @@ -671,7 +671,7 @@ private void updateAllRegisters(FrameNode frame) {
private void fetchAllRegisters(FrameNode frame) {
List<SmaliRegister> regs = cur.regAdapter.getInitializedList(frame.getCodeOffset());
for (SmaliRegister reg : regs) {
Entry<String, String> info = cur.regAdapter.getInfo(reg.getRuntimeRegNum(), frame.getCodeOffset());
RuntimeVarInfo info = cur.regAdapter.getInfo(reg.getRuntimeRegNum(), frame.getCodeOffset());
RegTreeNode regNode = frame.getRegNodes().get(reg.getRegNum());
if (info != null) {
applyDbgInfo(regNode, info);
Expand All @@ -680,9 +680,13 @@ private void fetchAllRegisters(FrameNode frame) {
}
}

private void applyDbgInfo(RegTreeNode rn, Entry<String, String> info) {
rn.setAlias(info.getKey());
rn.updateType(info.getValue());
private void applyDbgInfo(RegTreeNode rn, RuntimeVarInfo info) {
applyDbgInfo(rn, info.getName(), info.getType());
}

private void applyDbgInfo(RegTreeNode rn, String alias, String type) {
rn.setAlias(alias);
rn.updateType(type);
rn.setAbsoluteType(true);
}

Expand Down Expand Up @@ -877,7 +881,9 @@ private void updateAllInfo(long threadID, long codeOffset) {
cur.regAdapter = regAdaMap.computeIfAbsent(cur.mthFullID,
k -> RegisterObserver.merge(
getRuntimeDebugInfo(cur.frame),
getRegisterList()));
getSmaliRegisterList(),
art,
cur.mthFullID));

if (cur.smali.getRegCount(cur.mthFullID) > 0) {
updateAllRegisters(cur.frame);
Expand All @@ -889,7 +895,7 @@ private void updateAllInfo(long threadID, long codeOffset) {
});
}

private List<SmaliRegister> getRegisterList() {
private List<SmaliRegister> getSmaliRegisterList() {
int regCount = cur.smali.getRegCount(cur.mthFullID);
int paramStart = cur.smali.getParamRegStart(cur.mthFullID);
List<SmaliRegister> srs = cur.smali.getRegisterList(cur.mthFullID);
Expand Down
158 changes: 158 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/device/debugger/DebugSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package jadx.gui.device.debugger;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.core.utils.StringUtils;
import jadx.gui.device.protocol.ADB;
import jadx.gui.device.protocol.ADBDevice;
import jadx.gui.utils.NLS;

public class DebugSettings {
private static final Logger LOG = LoggerFactory.getLogger(DebugSettings.class);

private static final int FORWARD_TCP_PORT = 33233;

public static final DebugSettings INSTANCE = new DebugSettings();

private int ver;
private String pid;
private String name;
private ADBDevice device;
private int forwardTcpPort = FORWARD_TCP_PORT;
private String expectPkg = "";
private boolean autoAttachPkg = false;

private DebugSettings() {
}

public void set(ADBDevice device, int ver, String pid, String name) {
this.ver = ver;
this.pid = pid;
this.name = name;
this.device = device;
this.autoAttachPkg = false;
this.expectPkg = "";
}

public DebugSettings setPid(String pid) {
this.pid = pid;
return this;
}

public DebugSettings setName(String name) {
this.name = name;
return this;
}

public String forwardJDWP() {
int localPort = forwardTcpPort;
String resultDesc = "";
try {
do {
ADBDevice.ForwardResult rst = device.forwardJDWP(localPort + "", pid);
if (rst.state == 0) {
forwardTcpPort = localPort;
return "";
}
if (rst.state == 1) {
if (rst.desc.contains("Only one usage of each socket address")) { // port is taken by other process
if (localPort < 65536) {
localPort++; // retry
continue;
}
}
}
resultDesc = rst.desc;
break;
} while (true);
} catch (Exception e) {
LOG.error("JDWP forward error", e);
}
if (StringUtils.isEmpty(resultDesc)) {
resultDesc = NLS.str("adb_dialog.forward_fail");
}
return resultDesc;
}

// we have to remove all ports that forwarding the jdwp:pid, otherwise our JDWP handshake may fail.
public void clearForward() {
String jdwpPid = " jdwp:" + pid;
String tcpPort = " tcp:" + forwardTcpPort;
try {
List<String> list = ADB.listForward(device.getDeviceInfo().getAdbHost(),
device.getDeviceInfo().getAdbPort());
for (String s : list) {
if (s.startsWith(device.getSerial()) && s.endsWith(jdwpPid) && !s.contains(tcpPort)) {
String[] fields = s.split("\\s+");
for (String field : fields) {
if (field.startsWith("tcp:")) {
try {
device.removeForward(field.substring("tcp:".length()));
} catch (Exception e) {
LOG.error("JDWP remove forward error", e);
}
}
}
}
}
} catch (Exception e) {
LOG.error("JDWP clear forward error", e);
}
}

public boolean isBeingDebugged() {
String jdwpPid = " jdwp:" + pid;
String tcpPort = " tcp:" + forwardTcpPort;
try {
List<String> list = ADB.listForward(device.getDeviceInfo().getAdbHost(),
device.getDeviceInfo().getAdbPort());
for (String s : list) {
if (s.startsWith(device.getSerial()) && s.endsWith(jdwpPid)) {
return !s.contains(tcpPort);
}
}
} catch (Exception e) {
LOG.error("ADB list forward error", e);
}
return false;
}

public int getVer() {
return ver;
}

public String getPid() {
return pid;
}

public String getName() {
return name;
}

public ADBDevice getDevice() {
return device;
}

public int getForwardTcpPort() {
return forwardTcpPort;
}

public String getExpectPkg() {
return expectPkg;
}

public void setExpectPkg(String expectPkg) {
this.expectPkg = expectPkg;
}

public boolean isAutoAttachPkg() {
return autoAttachPkg;
}

public void setAutoAttachPkg(boolean autoAttachPkg) {
this.autoAttachPkg = autoAttachPkg;
}
}
Loading

0 comments on commit f695faf

Please sign in to comment.