hotspot/agent/src/share/classes/sun/jvm/hotspot/debugger/dbx/DbxDebuggerLocal.java
author coleenp
Sun, 13 Apr 2008 17:43:42 -0400
changeset 360 21d113ecbf6a
parent 1 489c9b5090e2
child 670 ddf3e9583f2f
permissions -rw-r--r--
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.debugger.dbx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.debugger.dbx.sparc.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.debugger.dbx.x86.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.debugger.cdbg.CDebugger;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
/** <P> An implementation of the JVMDebugger interface which sits on
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    top of dbx and relies on the SA's dbx import module for
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    communication with the debugger. </P>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    <P> <B>NOTE</B> that since we have the notion of fetching "Java
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    primitive types" from the remote process (which might have
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    different sizes than we expect) we have a bootstrapping
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    problem. We need to know the sizes of these types before we can
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    fetch them. The current implementation solves this problem by
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    requiring that it be configured with these type sizes before they
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    can be fetched. The readJ(Type) routines here will throw a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    RuntimeException if they are called before the debugger is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    configured with the Java primitive type sizes. </P>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
*/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
public class DbxDebuggerLocal extends DebuggerBase implements DbxDebugger {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // These may be set by DbxDebuggerRemote
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  protected boolean unalignedAccessesOkay;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  protected DbxThreadFactory threadFactory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  private String dbxPathName;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  private String[] dbxSvcAgentDSOPathNames;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  private Process dbxProcess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  private StreamMonitor dbxOutStreamMonitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  private StreamMonitor dbxErrStreamMonitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  private PrintWriter dbxOstr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  private PrintWriter out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  private InputLexer in;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  private Socket importModuleSocket;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  private static final int PORT = 21928;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  private static final int  LONG_TIMEOUT = 60000;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  private static final int  DBX_MODULE_NOT_FOUND      = 101;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  private static final int  DBX_MODULE_LOADED         = 102;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // Implementation of Debugger interface
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  /** <P> machDesc may be null if it couldn't be determined yet; i.e.,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      if we're on SPARC, we need to ask the remote process whether
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      we're in 32- or 64-bit mode. </P>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      <P> useCache should be set to true if debugging is being done
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
      locally, and to false if the debugger is being created for the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      purpose of supporting remote debugging. </P> */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  public DbxDebuggerLocal(MachineDescription machDesc,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
                          String dbxPathName,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                          String[] dbxSvcAgentDSOPathNames,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                          boolean useCache) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    this.machDesc = machDesc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    this.dbxPathName = dbxPathName;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    this.dbxSvcAgentDSOPathNames = dbxSvcAgentDSOPathNames;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    int cacheNumPages;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    int cachePageSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    if (PlatformInfo.getCPU().equals("sparc")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
      cacheNumPages = parseCacheNumPagesProperty(2048);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      cachePageSize = 8192;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      threadFactory = new DbxSPARCThreadFactory(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    } else if (PlatformInfo.getCPU().equals("x86")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      cacheNumPages = 4096;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      cachePageSize = 4096;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      threadFactory = new DbxX86ThreadFactory(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      unalignedAccessesOkay = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      throw new RuntimeException("Thread access for CPU architecture " + PlatformInfo.getCPU() + " not yet supported");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    if (useCache) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      // Cache portion of the remote process's address space.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
      // Fetching data over the socket connection to dbx is relatively
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      // slow. For now, this cache works best if it covers the entire
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      // heap of the remote process. FIXME: at least should make this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      // tunable from the outside, i.e., via the UI. This is a 16 MB
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
      // cache divided on SPARC into 2048 8K pages and on x86 into
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      // 4096 4K pages; the page size must be adjusted to be the OS's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      // page size. (FIXME: should pick this up from the debugger.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      initCache(cachePageSize, cacheNumPages);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  /** Only called by DbxDebuggerRemote */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  protected DbxDebuggerLocal() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  /** FIXME: implement this with a Runtime.exec() of ps followed by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      parsing of its output */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  public boolean hasProcessList() throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  public List getProcessList() throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    throw new DebuggerException("Not yet supported");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  /** From the Debugger interface via JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  public synchronized void attach(int processID) throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      launchProcess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      dbxErrStreamMonitor.addTrigger("dbx: no process", 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
      dbxErrStreamMonitor.addTrigger("dbx: Cannot open", 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      dbxErrStreamMonitor.addTrigger("dbx: Cannot find", DBX_MODULE_NOT_FOUND);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
      dbxOstr = new PrintWriter(dbxProcess.getOutputStream(), true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      dbxOstr.println("debug - " + processID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      dbxOstr.println("kprint -u2 \\(ready\\)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      boolean seen = dbxErrStreamMonitor.waitFor("(ready)", LONG_TIMEOUT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      if (!seen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
        detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
        throw new DebuggerException("Timed out while connecting to process " + processID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      List retVals = dbxErrStreamMonitor.getTriggersSeen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      if (retVals.contains(new Integer(1))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
        detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
        throw new DebuggerException("No such process " + processID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      // Throws DebuggerException upon failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      importDbxModule();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      dbxOstr.println("svc_agent_run");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      connectToImportModule();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      // Set "fail fast" mode on process memory reads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      printlnToOutput("peek_fail_fast 1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      throw new DebuggerException("Error while connecting to dbx process", e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  /** From the Debugger interface via JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  public synchronized void attach(String executableName, String coreFileName) throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      launchProcess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      // Missing executable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      dbxErrStreamMonitor.addTrigger("dbx: Cannot open", 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      // Missing core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      dbxErrStreamMonitor.addTrigger("dbx: can't read", 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      // Corrupt executable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      dbxErrStreamMonitor.addTrigger("dbx: File", 3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
      // Corrupt core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      dbxErrStreamMonitor.addTrigger("dbx: Unable to read", 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
      // Mismatched core and executable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
      dbxErrStreamMonitor.addTrigger("dbx: core object name", 5);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
      // Missing loadobject
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      dbxErrStreamMonitor.addTrigger("dbx: can't stat", 6);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
      // Successful load of svc module
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      dbxOstr = new PrintWriter(dbxProcess.getOutputStream(), true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
      dbxOstr.println("debug " + executableName + " " + coreFileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      dbxOstr.println("kprint -u2 \\(ready\\)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      boolean seen = dbxErrStreamMonitor.waitFor("(ready)", LONG_TIMEOUT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      if (!seen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
        throw new DebuggerException("Timed out while attaching to core file");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      List retVals = dbxErrStreamMonitor.getTriggersSeen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
      if (retVals.size() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
        detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
        if (retVals.contains(new Integer(1))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
          throw new DebuggerException("Can not find executable \"" + executableName + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
        } else if (retVals.contains(new Integer(2))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
          throw new DebuggerException("Can not find core file \"" + coreFileName + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
        } else if (retVals.contains(new Integer(3))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
          throw new DebuggerException("Corrupt executable \"" + executableName + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
        } else if (retVals.contains(new Integer(4))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
          throw new DebuggerException("Corrupt core file \"" + coreFileName + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        } else if (retVals.contains(new Integer(5))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
          throw new DebuggerException("Mismatched core file/executable \"" + coreFileName + "\"/\"" + executableName + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
          throw new DebuggerException("Couldn't find all loaded libraries for executable \"" + executableName + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
      // Throws DebuggerException upon failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      importDbxModule();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
      dbxOstr.println("svc_agent_run");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      connectToImportModule();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      // Set "fail fast" mode on process memory reads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
      printlnToOutput("peek_fail_fast 1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      throw new DebuggerException("Error while connecting to dbx process", e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  /** From the Debugger interface via JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  public synchronized boolean detach() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      if (dbxProcess == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      if (out != null && dbxOstr != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
        printlnToOutput("exit");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
        dbxOstr.println("exit");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
        // Wait briefly for the process to exit (FIXME: should make this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
        // nicer)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
          Thread.sleep(500);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
        catch (InterruptedException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
      shutdown();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    } catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  /** From the Debugger interface via JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  public Address parseAddress(String addressString) throws NumberFormatException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    long addr = utils.scanAddress(addressString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    if (addr == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    return new DbxAddress(this, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  /** From the Debugger interface via JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  public String getOS() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    return PlatformInfo.getOS();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  /** From the Debugger interface via JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  public String getCPU() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    return PlatformInfo.getCPU();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  public boolean hasConsole() throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  public synchronized String consoleExecuteCommand(String cmd) throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      // A little tricky. We need to cause the dbx import module to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
      // exit, then print our command on dbx's stdin along with a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
      // command which will allow our StreamMonitors to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
      // resynchronize. We need save the output from the StreamMonitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
      // along the way.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
      printlnToOutput("exit");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      importModuleSocket.close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      importModuleSocket = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      out = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      in = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      dbxOstr.println("kprint \\(ready\\)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
      dbxOstr.flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      dbxOutStreamMonitor.waitFor("(ready)", LONG_TIMEOUT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
      dbxOutStreamMonitor.startCapture();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
      dbxErrStreamMonitor.startCapture();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
      dbxOstr.println(cmd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      dbxOstr.println("kprint \\(ready\\)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
      dbxOutStreamMonitor.waitFor("(ready)", LONG_TIMEOUT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
      String result = dbxOutStreamMonitor.stopCapture();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
      String result2 = dbxErrStreamMonitor.stopCapture();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
      result = result + result2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
      // Cut out the "(ready)" string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
      StringBuffer outBuf = new StringBuffer(result.length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
      BufferedReader reader = new BufferedReader(new StringReader(result));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
      // FIXME: bug in BufferedReader? readLine returns null when
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      // ready() returns true.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      String line = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
      do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
        line = reader.readLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
        if ((line != null) && (!line.equals("(ready)"))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
          outBuf.append(line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
          outBuf.append("\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
      } while (line != null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
      dbxOstr.println("svc_agent_run");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
      dbxOstr.flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
      connectToImportModule();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
      return outBuf.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
      detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
      throw new DebuggerException("Error while executing command on dbx console", e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  public String getConsolePrompt() throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    return "(dbx) ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  public CDebugger getCDebugger() throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  /** From the SymbolLookup interface via Debugger and JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  public synchronized Address lookup(String objectName, String symbol) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    long addr = lookupInProcess(objectName, symbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    if (addr == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    return new DbxAddress(this, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  /** From the SymbolLookup interface via Debugger and JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  public synchronized OopHandle lookupOop(String objectName, String symbol) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    long addr = lookupInProcess(objectName, symbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    if (addr == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    return new DbxOopHandle(this, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  /** From the Debugger interface */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  public MachineDescription getMachineDescription() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    return machDesc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  /** Internal routine supporting lazy setting of MachineDescription,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
      since on SPARC we will need to query the remote process to ask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
      it what its data model is (32- or 64-bit). NOTE that this is NOT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
      present in the DbxDebugger interface because it should not be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
      called across the wire (until we support attaching to multiple
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
      remote processes via RMI -- see the documentation for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
      DbxDebuggerRemoteIntf.) */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  public void setMachineDescription(MachineDescription machDesc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    this.machDesc = machDesc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    setBigEndian(machDesc.isBigEndian());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    utils = new DebuggerUtilities(machDesc.getAddressSize(), machDesc.isBigEndian());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  /** Internal routine which queries the remote process about its data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
      model -- i.e., size of addresses. Returns -1 upon error.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
      Currently supported return values are 32 and 64. NOTE that this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
      is NOT present in the DbxDebugger interface because it should
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
      not be called across the wire (until we support attaching to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
      multiple remote processes via RMI -- see the documentation for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
      DbxDebuggerRemoteIntf.) */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  public int getRemoteProcessAddressSize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    if (dbxProcess == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
      throw new RuntimeException("Not attached to remote process");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
      printlnToOutput("address_size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
      int i = in.parseInt();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
      return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  // Implementation of ThreadAccess interface
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  /** From the ThreadAccess interface via Debugger and JVMDebugger */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  public ThreadProxy getThreadForIdentifierAddress(Address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    return threadFactory.createThreadWrapper(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  public ThreadProxy getThreadForThreadId(long id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    return threadFactory.createThreadWrapper(id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  //----------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  // Overridden from DebuggerBase because we need to relax alignment
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  // constraints on x86
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  public long readJLong(long address)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
    throws UnmappedAddressException, UnalignedAddressException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
    checkJavaConfigured();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
    // FIXME: allow this to be configurable. Undesirable to add a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
    // dependency on the runtime package here, though, since this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    // package should be strictly underneath it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
    if (unalignedAccessesOkay) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
      utils.checkAlignment(address, jintSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
      utils.checkAlignment(address, jlongSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
    byte[] data = readBytes(address, jlongSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    return utils.dataToJLong(data, jlongSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  // Internal routines (for implementation of DbxAddress).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  // These must not be called until the MachineDescription has been set up.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  /** From the DbxDebugger interface */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  public String addressValueToString(long address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
    return utils.addressValueToString(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  /** Need to override this to relax alignment checks on Solaris/x86. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  public long readCInteger(long address, long numBytes, boolean isUnsigned)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
    throws UnmappedAddressException, UnalignedAddressException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
    checkConfigured();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    if (!unalignedAccessesOkay) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
      utils.checkAlignment(address, numBytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
      // Only slightly relaxed semantics -- this is a hack, but is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
      // necessary on Solaris/x86 where it seems the compiler is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      // putting some global 64-bit data on 32-bit boundaries
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
      if (numBytes == 8) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
        utils.checkAlignment(address, 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
        utils.checkAlignment(address, numBytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    byte[] data = readBytes(address, numBytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    return utils.dataToCInteger(data, isUnsigned);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  /** From the DbxDebugger interface */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  public DbxAddress readAddress(long address)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
    throws UnmappedAddressException, UnalignedAddressException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
    long value = readAddressValue(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
    return (value == 0 ? null : new DbxAddress(this, value));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   463
  public DbxAddress readCompOopAddress(long address)
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   464
    throws UnmappedAddressException, UnalignedAddressException {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   465
    long value = readCompOopAddressValue(address);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   466
    return (value == 0 ? null : new DbxAddress(this, value));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   467
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   468
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  /** From the DbxDebugger interface */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  public DbxOopHandle readOopHandle(long address)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
    throws UnmappedAddressException, UnalignedAddressException, NotInHeapException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    long value = readAddressValue(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    return (value == 0 ? null : new DbxOopHandle(this, value));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  }
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   475
  public DbxOopHandle readCompOopHandle(long address)
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   476
    throws UnmappedAddressException, UnalignedAddressException, NotInHeapException {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   477
    long value = readCompOopAddressValue(address);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   478
    return (value == 0 ? null : new DbxOopHandle(this, value));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   479
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  // Thread context access. Can not be package private, but should
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  // only be accessed by the architecture-specific subpackages.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  /** From the DbxDebugger interface. May have to redefine this later. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  public synchronized long[] getThreadIntegerRegisterSet(int tid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
      printlnToOutput("thr_gregs " + tid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
      int num = in.parseInt();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
      long[] res = new long[num];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
      for (int i = 0; i < num; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
        res[i] = in.parseAddress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
      return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
    catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
      e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  // Address access. Can not be package private, but should only be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  // accessed by the architecture-specific subpackages.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  /** From the Debugger interface */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  public long getAddressValue(Address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
    if (addr == null) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
    return ((DbxAddress) addr).getValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  /** From the DbxDebugger interface */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  public Address newAddress(long value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
    if (value == 0) return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
    return new DbxAddress(this, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  // Internals only below this point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
  private void launchProcess() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    dbxProcess = Runtime.getRuntime().exec(dbxPathName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    //      dbxOutStreamMonitor = new StreamMonitor(dbxProcess.getInputStream());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    //      dbxErrStreamMonitor = new StreamMonitor(dbxProcess.getErrorStream());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
    dbxOutStreamMonitor = new StreamMonitor(dbxProcess.getInputStream(), "dbx stdout", true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    dbxErrStreamMonitor = new StreamMonitor(dbxProcess.getErrorStream(), "dbx stderr", true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  /** Requires that dbxErrStreamMonitor has a trigger on "dbx: Cannot
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
      find" with number DBX_MODULE_NOT_FOUND as well as one on "dbx:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
      warning:" (plus the serviceability agent's dbx module path name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
      to avoid conflation with inability to load individual object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
      files) with number DBX_MODULE_FAILED_TO_LOAD. The former
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
      indicates an absence of libsvc_agent_dbx.so, while the latter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
      indicates that the module failed to load, specifically because
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
      the architecture was mismatched. (I don't see a way to detect
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
      from the dbx command prompt whether it's running the v8 or v9
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
      executbale, so we try to import both flavors of the import
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
      module; the "v8" file name convention doesn't actually include
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
      the v8 prefix, so this code should work for Intel as well.) */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
  private void importDbxModule() throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    // Trigger for a successful load
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
    dbxOutStreamMonitor.addTrigger("Defining svc_agent_run", DBX_MODULE_LOADED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
    for (int i = 0; i < dbxSvcAgentDSOPathNames.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
      dbxOstr.println("import " + dbxSvcAgentDSOPathNames[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
      dbxOstr.println("kprint -u2 \\(Ready\\)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
      boolean seen = dbxErrStreamMonitor.waitFor("(Ready)", LONG_TIMEOUT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
      if (!seen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
        detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
        throw new DebuggerException("Timed out while importing dbx module from file\n" + dbxSvcAgentDSOPathNames[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
      List retVals = dbxErrStreamMonitor.getTriggersSeen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
      if (retVals.contains(new Integer(DBX_MODULE_NOT_FOUND))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
        detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
        throw new DebuggerException("Unable to find the Serviceability Agent's dbx import module at pathname \"" +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
                                    dbxSvcAgentDSOPathNames[i] + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
        retVals = dbxOutStreamMonitor.getTriggersSeen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
        if (retVals.contains(new Integer(DBX_MODULE_LOADED))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
          System.out.println("importDbxModule: imported " +  dbxSvcAgentDSOPathNames[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
          return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
    // Failed to load all flavors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
    detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
    String errMsg = ("Unable to find a version of the Serviceability Agent's dbx import module\n" +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
                     "matching the architecture of dbx at any of the following locations:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
    for (int i = 0; i < dbxSvcAgentDSOPathNames.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
      errMsg = errMsg + "\n" + dbxSvcAgentDSOPathNames[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
    throw new DebuggerException(errMsg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  /** Terminate the debugger forcibly */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  private void shutdown() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
    if (dbxProcess != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
      // See whether the process has exited and, if not, terminate it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
      // forcibly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
      try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
        dbxProcess.exitValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
      catch (IllegalThreadStateException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
        dbxProcess.destroy();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
      if (importModuleSocket != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
        importModuleSocket.close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
    // Release references to all objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
    clearCache();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  /** Looks up an address in the remote process's address space.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
      Returns 0 if symbol not found or upon error. Package private to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
      allow DbxDebuggerRemoteIntfImpl access. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  synchronized long lookupInProcess(String objectName, String symbol) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
      printlnToOutput("lookup " + objectName + " " + symbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
      return in.parseAddress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
    catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
      return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
  /** This reads bytes from the remote process. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  public synchronized ReadResult readBytesFromProcess(long address, long numBytes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
    throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
    if (numBytes < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
      throw new DebuggerException("Can not read negative number (" + numBytes + ") of bytes from process");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
      String cmd = "peek " + utils.addressValueToString(address) + " " + numBytes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
      printlnToOutput(cmd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
      while (in.readByte() != 'B') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
      byte res = in.readByte();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
      if (res == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
        System.err.println("Failing command: " + cmd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
        throw new DebuggerException("Read of remote process address space failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
      // NOTE: must read ALL of the data regardless of whether we need
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
      // to throw an UnmappedAddressException. Otherwise will corrupt
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
      // the input stream each time we have a failure. Not good. Do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
      // not want to risk "flushing" the input stream in case a huge
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
      // read has a hangup in the middle and we leave data on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
      // stream.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
      byte[] buf = new byte[(int) numBytes];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
      boolean bailOut = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
      long failureAddress = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
      int numReads = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
      while (numBytes > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
        long len = in.readUnsignedInt();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
        boolean isMapped = ((in.readByte() == 0) ? false : true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
        if (!isMapped) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
          if (!bailOut) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
            bailOut = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
            failureAddress = address;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
          // This won't work if we have unmapped regions, but if we do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
          // then we're going to throw an exception anyway
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
          // NOTE: there is a factor of 20 speed difference between
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
          // these two ways of doing this read.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
          in.readBytes(buf, 0, (int) len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
        // Do NOT do this:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
        //        for (int i = 0; i < (int) len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
        //          buf[i] = in.readByte();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
        //        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
        numBytes -= len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
        address += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
        ++numReads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
      if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
        Assert.that(numBytes == 0, "Bug in debug server's implementation of peek: numBytesLeft == " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
                    numBytes + ", should be 0 (did " + numReads + " reads)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
      if (bailOut) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
        return new ReadResult(failureAddress);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
      return new ReadResult(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
      throw new DebuggerException(e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
  public void writeBytesToProcess(long address, long numBytes, byte[] data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
    throws UnmappedAddressException, DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
    // FIXME
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
    throw new DebuggerException("Unimplemented");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  /** This provides DbxDebuggerRemoteIntfImpl access to readBytesFromProcess */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  ReadResult readBytesFromProcessInternal(long address, long numBytes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
    throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
    return readBytesFromProcess(address, numBytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
  /** Convenience routine */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
  private void printlnToOutput(String s) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    out.println(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
    if (out.checkError()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
      throw new IOException("Error occurred while writing to debug server");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
  private void clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
    dbxProcess = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
    dbxOstr = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
    out = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
    in = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
    importModuleSocket = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  /** Connects to the dbx import module, setting up out and in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
      streams. Factored out to allow access to the dbx console. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
  private void connectToImportModule() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
    // Try for 20 seconds to connect to dbx import module; time out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
    // with failure if didn't succeed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
    importModuleSocket = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
    long endTime = System.currentTimeMillis() + LONG_TIMEOUT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
    while ((importModuleSocket == null) && (System.currentTimeMillis() < endTime)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
      try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
        importModuleSocket = new Socket(InetAddress.getLocalHost(), PORT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
        importModuleSocket.setTcpNoDelay(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
      catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
        // Swallow IO exceptions while attempting connection
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
          // Don't swamp the CPU
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
          Thread.sleep(1000);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
        catch (InterruptedException ex) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
    if (importModuleSocket == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
      // Failed to connect because of timeout
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
      detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
      throw new DebuggerException("Timed out while attempting to connect to remote dbx process");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(importModuleSocket.getOutputStream(), "US-ASCII")), true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
    in = new InputLexer(new BufferedInputStream(importModuleSocket.getInputStream()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
}