Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3990 - PDB perf: create random access symbol methods and iterator - caching disabled for now

ghizard3da0bf31ff9c
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java
index 5f2d6a29ae..27e5f73b17 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java
@@ -19,6 +19,7 @@ import java.io.IOException;
import java.io.Writer;
import java.util.*;
+import ghidra.app.util.bin.format.pdb2.pdbreader.msf.MsfStream;
import ghidra.util.exception.CancelledException;
/**
@@ -134,9 +135,9 @@ public class DebugData {
* Deserialize {@link DebugData} header from the {@link PdbByteReader} input. This parses
* stream numbers for varying Debug Types--the order/location of the stream number is for
* each particular debug type (e.g., the first stream number read is for the stream containing
- * Frame Pointer Omission debug data). A stream number of 0XFFFF says that there is no data
- * for that debug type; else the stream number represents the stream that should
- * be deserialized to retrieve the debug data of that type. The
+ * Frame Pointer Omission debug data). A stream number of 0XFFFF (MsfStream.NIL_STREAM_NUMBER)
+ * says that there is no data for that debug type; else the stream number represents the
+ * stream that should be deserialized to retrieve the debug data of that type. The
* {@link #deserialize()} method deserializes each of these streams
* that are valid to the corresponding debug data type
* @param reader {@link PdbByteReader} from which to parse the header
@@ -172,7 +173,7 @@ public class DebugData {
}
for (DebugType dbg : DebugType.values()) {
int streamNum = debugStreams.get(dbg.getValue());
- if (streamNum == 0XFFFF) {
+ if (streamNum == MsfStream.NIL_STREAM_NUMBER) {
continue;
}
switch (dbg) {
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/GlobalReferenceIterator.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/GlobalReferenceIterator.java
index 3dac26f484..6907d6685b 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/GlobalReferenceIterator.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/GlobalReferenceIterator.java
@@ -54,7 +54,7 @@ class GlobalReferenceIterator implements ParsingIterator<MsSymbolIterator> {
"Cannot create " + getClass() + " because PDB Debug Info is null");
}
symbolsStreamNumber = debugInfo.getSymbolRecordsStreamNumber();
- if (symbolsStreamNumber == 0xffff) {
+ if (symbolsStreamNumber == MsfStream.NIL_STREAM_NUMBER) {
throw new PdbException(
"Cannot create " + getClass() + " because there is no symbol stream");
}
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/Module.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/Module.java
index 5ef970e504..d09f41b2a6 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/Module.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/Module.java
@@ -73,7 +73,7 @@ public class Module {
private void precalculateStreamLocations() {
streamNumber = moduleInformation.getStreamNumberDebugInformation();
- if (streamNumber == 0xffff) {
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
return;
}
stream = pdb.getMsf().getStream(streamNumber);
@@ -272,7 +272,7 @@ public class Module {
private PdbByteReader getReader(int offset, int size, String sectionName)
throws CancelledException {
- if (streamNumber == 0xffff) {
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
return PdbByteReader.DUMMY;
}
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/PdbDebugInfo.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/PdbDebugInfo.java
index 3c0f95e3fe..24b8c004f3 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/PdbDebugInfo.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/PdbDebugInfo.java
@@ -19,6 +19,7 @@ import java.io.IOException;
import java.io.Writer;
import java.util.*;
+import ghidra.app.util.bin.format.pdb2.pdbreader.msf.MsfStream;
import ghidra.app.util.bin.format.pdb2.pdbreader.symbol.AbstractMsSymbol;
import ghidra.util.exception.CancelledException;
@@ -266,6 +267,14 @@ public abstract class PdbDebugInfo {
return publicSymbolInformation;
}
+ /**
+ * Returns the stream number for {@link SymbolRecords} component
+ * @return stream number
+ */
+ public int getSymbolRecordsStreamNumber() {
+ return streamNumberSymbolRecords;
+ }
+
//==============================================================================================
// Package-Protected Internals
//==============================================================================================
@@ -285,14 +294,6 @@ public abstract class PdbDebugInfo {
return streamNumberPublicStaticSymbolsHashMaybe;
}
- /**
- * Returns the stream number for {@link SymbolRecords} component
- * @return stream number
- */
- int getSymbolRecordsStreamNumber() {
- return streamNumberSymbolRecords;
- }
-
//==============================================================================================
// Abstract Methods
//==============================================================================================
@@ -642,7 +643,7 @@ public abstract class PdbDebugInfo {
*/
public MsSymbolIterator getSymbolIterator()
throws CancelledException, IOException {
- if (streamNumberSymbolRecords == 0xffff) {
+ if (streamNumberSymbolRecords == MsfStream.NIL_STREAM_NUMBER) {
return null;
}
PdbByteReader reader = pdb.getReaderForStreamNumber(streamNumberSymbolRecords);
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/SymbolRecords.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/SymbolRecords.java
index f6e2dfbf7b..975f1c51c7 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/SymbolRecords.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/SymbolRecords.java
@@ -19,7 +19,9 @@ import java.io.IOException;
import java.io.Writer;
import java.util.*;
+import ghidra.app.util.bin.format.pdb2.pdbreader.msf.MsfStream;
import ghidra.app.util.bin.format.pdb2.pdbreader.symbol.AbstractMsSymbol;
+import ghidra.util.datastruct.LRUMap;
import ghidra.util.exception.CancelledException;
/**
@@ -35,6 +37,15 @@ public class SymbolRecords {
private Map<Long, AbstractMsSymbol> symbolsByOffset;
private List<Map<Long, AbstractMsSymbol>> moduleSymbolsByOffset = new ArrayList<>();
+ // Used for CvSig part of streams. See methods below.
+ private boolean getSig = true;
+ private int cvSignature = -1;
+ private int cvSignatureCase1and2Stream = MsfStream.NIL_STREAM_NUMBER;
+
+ // Used for caching symbol records
+ private double factor;
+ private Map<Integer, LRUMap<Integer, SymLen>> symbolCache;
+
/**
* Constructor
* @param pdb {@link AbstractPdb} to which the {@link SymbolRecords} belong
@@ -71,6 +82,8 @@ public class SymbolRecords {
* @throws CancelledException upon user cancellation
*/
void deserialize() throws IOException, PdbException, CancelledException {
+ initializeCache(0.001);
+ determineCvSigValues(); // new method for random-access symbol work
processSymbols();
processModuleSymbols();
}
@@ -115,7 +128,7 @@ public class SymbolRecords {
for (ModuleInformation module : debugInfo.moduleInformationList) {
pdb.checkCancelled();
int streamNumber = module.getStreamNumberDebugInformation();
- if (streamNumber == 0xffff) {
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
moduleSymbolsByOffset.add(new TreeMap<>());
continue;
}
@@ -155,6 +168,96 @@ public class SymbolRecords {
}
}
+ // These methods are trying to adapt the logic of the previous method here (which attempted
+ // to capture the logic of the original MSFT design) to the new random access
+ // model. However, the logic was never verified with other/older PDBs to know if the
+ // signature was only seen on the first module stream. So, we really do not know if we
+ // should continue with this logic or not.
+ //
+ // Following is comment from the method that has been replaced, which might still be applicable:
+ //
+ // Could split this method up into separate methods: one for module symbols and the other for
+ // Lines processing. Note: would be processing streams more than once; lines would need to
+ // skip over the symbols.
+ private void determineCvSigValues() throws CancelledException, IOException, PdbException {
+ PdbDebugInfo debugInfo = pdb.getDebugInfo();
+ if (debugInfo == null) {
+ return;
+ }
+ // We are assuming that first in the list is the one to look at for cases 1 and 2.
+ // If something else like lowest stream number, then need to change the logic.
+ ModuleInformation moduleInfo = debugInfo.getModuleInformationList().get(0);
+ int streamNumber = moduleInfo.getStreamNumberDebugInformation();
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
+ moduleSymbolsByOffset.add(new TreeMap<>());
+ return;
+ }
+ PdbByteReader reader = pdb.getReaderForStreamNumber(streamNumber);
+ // Skipping pulling out of sub-reader for symbols, as symbols stuff is first.
+ if (getSig) {
+ cvSignature = reader.parseInt();
+ }
+ switch (cvSignature) {
+ case 1:
+ case 2:
+ // We have no 1,2 examples to test this logic for cvSignature. Confirming
+ // or rejecting this logic is important for simplifying/refactoring this
+ // method or writing new methods to allow for extraction of information from
+ // individual modules. The current implementation has cross-module logic
+ // (setting state in the processing of the first and using this state in the
+ // processing of follow-on modules).
+ getSig = false;
+ break;
+ case 4:
+ break;
+ default:
+ if (cvSignature < 0x10000) {
+ throw new PdbException(
+ "Invalid module CV signature in stream " + streamNumber);
+ }
+ break;
+ }
+ cvSignatureCase1and2Stream = streamNumber;
+ }
+
+ /**
+ * Returns the space occupied by the cvSignature for the stream number
+ * @param streamNumber the stream number
+ * @return the space
+ * @throws CancelledException upon user cancellation
+ * @throws IOException upon error reading from file
+ * @throws PdbException upon processing error
+ */
+ public int getCvSigLength(int streamNumber)
+ throws CancelledException, IOException, PdbException {
+ if (cvSignatureCase1and2Stream == MsfStream.NIL_STREAM_NUMBER) {
+ throw new PdbException("CvSignLength not initialized");
+ }
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
+ throw new PdbException("Attempting to read unassigned stream");
+ }
+ PdbByteReader reader = pdb.getReaderForStreamNumber(streamNumber, 0, 4);
+ if (getSig) {
+ cvSignature = reader.parseInt();
+ }
+ int size = 0;
+ switch (cvSignature) {
+ case 1:
+ case 2:
+ if (streamNumber == cvSignatureCase1and2Stream) {
+ size = 4;
+ }
+ // else size remains 0
+ break;
+ case 4:
+ size = 4;
+ break;
+ default:
+ throw new PdbException("PDB Error: Bad CvSsigLength state");
+ }
+ return size;
+ }
+
/**
* Deserializes the {@link AbstractMsSymbol} symbols from the {@link PdbByteReader} and
* returns a {@link Map}&lt;{@link Long},{@link AbstractMsSymbol}&gt; of buffer offsets to
@@ -181,6 +284,101 @@ public class SymbolRecords {
return mySymbolsByOffset;
}
+ public record SymLen(AbstractMsSymbol symbol, int length) {}
+
+ /**
+ * Returns the symbol at the offset of the stream assigned to the module
+ * @param moduleNumber the module
+ * @param offset the stream offset
+ * @return the PDB symbol
+ * @throws PdbException upon moduleNumber out of range or no module information
+ * @throws CancelledException upon user cancellation
+ */
+ public SymLen getRandomAccessRecordUsingModuleNumber(int moduleNumber, int offset)
+ throws PdbException, CancelledException {
+ ModuleInformation moduleInfo = pdb.getDebugInfo().getModuleInformation(moduleNumber);
+ int streamNumber = moduleInfo.getStreamNumberDebugInformation();
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
+ return null;
+ }
+ return getRandomAccessRecord(streamNumber, offset);
+ }
+
+ // TODO: consider pre-storing the lengths with one build stream read. However, that would
+ // consume more memory, so only do this if willing to improve process performance at
+ // cost of memroy.
+ //Map<Long, Integer> recordLengths = new TreeMap<>();
+
+ /**
+ * Returns the symbol at the offset of the stream
+ * @param streamNumber the stream
+ * @param offset the stream offset
+ * @return the PDB symbol
+ * @throws PdbException upon moduleNumber out of range or no module information
+ * @throws CancelledException upon user cancellation
+ */
+ public SymLen getRandomAccessRecord(int streamNumber, int offset)
+ throws CancelledException, PdbException {
+ // TODO: Further investigate caching for larger PDBs with other coming changes
+// LRUMap<Integer, SymLen> streamSymbolCache = getSymbolCache(streamNumber);
+// SymLen symLen = streamSymbolCache.get(offset);
+// if (symLen == null) {
+// symLen = getRandomAccessRecordFromStream(streamNumber, offset);
+// streamSymbolCache.put(offset, symLen);
+// }
+// return symLen;
+ return getRandomAccessRecordFromStream(streamNumber, offset);
+ }
+
+ /**
+ * Returns the symbol at the offset of the stream
+ * @param streamNumber the stream
+ * @param offset the stream offset
+ * @return the PDB symbol
+ * @throws PdbException upon moduleNumber out of range or no module information
+ * @throws CancelledException upon user cancellation
+ */
+ private SymLen getRandomAccessRecordFromStream(int streamNumber, int offset)
+ throws CancelledException, PdbException {
+ try {
+ PdbByteReader reader;
+ reader = pdb.getReaderForStreamNumber(streamNumber, offset, 2);
+ int recordLength = reader.parseUnsignedShortVal();
+ // offset + 2 where 2 is sizeof(short)
+ PdbByteReader recordReader =
+ pdb.getReaderForStreamNumber(streamNumber, offset + 2, recordLength);
+ AbstractMsSymbol symbol = SymbolParser.parse(pdb, recordReader);
+ return new SymLen(symbol, recordLength + 2);
+ }
+ catch (IOException e) {
+ return null;
+ }
+ }
+
+ private void initializeCache(double factorArg) {
+ // See notes about factor where the value is used.
+ this.factor = factorArg;
+ symbolCache = new HashMap<>();
+ }
+
+ private LRUMap<Integer, SymLen> getSymbolCache(int streamNumber) {
+ LRUMap<Integer, SymLen> streamSymbolCache = symbolCache.get(streamNumber);
+ if (streamSymbolCache == null) {
+ MsfStream stream = pdb.getMsf().getStream(streamNumber);
+ if (stream == null) {
+ return null;
+ }
+ // Note that stream length is in bytes; factor needs to deal with both ratio of
+ // bytes to average symbol record size and the sizing of the cache as a percentage
+ // of total number of symbol records in the stream. We are adding a fixed number
+ // for a minimum size.
+ int size = (int) (factor * stream.getLength() + 256);
+ streamSymbolCache = new LRUMap<>(size);
+ symbolCache.put(streamNumber, streamSymbolCache);
+ }
+ return streamSymbolCache;
+ }
+
/**
* Debug method for dumping information from this Symbol Records instance
* @param writer {@link Writer} to which to dump the information
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/TypeProgramInterface.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/TypeProgramInterface.java
index e773b97127..edfc62e840 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/TypeProgramInterface.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/TypeProgramInterface.java
@@ -19,6 +19,7 @@ import java.io.IOException;
import java.io.Writer;
import java.util.*;
+import ghidra.app.util.bin.format.pdb2.pdbreader.msf.MsfStream;
import ghidra.app.util.bin.format.pdb2.pdbreader.type.*;
import ghidra.util.exception.AssertException;
import ghidra.util.exception.CancelledException;
@@ -417,7 +418,7 @@ public abstract class TypeProgramInterface implements TPI {
protected void initHeader200500(int hashStreamNumberParam, int typeIndexMinParam,
int typeIndexMaxExclusiveParam) throws PdbException {
hashStreamNumber = hashStreamNumberParam;
- hashStreamNumberAuxiliary = 0xffff;
+ hashStreamNumberAuxiliary = MsfStream.NIL_STREAM_NUMBER;
hashKeySize = 2;
numHashBins = 0x1000;
offsetHashVals = 0;
@@ -449,7 +450,7 @@ public abstract class TypeProgramInterface implements TPI {
// contents might need to get concatenated with the contents of the primary
// stream before the processing takes place, but the API does not show it being
// used at all.
- if (hashStreamNumber == 0xffff) {
+ if (hashStreamNumber == MsfStream.NIL_STREAM_NUMBER) {
return;
}
PdbByteReader reader = pdb.getReaderForStreamNumber(hashStreamNumber);
@@ -468,7 +469,7 @@ public abstract class TypeProgramInterface implements TPI {
deserializeTypeIndexOffsetPairs(typeInfoOffsetPairsReader, monitor);
deserializeHashVals(hashValsReader, monitor);
- if (hashStreamNumberAuxiliary == 0xffff) {
+ if (hashStreamNumberAuxiliary == MsfStream.NIL_STREAM_NUMBER) {
return;
}
PdbByteReader readerAuxiliary = pdb.getReaderForStreamNumber(hashStreamNumberAuxiliary);
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/msf/MsfStream.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/msf/MsfStream.java
index 450b25dd76..b4e67b2455 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/msf/MsfStream.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/msf/MsfStream.java
@@ -41,6 +41,7 @@ public class MsfStream {
* The maximum length of a stream that we currently allow under this design.
*/
public static final int MAX_STREAM_LENGTH = Integer.MAX_VALUE;
+ public static final int NIL_STREAM_NUMBER = 0xffff;
//==============================================================================================
// Internals
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/DefaultPdbApplicator.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/DefaultPdbApplicator.java
index dc1512bf51..9a23408f88 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/DefaultPdbApplicator.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/DefaultPdbApplicator.java
@@ -479,11 +479,13 @@ public class DefaultPdbApplicator implements PdbApplicator {
}
int num = debugInfo.getNumModules();
- // moduleNumber zero is our global/public group.
+ // moduleNumber zero (SymbolGroup.PUBLIC_GLOBAL_MODULE_NUMBER) is our global/public group.
for (int moduleNumber = 0; moduleNumber <= num; moduleNumber++) {
checkCancelled();
- Map<Long, AbstractMsSymbol> symbols = debugInfo.getModuleSymbolsByOffset(moduleNumber);
- SymbolGroup symbolGroup = new SymbolGroup(symbols, moduleNumber);
+ // Keeping next two lines until all other calls to them are removed
+// Map<Long, AbstractMsSymbol> symbols = debugInfo.getModuleSymbolsByOffset(moduleNumber);
+// SymbolGroup symbolGroup = new SymbolGroup(symbols, moduleNumber);
+ SymbolGroup symbolGroup = new SymbolGroup(pdb, moduleNumber);
mySymbolGroups.add(symbolGroup);
}
return mySymbolGroups;
@@ -1172,9 +1174,11 @@ public class DefaultPdbApplicator implements PdbApplicator {
if (symbolGroup == null) {
continue; // should not happen
}
- totalCount += symbolGroup.size();
+ totalCount++;
+ //totalCount += symbolGroup.size();
}
- monitor.setMessage("PDB: Applying " + totalCount + " module symbol components...");
+ monitor.setMessage("PDB: Applying module symbol components...");
+ //monitor.setMessage("PDB: Applying " + totalCount + " module symbol components...");
monitor.initialize(totalCount);
// Process symbols list for each module
@@ -1187,6 +1191,7 @@ public class DefaultPdbApplicator implements PdbApplicator {
}
AbstractMsSymbolIterator iter = symbolGroup.iterator();
processSymbolGroup(moduleNumber, iter);
+ monitor.increment();
// catelogSymbols(index, symbolGroup);
// do not call monitor.incrementProgress(1) here, as it is updated inside of
// processSymbolGroup.
@@ -1525,7 +1530,8 @@ public class DefaultPdbApplicator implements PdbApplicator {
if (symbolGroup == null) {
continue; // should not happen
}
- totalCount += symbolGroup.size();
+ //totalCount += symbolGroup.size();
+ totalCount++;
}
monitor.setMessage("PDB: Processing module thunks...");
monitor.initialize(totalCount);
@@ -1550,8 +1556,9 @@ public class DefaultPdbApplicator implements PdbApplicator {
else {
iter.next();
}
- monitor.incrementProgress(1);
+ //monitor.incrementProgress(1);
}
+ monitor.incrementProgress(1);
}
}
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/SymbolGroup.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/SymbolGroup.java
index cd8b370a6c..311e63ba46 100644
--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/SymbolGroup.java
+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/SymbolGroup.java
@@ -19,7 +19,11 @@ import java.io.IOException;
import java.io.Writer;
import java.util.*;
+import ghidra.app.util.bin.format.pdb2.pdbreader.*;
+import ghidra.app.util.bin.format.pdb2.pdbreader.msf.MsfStream;
import ghidra.app.util.bin.format.pdb2.pdbreader.symbol.AbstractMsSymbol;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
/**
* This class represents a particular group of Symbols that came from the same PDB stream. This
@@ -28,11 +32,25 @@ import ghidra.app.util.bin.format.pdb2.pdbreader.symbol.AbstractMsSymbol;
*/
public class SymbolGroup {
+ public static final int PUBLIC_GLOBAL_MODULE_NUMBER = 0;
+
+ private AbstractPdb pdb;
+
private Map<Long, AbstractMsSymbol> symbolsByOffset;
private int moduleNumber;
private List<Long> offsets;
private Map<Long, Integer> indexByOffset;
+ /**
+ * Constructor
+ * @param pdb the containing the symbols
+ * @param moduleNumber The Module number (0 (PUBLIC_GLOBAL_MODULE_NUMBER) for public/global)
+ */
+ public SymbolGroup(AbstractPdb pdb, int moduleNumber) {
+ this.pdb = pdb;
+ this.moduleNumber = moduleNumber;
+ }
+
/**
* Constructor. The starting offset is set to zero.
* @param symbolsByOffset the Map used to initialize the constructor.
@@ -47,7 +65,7 @@ public class SymbolGroup {
* Constructor.
* @param symbolsByOffset the Map used to initialize the constructor.
* @param moduleNumber The Module number corresponding to the initializing Map
- * (0 for public/global Map).
+ * (0 (PUBLIC_GLOBAL_MODULE_NUMBER) for public/global).
* @param offset the offset location to start.
*/
public SymbolGroup(Map<Long, AbstractMsSymbol> symbolsByOffset, int moduleNumber, long offset) {
@@ -150,20 +168,46 @@ public class SymbolGroup {
*/
class AbstractMsSymbolIterator implements Iterator<AbstractMsSymbol> {
- private int nextIndex;
- private long currentOffset;
+ private SymbolRecords symbolRecords;
+ private int streamNumber;
+ private int lengthSymbols;
+ private int nextRetrieveOffset;
+ private SymbolRecords.SymLen symLen;
public AbstractMsSymbolIterator() {
- nextIndex = 0;
- currentOffset = -1L;
+ symbolRecords = pdb.getDebugInfo().getSymbolRecords();
+ try {
+ if (moduleNumber == 0) {
+ streamNumber = pdb.getDebugInfo().getSymbolRecordsStreamNumber();
+ lengthSymbols = Integer.MAX_VALUE;
+ }
+ else {
+ ModuleInformation moduleInfo =
+ pdb.getDebugInfo().getModuleInformation(moduleNumber);
+ streamNumber = moduleInfo.getStreamNumberDebugInformation();
+ lengthSymbols = moduleInfo.getSizeLocalSymbolsDebugInformation();
+
+ }
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
+ int a = 1;
+ a = a + 1;
+ symLen = null;
+ nextRetrieveOffset = 0;
+ }
+ else {
+ initGet();
+ }
+ }
+ catch (PdbException e) {
+ symLen = null;
+ nextRetrieveOffset = 0;
+ Msg.warn(this, "Could not retrieve moduleInfo for module number: " + moduleNumber);
+ }
}
@Override
public boolean hasNext() {
- if (nextIndex == offsets.size()) {
- return false;
- }
- return true;
+ return (symLen != null);
}
/**
@@ -175,24 +219,38 @@ public class SymbolGroup {
* @throws NoSuchElementException if there are no more elements
*/
public AbstractMsSymbol peek() throws NoSuchElementException {
- if (nextIndex == offsets.size()) {
- throw new NoSuchElementException("none left");
+ if (symLen == null) {
+ throw new NoSuchElementException();
}
- long temporaryOffset = offsets.get(nextIndex);
- AbstractMsSymbol symbol = symbolsByOffset.get(temporaryOffset);
- if (symbol == null) {
- throw new NoSuchElementException("No symbol");
- }
- return symbol;
+ return symLen.symbol();
}
@Override
public AbstractMsSymbol next() {
- if (nextIndex == offsets.size()) {
- throw new NoSuchElementException("none left");
+ if (symLen == null) {
+ throw new NoSuchElementException();
+ }
+ SymbolRecords.SymLen offer = symLen;
+ symLen = retrieveRecord();
+ return offer.symbol();
+ }
+
+ private SymbolRecords.SymLen retrieveRecord() {
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
+ return null;
+ }
+ if (nextRetrieveOffset >= lengthSymbols) {
+ return null;
+ }
+ try {
+ SymbolRecords.SymLen retrieved =
+ symbolRecords.getRandomAccessRecord(streamNumber, nextRetrieveOffset);
+ nextRetrieveOffset += retrieved.length();
+ return retrieved;
+ }
+ catch (PdbException | CancelledException e) {
+ return null;
}
- currentOffset = offsets.get(nextIndex++);
- return symbolsByOffset.get(currentOffset);
}
/**
@@ -203,7 +261,10 @@ public class SymbolGroup {
* @throws NoSuchElementException if there are no more elements
*/
long getCurrentOffset() {
- return currentOffset;
+ if (symLen == null) {
+ throw new NoSuchElementException();
+ }
+ return nextRetrieveOffset - symLen.length();
}
/**
@@ -211,7 +272,17 @@ public class SymbolGroup {
* @see #hasNext()
*/
void initGet() {
- nextIndex = 0;
+ if (streamNumber == MsfStream.NIL_STREAM_NUMBER) {
+ return;
+ }
+ try {
+ nextRetrieveOffset = symbolRecords.getCvSigLength(streamNumber);
+ symLen = retrieveRecord();
+ }
+ catch (PdbException | CancelledException | IOException e) {
+ nextRetrieveOffset = 0;
+ symLen = null;
+ }
}
/**
@@ -220,15 +291,11 @@ public class SymbolGroup {
* @see #hasNext()
*/
void initGetByOffset(long offset) {
- int index = indexByOffset.get(offset);
- if (index < 0) {
- index = 0;
- }
- nextIndex = index;
- currentOffset = offset;
+ Long l = offset;
+ nextRetrieveOffset = l.intValue();
+ symLen = retrieveRecord();
}
- // TODO: might not need this
/**
* Returns the module number.
* @return the module number.