Top Description Fields Constructors Methods
org.droidparts.dexmaker.dx.dex.file

public final Class DebugInfoEncoder

extends Object
Class Inheritance
Imports
org.droidparts.dexmaker.dx.dex.code.LocalList, .PositionList, org.droidparts.dexmaker.dx.rop.code.RegisterSpec, .SourcePosition, org.droidparts.dexmaker.dx.rop.cst.CstMethodRef, .CstString, .CstType, org.droidparts.dexmaker.dx.rop.type.Prototype, .StdTypeList, .Type, org.droidparts.dexmaker.dx.util.AnnotatedOutput, .ByteArrayAnnotatedOutput, .ExceptionWithContext, java.io.IOException, .PrintWriter, java.util.ArrayList, .BitSet, .Collections, .Comparator

An encoder for the dex debug info state machine format. The format for each method enrty is as follows:
  1. signed LEB128: initial value for line register.
  2. n instances of signed LEB128: string indicies (offset by 1) for each method argument in left-to-right order with this excluded. A value of '0' indicates "no name"
  3. A sequence of special or normal opcodes as defined in DebugInfoConstants.
  4. A single terminating OP_END_SEQUENCE

Field Summary

Modifier and TypeField and Description
private int
address

current encoding state: bytecode address

private AnnotatedOutput
annotateTo

if non-null: the output to write annotations to.

private final int
private static final boolean
private PrintWriter
debugPrint

if non-null: another possible output for annotations

private final Prototype
private final DexFile
private final boolean
private final LocalList.Entry[]
lastEntryForReg

indexed by register; last local alive in register

private int
line

current encoding state: line number

private final LocalList
locals

null-ok; local variables to encode

private final ByteArrayAnnotatedOutput
private final PositionList
positions

null-ok; positions (line numbers) to encode

private String
prefix

if non-null: the prefix for each annotation or debugPrint line

private final int
private boolean
shouldConsume

true if output should be consumed during annotation

Constructor Summary

AccessConstructor and Description
public
DebugInfoEncoder(PositionList
null-ok; positions (line numbers) to encode
positions
,
LocalList
null-ok; local variables to encode
locals
,
DexFile
null-ok; may only be null if simply using this class to do a debug print
file
,
int codeSize, int regSize, boolean isStatic, CstMethodRef ref)

Creates an instance.

Method Summary

Modifier and TypeMethod and Description
private void
annotate(int
the number of bytes associated with this message
length
,
String
the message itself
message
)

Annotates or writes a message to the debugPrint writer if applicable.

private ArrayList<PositionList.Entry>

Returns:

A sorted positions list
buildSortedPositions
()

Builds a list of position entries, sorted by ascending address.

private static int

Returns:

<= 0xff if in range, otherwise parameters are out of range
computeOpcode
(int
>= DBG_LINE_BASE, <= DBG_LINE_BASE + DBG_LINE_RANGE; the line change to encode
deltaLines
,
int
>= 0; the address change to encode
deltaAddress
)

Computes a special opcode that will encode the given position change.

public byte[]

Returns:

non-null; encoded byte sequence without padding and terminated with a 0x00 byte
convert
()

Converts this (PositionList, LocalList) pair into a state machine sequence.

private byte[]
public byte[]

Returns:

non-null; encoded output
convertAndAnnotate
(String
null-ok; prefix to attach to each line of output
prefix
,
PrintWriter
null-ok; if specified, an alternate output for annotations
debugPrint
,
AnnotatedOutput
null-ok; if specified, where annotations should go
out
,
boolean
whether to claim to have consumed output for out
consume
)

Converts and produces annotations on a stream.

private void
emitAdvanceLine(int
amount to change line number register by
deltaLines
)

Emits an DBG_ADVANCE_LINE sequence.

private void
emitAdvancePc(int
>= 0; amount to change program counter by
deltaAddress
)

Emits an DBG_ADVANCE_PC sequence.

private void
emitEndSequence()

Emits the DBG_END_SEQUENCE bytecode.

private void
emitHeader(ArrayList<PositionList.Entry>
positions, sorted by ascending address
sortedPositions
,
ArrayList<LocalList.Entry>
local list entries for method argumens arguments, in left-to-right order omitting "this"
methodArgs
)

Emits the header sequence, which consists of LEB128-encoded initial line number and string indicies for names of all non-"this" arguments.

private void
emitLocalEnd(LocalList.Entry
entry non-null; entry associated with end.
entry
)

Emits a DBG_END_LOCAL sequence.

private void
emitLocalRestart(LocalList.Entry
entry associated with this restart
entry
)

Emits a DBG_RESTART_LOCAL sequence.

private int

Returns:

new value for curLocalIdx
emitLocalsAtAddress
(int
Current index in locals
curLocalIdx
)

Emits all local variable activity that occurs at the current address starting at the given index into locals and including all subsequent activity at the same address.

