OSDN Git Service

Various fixes to the DDMS Native heap panel.
authorXavier Ducrohet <xav@android.com>
Mon, 18 Jul 2011 22:54:39 +0000 (15:54 -0700)
committerXavier Ducrohet <xav@android.com>
Mon, 18 Jul 2011 22:56:45 +0000 (15:56 -0700)
Change-Id: I468be358b937f63adca3528fb66aa5d70e2e6827

ddms/libs/ddmlib/src/com/android/ddmlib/AndroidDebugBridge.java
ddms/libs/ddmlib/src/com/android/ddmlib/HandleNativeHeap.java
ddms/libs/ddmlib/src/com/android/ddmlib/NativeAllocationInfo.java
ddms/libs/ddmuilib/src/com/android/ddmuilib/Addr2Line.java
ddms/libs/ddmuilib/src/com/android/ddmuilib/NativeHeapPanel.java

index f697cba..253f41f 100644 (file)
@@ -195,6 +195,7 @@ public final class AndroidDebugBridge {
         HandleHeap.register(monitorThread);
         HandleWait.register(monitorThread);
         HandleProfiling.register(monitorThread);
+        HandleNativeHeap.register(monitorThread);
     }
 
     /**
index 2b91b1e..8482863 100644 (file)
@@ -171,7 +171,7 @@ final class HandleNativeHeap extends ChunkHandler {
                     buffer.getInt() /* allocations */);
 
             for (int j = 0 ; j < backtraceSize ; j++) {
-                long addr = ((long)buffer.getInt()) & 0x00000000ffffffffL;
+                long addr = (buffer.getInt()) & 0x00000000ffffffffL;
 
                 info.addStackCallAddress(addr);;
             }