private void
emitLocalStart(LocalList.Entry
entry to emit
entry
)

Emits a DBG_START_LOCAL or DBG_START_LOCAL_EXTENDED sequence.

private void
emitLocalStartExtended(LocalList.Entry
entry to emit
entry
)

Emits a DBG_START_LOCAL_EXTENDED sequence.

private void
emitPosition(PositionList.Entry
position entry to emit.
entry
)

Emits the necessary byte sequences to emit the given position table entry.

private int

Returns:

new value for curPositionIdx
emitPositionsAtAddress
(int
Current index in sortedPositions
curPositionIdx
,
ArrayList<PositionList.Entry>
positions, sorted by ascending address
sortedPositions
)

Emits all positions that occur at the current address

private void
emitStringIndex(CstString
null-ok; string to emit
string
)

Emits a string index as an unsigned LEB128.

private void
emitTypeIndex(CstType
null-ok; type to emit
type
)

Emits a type index as an unsigned LEB128.

private void
emitUnsignedLeb128(int
>= 0; value to emit. Note that, although this can represent integers larger than Integer.MAX_VALUE, we currently don't allow that.
n
)

Emits an unsigned LEB128 value.

private String

Returns:

non-null; annotation string
entryAnnotationString
(LocalList.Entry
non-null; entry
e
)

Returns a string representation of this LocalList entry that is appropriate for emitting as an annotation.

private ArrayList<LocalList.Entry>

Returns:

list of non-this method argument locals, sorted by ascending register
extractMethodArguments
()

Extracts method arguments from a locals list.

private int

Returns:

register as noted above
getParamBase
()

Gets the register that begins the method's parameter range (including the 'this' parameter for non-static methods).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addressback to summary
private int address

current encoding state: bytecode address

annotateToback to summary
private AnnotatedOutput annotateTo

if non-null: the output to write annotations to. No normal output is written to this.

codeSizeback to summary
private final int codeSize
DEBUGback to summary
private static final boolean DEBUG
debugPrintback to summary
private PrintWriter debugPrint

if non-null: another possible output for annotations

descback to summary
private final Prototype desc
fileback to summary
private final DexFile file
isStaticback to summary
private final boolean isStatic
lastEntryForRegback to summary
private final LocalList.Entry[] lastEntryForReg

indexed by register; last local alive in register

lineback to summary
private int line

current encoding state: line number

localsback to summary
private final LocalList locals

null-ok; local variables to encode

outputback to summary
private final ByteArrayAnnotatedOutput output
positionsback to summary
private final PositionList positions

null-ok; positions (line numbers) to encode

prefixback to summary
private String prefix

if non-null: the prefix for each annotation or debugPrint line

regSizeback to summary
private final int regSize
shouldConsumeback to summary
private boolean shouldConsume

true if output should be consumed during annotation

Constructor Detail

DebugInfoEncoderback to summary
public DebugInfoEncoder(PositionList positions, LocalList locals, DexFile file, int codeSize, int regSize, boolean isStatic, CstMethodRef ref)

Creates an instance.

Parameters
positions:PositionList

null-ok; positions (line numbers) to encode

locals:LocalList

null-ok; local variables to encode

file:DexFile

null-ok; may only be null if simply using this class to do a debug print

Method Detail

annotateback to summary
private void annotate(int length, String message)

Annotates or writes a message to the debugPrint writer if applicable.

Parameters
length:int

the number of bytes associated with this message

message:String

the message itself

buildSortedPositionsback to summary
private ArrayList<PositionList.Entry> buildSortedPositions()

Builds a list of position entries, sorted by ascending address.

Returns:ArrayList<PositionList.Entry>

A sorted positions list

computeOpcodeback to summary
private static int computeOpcode(int deltaLines, int deltaAddress)

Computes a special opcode that will encode the given position change. If the return value is > 0xff, then the request cannot be fulfilled. Essentially the same as described in "DWARF Debugging Format Version 3" section 6.2.5.1.

Parameters
deltaLines:int

>= DBG_LINE_BASE, <= DBG_LINE_BASE + DBG_LINE_RANGE; the line change to encode

deltaAddress:int

>= 0; the address change to encode

Returns:int

<= 0xff if in range, otherwise parameters are out of range

convertback to summary
public byte[] convert()

Converts this (PositionList, LocalList) pair into a state machine sequence.

Returns:byte[]

non-null; encoded byte sequence without padding and terminated with a 0x00 byte

convert0back to summary
private byte[] convert0() throws IOException
convertAndAnnotateback to summary
public byte[] convertAndAnnotate(String prefix, PrintWriter debugPrint, AnnotatedOutput out, boolean consume)

Converts and produces annotations on a stream. Does not write actual bits to the AnnotatedOutput.

Parameters
prefix:String

null-ok; prefix to attach to each line of output

debugPrint:PrintWriter