@@ -203,8 +203,8 @@ final class HandleNativeHeap extends ChunkHandler {
         buffer.order(ByteOrder.BIG_ENDIAN);
 
         int id = buffer.getInt();
-        int unitsize = (int) buffer.get();
-        long startAddress = (long) buffer.getInt() & 0x00000000ffffffffL;
+        int unitsize = buffer.get();
+        long startAddress = buffer.getInt() & 0x00000000ffffffffL;
         int offset = buffer.getInt();
         int allocationUnitCount = buffer.getInt();
 
@@ -218,8 +218,8 @@ final class HandleNativeHeap extends ChunkHandler {
 
         // read the usage
         while (buffer.position() < buffer.limit()) {
-            int eState = (int)buffer.get() & 0x000000ff;
-            int eLen = ((int)buffer.get() & 0x000000ff) + 1;
+            int eState = buffer.get() & 0x000000ff;
+            int eLen = (buffer.get() & 0x000000ff) + 1;
             //Log.e("ddm-nativeheap", "solidity: " + (eState & 0x7) + " - kind: "
             //        + ((eState >> 3) & 0x7) + " - len: " + eLen);
         }
@@ -254,36 +254,29 @@ final class HandleNativeHeap extends ChunkHandler {
                     long tmpStart = Long.parseLong(line.substring(0, 8), 16);
                     long tmpEnd = Long.parseLong(line.substring(9, 17), 16);
 
-                     /*
-                      * only check for library addresses as defined in
-                      * //device/config/prelink-linux-arm.map
-                      */
-                    if (tmpStart >= 0x0000000080000000L && tmpStart <= 0x00000000BFFFFFFFL) {
+                    int index = line.indexOf('/');
 
-                        int index = line.indexOf('/');
+                    if (index == -1)
+                        continue;
 
-                        if (index == -1)
-                            continue;
+                    String tmpLib = line.substring(index);
 
-                        String tmpLib = line.substring(index);
+                    if (library == null ||
+                            (library != null && tmpLib.equals(library) == false)) {
 
-                        if (library == null ||
-                                (library != null && tmpLib.equals(library) == false)) {
-
-                            if (library != null) {
-                                cd.addNativeLibraryMapInfo(startAddr, endAddr, library);
-                                Log.d("ddms", library + "(" + Long.toHexString(startAddr) +
-                                        " - " + Long.toHexString(endAddr) + ")");
-                            }
-
-                            // now init the new library
-                            library = tmpLib;
-                            startAddr = tmpStart;
-                            endAddr = tmpEnd;
-                        } else {
-                            // add the new end
-                            endAddr = tmpEnd;
+                        if (library != null) {
+                            cd.addNativeLibraryMapInfo(startAddr, endAddr, library);
+                            Log.d("ddms", library + "(" + Long.toHexString(startAddr) +
+                                    " - " + Long.toHexString(endAddr) + ")");
                         }
+
+                        // now init the new library
+                        library = tmpLib;
+                        startAddr = tmpStart;
+                        endAddr = tmpEnd;
+                    } else {
+                        // add the new end
+                        endAddr = tmpEnd;
                     }
                 } catch (NumberFormatException e) {
                     e.printStackTrace();
index 41d63b2..9909b9a 100644 (file)
@@ -51,6 +51,8 @@ public final class NativeAllocationInfo {
         sAllocFunctionFilter.add("chk_memalign"); //$NON-NLS-1$
         sAllocFunctionFilter.add("Malloc"); //$NON-NLS-1$
         sAllocFunctionFilter.add("leak_memalign"); //$NON-NLS-1$
+        sAllocFunctionFilter.add("strcmp"); //$NON-NLS-1$
+        sAllocFunctionFilter.add("dlrealloc"); //$NON-NLS-1$
     }
 
     private final int mSize;
@@ -247,7 +249,7 @@ public final class NativeAllocationInfo {
                 long addr = addrIterator.next();
                 NativeStackCallInfo info = sourceIterator.next();
                 if (addr != 0 && info != null) {
-                    if (isRelevant(info.getMethodName())) {
+                    if (isRelevant(info.getMethodName(), addr)) {
                         return info;
                     }
                 }
@@ -265,14 +267,16 @@ public final class NativeAllocationInfo {
     /**
      * Returns true if the method name is relevant.
      * @param methodName the method name to test.
+     * @param addr the original address. This is used because sometimes the name of the method is
+     * the address itself which is not relevant
      */
-    private boolean isRelevant(String methodName) {
+    private boolean isRelevant(String methodName, long addr) {
         for (String filter : sAllocFunctionFilter) {
             if (methodName.contains(filter)) {
                 return false;
             }
         }
 
-        return true;
+        return methodName.equals(Long.toString(addr, 16)) == false;
     }
 }
index 5663fc2..c921ac2 100644 (file)
@@ -16,7 +16,8 @@
 
 package com.android.ddmuilib;
 
-import com.android.ddmlib.*;
+import com.android.ddmlib.Log;
+import com.android.ddmlib.NativeStackCallInfo;
 
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
@@ -138,9 +139,14 @@ public class Addr2Line {
             symbols = DdmUiPreferences.getSymbolDirectory();
         }
 
+        String addr2Line = System.getenv("ANDROID_ADDR2LINE");
+        if (addr2Line == null) {
+            addr2Line = DdmUiPreferences.getAddr2Line();
+        }
+
         // build the command line
         String[] command = new String[5];
-        command[0] = DdmUiPreferences.getAddr2Line();
+        command[0] = addr2Line;
         command[1] = "-C";
         command[2] = "-f";
         command[3] = "-e";
index 0b2460b..c7f7b4f 100644 (file)
@@ -77,7 +77,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
     private static final int NUM_PALETTE_ENTRIES = HeapSegmentElement.KIND_NATIVE+2 +1;
     private static final String[] mMapLegend = new String[NUM_PALETTE_ENTRIES];
     private static final PaletteData mMapPalette = createPalette();
-    
+
     private static final int ALLOC_DISPLAY_ALL = 0;
     private static final int ALLOC_DISPLAY_PRE_ZYGOTE = 1;
     private static final int ALLOC_DISPLAY_POST_ZYGOTE = 2;
@@ -137,7 +137,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
     private Table mDetailTable;
 
     private Label mImage;
-    
+
     private int mAllocDisplayMode = ALLOC_DISPLAY_ALL;
 
     /**
@@ -163,7 +163,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
     /** list of NativeAllocationInfo objects filled with the list from ClientData */
     private final ArrayList<NativeAllocationInfo> mAllocations =
         new ArrayList<NativeAllocationInfo>();
-    
+
     /** list of the {@link NativeAllocationInfo} being displayed based on the selection
      * of {@link #mAllocDisplayCombo}.
      */
@@ -271,9 +271,9 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 if (info.isStackCallResolved() == false) {
                     final Long[] list = info.getStackCallAddresses();
                     final int size = list.length;
-                    
+
                     ArrayList<NativeStackCallInfo> resolvedStackCall =
-                        new ArrayList<NativeStackCallInfo>(); 
+                        new ArrayList<NativeStackCallInfo>();
 
                     for (int i = 0; i < size; i++) {
                         long addr = list[i];
@@ -289,7 +289,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
 
                         resolvedStackCall.add(source);
                     }
-                    
+
                     info.setResolvedStackCall(resolvedStackCall);
                 }
                 // after every DISPLAY_PER_PAGE we ask for a ui refresh, unless
@@ -488,7 +488,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
         gl.verticalSpacing = 0;
         mBase.setLayout(gl);
         mBase.setLayoutData(new GridData(GridData.FILL_BOTH));
-        
+
         // composite for <update btn> <status>
         Composite tmp = new Composite(mBase, SWT.NONE);
         tmp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
@@ -527,9 +527,9 @@ public final class NativeHeapPanel extends BaseHeapPanel {
         Composite top_layout = new Composite(mBase, SWT.NONE);
         top_layout.setLayout(gl = new GridLayout(4, false));
         gl.marginWidth = gl.marginHeight = 0;
-        
+
         new Label(top_layout, SWT.NONE).setText("Show:");
-        
+
         mAllocDisplayCombo = new Combo(top_layout, SWT.DROP_DOWN | SWT.READ_ONLY);
         mAllocDisplayCombo.setLayoutData(new GridData(
                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
@@ -543,7 +543,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
             }
         });
         mAllocDisplayCombo.select(0);
-        
+
         // separator
         Label separator = new Label(top_layout, SWT.SEPARATOR | SWT.VERTICAL);
         GridData gd;
@@ -569,7 +569,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 }
             }
         });
-        
+
         /*
          * TODO: either fix the diff mechanism or remove it altogether.
         mDiffUpdateButton = new Button(top_layout, SWT.NONE);
@@ -619,7 +619,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
             }
         });
         mDisplayModeCombo.setEnabled(false);
-        
+
         mSymbolsButton = new Button(top_layout, SWT.PUSH);
         mSymbolsButton.setText("Load Symbols");
         mSymbolsButton.setEnabled(false);
@@ -651,7 +651,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
 
         return mBase;
     }
-    
+
     /**
      * Sets the focus to the proper control inside the panel.
      */
@@ -754,7 +754,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 // we're done: do something
                 mDisplayModeCombo.setEnabled(true);
                 mSaveButton.setEnabled(true);
-                
+
                 mStackCallThread = null;
             } else {
                 // work in progress, update the progress bar.
@@ -773,7 +773,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 // get the current selection of the allocation
                 int index = mAllocationTable.getSelectionIndex();
                 NativeAllocationInfo info = null;
-                
+
                 if (index != -1) {
                     info = (NativeAllocationInfo)mAllocationTable.getItem(index).getData();
                 }
@@ -811,13 +811,13 @@ public final class NativeHeapPanel extends BaseHeapPanel {
         addTableToFocusListener(mLibraryAllocationTable);
         addTableToFocusListener(mDetailTable);
     }
-    
+
     protected void onAllocDisplayChange() {
         mAllocDisplayMode = mAllocDisplayCombo.getSelectionIndex();
-        
+
         // create the new list
         updateAllocDisplayList();
-        
+
         updateTotalMemoryDisplay();
 
         // reset the ui.
@@ -825,7 +825,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
         updatePageUI();
         switchDisplayMode();
     }
-    
+
     private void updateAllocDisplayList() {
         mTotalSize = 0;
         mDisplayedAllocations.clear();
@@ -839,9 +839,9 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 continue;
             }
         }
-        
+
         int count = mDisplayedAllocations.size();
-        
+
         mPageCount = count / DISPLAY_PER_PAGE;
 
         // need to add a page for the rest of the div
@@ -849,7 +849,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
             mPageCount++;
         }
     }
-    
+
     private void updateTotalMemoryDisplay() {
         switch (mAllocDisplayMode) {
             case ALLOC_DISPLAY_ALL:
@@ -892,9 +892,13 @@ public final class NativeHeapPanel extends BaseHeapPanel {
     }
 
     private void initAllocationDisplay() {
+        if (mStackCallThread != null) {
+            mStackCallThread.quit();
+        }
+
         mAllocations.clear();
         mAllocations.addAll(mClientData.getNativeAllocationList());
-        
+
         updateAllocDisplayList();
 
         // if we have a previous clientdata and it matches the current one. we
@@ -1116,12 +1120,12 @@ public final class NativeHeapPanel extends BaseHeapPanel {
     private void fillDetailTable(final NativeAllocationInfo mi) {
         mDetailTable.removeAll();
         mDetailTable.setRedraw(false);
-        
+
         try {
             // populate the detail Table with the back trace
             Long[] addresses = mi.getStackCallAddresses();
             NativeStackCallInfo[] resolvedStackCall = mi.getResolvedStackCall();
-            
+
             if (resolvedStackCall == null) {
                 return;
             }
@@ -1130,18 +1134,18 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 if (addresses[i] == null || addresses[i].longValue() == 0) {
                     continue;
                 }
-                
+
                 long addr = addresses[i].longValue();
                 NativeStackCallInfo source = resolvedStackCall[i];
-                
+
                 TableItem item = new TableItem(mDetailTable, SWT.NONE);
                 item.setText(0, String.format("%08x", addr)); //$NON-NLS-1$
-    
+
                 String libraryName = source.getLibraryName();
                 String methodName = source.getMethodName();
                 String sourceFile = source.getSourceFile();
                 int lineNumber = source.getLineNumber();
-                
+
                 if (libraryName != null)
                     item.setText(1, libraryName);
                 if (methodName != null)
@@ -1373,9 +1377,11 @@ public final class NativeHeapPanel extends BaseHeapPanel {
             public void widgetSelected(SelectionEvent e) {
                 // get the selection index
                 int index = mAllocationTable.getSelectionIndex();
-                TableItem item = mAllocationTable.getItem(index);
-                if (item != null && item.getData() instanceof NativeAllocationInfo) {
-                    fillDetailTable((NativeAllocationInfo)item.getData());
+                if (index >= 0 && index < mAllocationTable.getItemCount()) {
+                    TableItem item = mAllocationTable.getItem(index);
+                    if (item != null && item.getData() instanceof NativeAllocationInfo) {
+                        fillDetailTable((NativeAllocationInfo)item.getData());
+                    }
                 }
             }
         });
@@ -1437,9 +1443,11 @@ public final class NativeHeapPanel extends BaseHeapPanel {
                 // get the index in the library allocation table
                 int index2 = mLibraryAllocationTable.getSelectionIndex();
                 // get the MallocInfo object
-                LibraryAllocations liballoc = mLibraryAllocations.get(index1);
-                NativeAllocationInfo info = liballoc.getAllocation(index2);
-                fillDetailTable(info);
+                if (index1 != -1 && index2 != -1) {
+                    LibraryAllocations liballoc = mLibraryAllocations.get(index1);
+                    NativeAllocationInfo info = liballoc.getAllocation(index2);
+                    fillDetailTable(info);
+                }
             }
         });
 
@@ -1495,7 +1503,7 @@ public final class NativeHeapPanel extends BaseHeapPanel {
     private void sortAllocationsPerLibrary() {
         if (mClientData != null) {
             mLibraryAllocations.clear();
-            
+
             // create a hash map of LibraryAllocations to access aggregate
             // objects already created
             HashMap<String, LibraryAllocations> libcache =
@@ -1617,11 +1625,11 @@ public final class NativeHeapPanel extends BaseHeapPanel {
 
         return new PaletteData(colors);
     }
-    
+
     private void saveAllocations(String fileName) {
         try {
             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
-    
+
             for (NativeAllocationInfo alloc : mAllocations) {
                 out.println(alloc.toString());
             }