null-ok; if specified, an alternate output for annotations

out:AnnotatedOutput

null-ok; if specified, where annotations should go

consume:boolean

whether to claim to have consumed output for out

Returns:byte[]

non-null; encoded output

emitAdvanceLineback to summary
private void emitAdvanceLine(int deltaLines) throws IOException

Emits an DBG_ADVANCE_LINE sequence.

Parameters
deltaLines:int

amount to change line number register by

emitAdvancePcback to summary
private void emitAdvancePc(int deltaAddress) throws IOException

Emits an DBG_ADVANCE_PC sequence.

Parameters
deltaAddress:int

>= 0; amount to change program counter by

emitEndSequenceback to summary
private void emitEndSequence()

Emits the DBG_END_SEQUENCE bytecode.

emitHeaderback to summary
private void emitHeader(ArrayList<PositionList.Entry> sortedPositions, ArrayList<LocalList.Entry> methodArgs) throws IOException

Emits the header sequence, which consists of LEB128-encoded initial line number and string indicies for names of all non-"this" arguments.

Parameters
sortedPositions:ArrayList<PositionList.Entry>

positions, sorted by ascending address

methodArgs:ArrayList<LocalList.Entry>

local list entries for method argumens arguments, in left-to-right order omitting "this"

emitLocalEndback to summary
private void emitLocalEnd(LocalList.Entry entry) throws IOException

Emits a DBG_END_LOCAL sequence.

Parameters
entry:LocalList.Entry

entry non-null; entry associated with end.

emitLocalRestartback to summary
private void emitLocalRestart(LocalList.Entry entry) throws IOException

Emits a DBG_RESTART_LOCAL sequence.

Parameters
entry:LocalList.Entry

entry associated with this restart

emitLocalsAtAddressback to summary
private int emitLocalsAtAddress(int curLocalIdx) throws IOException

Emits all local variable activity that occurs at the current address starting at the given index into locals and including all subsequent activity at the same address.

Parameters
curLocalIdx:int

Current index in locals

Returns:int

new value for curLocalIdx

emitLocalStartback to summary
private void emitLocalStart(LocalList.Entry entry) throws IOException

Emits a DBG_START_LOCAL or DBG_START_LOCAL_EXTENDED sequence.

Parameters
entry:LocalList.Entry

entry to emit

emitLocalStartExtendedback to summary
private void emitLocalStartExtended(LocalList.Entry entry) throws IOException

Emits a DBG_START_LOCAL_EXTENDED sequence.

Parameters
entry:LocalList.Entry

entry to emit

emitPositionback to summary
private void emitPosition(PositionList.Entry entry) throws IOException

Emits the necessary byte sequences to emit the given position table entry. This will typically be a single special opcode, although it may also require DBG_ADVANCE_PC or DBG_ADVANCE_LINE.

Parameters
entry:PositionList.Entry

position entry to emit.

emitPositionsAtAddressback to summary
private int emitPositionsAtAddress(int curPositionIdx, ArrayList<PositionList.Entry> sortedPositions) throws IOException

Emits all positions that occur at the current address

Parameters
curPositionIdx:int

Current index in sortedPositions

sortedPositions:ArrayList<PositionList.Entry>

positions, sorted by ascending address

Returns:int

new value for curPositionIdx

emitStringIndexback to summary
private void emitStringIndex(CstString string) throws IOException

Emits a string index as an unsigned LEB128. The actual value written is shifted by 1, so that the '0' value is reserved for "null". The null symbol is used in some cases by the parameter name list at the beginning of the sequence.

Parameters
string:CstString

null-ok; string to emit

emitTypeIndexback to summary
private void emitTypeIndex(CstType type) throws IOException

Emits a type index as an unsigned LEB128. The actual value written is shifted by 1, so that the '0' value is reserved for "null".

Parameters
type:CstType

null-ok; type to emit

emitUnsignedLeb128back to summary
private void emitUnsignedLeb128(int n) throws IOException

Emits an unsigned LEB128 value.

Parameters
n:int

>= 0; value to emit. Note that, although this can represent integers larger than Integer.MAX_VALUE, we currently don't allow that.

entryAnnotationStringback to summary
private String entryAnnotationString(LocalList.Entry e)

Returns a string representation of this LocalList entry that is appropriate for emitting as an annotation.

Parameters
e:LocalList.Entry

non-null; entry

Returns:String

non-null; annotation string

extractMethodArgumentsback to summary
private ArrayList<LocalList.Entry> extractMethodArguments()

Extracts method arguments from a locals list. These will be collected from the input list and sorted by ascending register in the returned list.

Returns:ArrayList<LocalList.Entry>

list of non-this method argument locals, sorted by ascending register

getParamBaseback to summary
private int getParamBase()

Gets the register that begins the method's parameter range (including the 'this' parameter for non-static methods). The range continues until regSize

Returns:int

register as noted above