1 19 20 package org.netbeans.modules.scripting.php.dbginterface; 21 22 import java.io.BufferedInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.Socket ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import javax.swing.SwingUtilities ; 33 import org.netbeans.api.debugger.Breakpoint; 34 import org.netbeans.api.debugger.DebuggerManager; 35 import org.netbeans.modules.scripting.php.dbginterface.api.DbgSourceMap; 36 import org.netbeans.modules.scripting.php.dbginterface.breakpoints.PhpBreakpoint; 37 import org.netbeans.modules.scripting.php.dbginterface.models.DebugError; 38 import org.netbeans.modules.scripting.php.dbginterface.models.DebugFrame; 39 import org.netbeans.modules.scripting.php.dbginterface.models.Variable; 40 import org.openide.ErrorManager; 41 import org.openide.filesystems.FileObject; 42 import org.openide.text.Line; 43 import org.openide.util.Utilities; 44 45 49 public class DbgServerHandler extends DbgDebuggerImpl.Context implements Runnable { 50 private Socket socket; 51 private InputStream in; 52 private int flags; 53 private boolean stop; 54 private List <DbgPacket> packetsToSend = new ArrayList <DbgPacket>(); 55 private Thread handlerThread; 56 private String sessionId; 57 private DbgSourceMap sourceMap; 58 private DebuggerAnnotation currentLineAnnotation; 59 private Line currentLine; 60 private Set <DebuggerAnnotation> errorAnnotations = new HashSet <DebuggerAnnotation>(); 61 private boolean suspended; 62 private List <DebugFrame> callStack; 63 private Map <Integer , String > sourceFileMap = new HashMap <Integer , String >(); 65 66 public DbgServerHandler(DbgDebuggerImpl impl, Socket socket) { 67 super(impl); 68 sourceMap = impl.getSourceMap(); 69 this.socket = socket; 70 stop = false; 71 72 try { 73 in = new BufferedInputStream (socket.getInputStream()); 74 } 75 catch (IOException ioe) { 76 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 77 } 78 } 79 80 public void sendPacket(DbgPacket packet) { 81 if (handlerThread == null) { 82 return; 83 } 84 85 synchronized (packetsToSend) { 86 packetsToSend.add(packet); 87 } 88 89 handlerThread.interrupt(); 90 } 91 92 public void stop() { 93 Thread.dumpStack(); 94 synchronized (this) { 95 stop = true; 96 97 if (handlerThread != null) { 98 handlerThread.interrupt(); 99 } 100 } 101 } 102 103 public void run() { 104 System.err.println("mw DbgServerHandler.run() started!"); 105 106 synchronized (this) { 107 handlerThread = Thread.currentThread(); 108 } 109 110 while (!stop && !socket.isClosed()) { 111 try { 112 List <DbgPacket> toSend = null; 113 114 synchronized (packetsToSend) { 115 if (packetsToSend != null && packetsToSend.size() > 0) { 116 toSend = new ArrayList <DbgPacket>(packetsToSend); 117 packetsToSend.clear(); 118 } 119 } 120 121 if (toSend != null) { 122 for (DbgPacket p : toSend) { 123 if (p.isWaitAck()) { 124 handleIncomingPackets(false); 126 } 127 128 p.send(socket.getOutputStream()); 129 130 if (p.isWaitAck()) { 131 handleIncomingPackets(true); 133 } 134 } 135 } 136 137 handleIncomingPackets(false); 138 } 139 catch (IOException ioe) { 140 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 141 } 142 143 try { 144 Thread.sleep(100); 148 } 149 catch (InterruptedException ie) { 150 } 152 } 153 154 if (!socket.isClosed()) { 155 try { 156 socket.close(); 157 } catch (IOException ioe) { 158 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 159 } 160 } 161 162 synchronized (this) { 163 handlerThread = null; 164 } 165 166 callStack = null; 167 getServer().getCallStackModel().refresh(true); 168 getServer().getVariablesModel().clearModel(); 169 hideCurrentLine(); 170 171 for (DebuggerAnnotation a : errorAnnotations) { 172 a.detach(); 173 } 174 } 175 176 public boolean isSuspended() { 177 return suspended; 178 } 179 180 public void setSuspended(boolean s) { 181 suspended = s; 182 183 getServer().setEnableContActions(s); 184 } 185 186 public void setCurrentLine(Line line) { 187 currentLine =line; 188 } 189 190 public void finish() { 191 getServer().closeScriptContext(this); 192 } 193 194 public DbgSourceMap getSourceMap() { 195 return sourceMap; 196 } 197 198 public void addError(DebugError e) { 199 String aType = null; 200 if (e.isError()) { 201 aType = DebuggerAnnotation.ERROR_ANNOTATION_TYPE; 202 } 203 else if (e.isNotice()) { 204 aType = DebuggerAnnotation.NOTICE_ANNOTATION_TYPE; 205 } 206 else if (e.isWarning()) { 207 aType = DebuggerAnnotation.WARNING_ANNOTATION_TYPE; 208 } 209 210 errorAnnotations.add(new DebuggerAnnotation(aType, e.getLine(), e.getString())); 211 } 212 213 public void hideCurrentLine() { 214 if (currentLineAnnotation != null) { 215 currentLineAnnotation.detach(); 216 currentLineAnnotation = null; 217 } 218 } 219 220 public void handleCurrentLine() { 221 if (currentLineAnnotation != null) { 222 currentLineAnnotation.detach(); 223 } 224 225 if (currentLine != null) { 226 final Line line = currentLine; 227 228 currentLineAnnotation = new DebuggerAnnotation(DebuggerAnnotation.CURRENT_LINE_ANNOTATION_TYPE, 229 line); 230 231 SwingUtilities.invokeLater(new Runnable () { 232 public void run() { 233 line.show(Line.SHOW_GOTO); 234 } 235 }); 236 } 237 } 238 239 public void setCallStack(List <DebugFrame> list) { 240 callStack = list; 241 getServer().getCallStackModel().setNeedsRefresh(); 242 getServer().getVariablesModel().setStackFrame(callStack.get(0)); 243 getServer().getWatchesModel().setStackFrame(callStack.get(0)); 244 } 245 246 public List <DebugFrame> getCallStack() { 247 return callStack; 248 } 249 250 public void addSourceFileInfo(int modNo, String filename) { 251 sourceFileMap.put(new Integer (modNo), filename); 252 } 253 254 public String getSourceFileInfo(int modNo) { 255 return sourceFileMap.get(new Integer (modNo)); 256 } 257 258 public Variable getScopeVariables(DebugFrame frame) { 259 DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST); 262 263 packet.setFlags(DbgConstants.DBGF_WAITACK); 264 DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, "$this"); 265 266 packet.addFrame(f); 267 packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), frame.getScope())); 268 269 try { 270 packet.send(socket.getOutputStream()); 271 packet = DbgPacket.receive(in); 273 packet.handle(this); 274 } 275 catch (IOException ioe) { 276 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 277 } 278 279 packet = new DbgPacket(DbgConstants.DBGA_REQUEST); 281 282 packet.setFlags(DbgConstants.DBGF_WAITACK); 283 packet.addFrame(new DbgPacket.FrameEval(0, frame.getScope())); 284 285 try { 286 packet.send(socket.getOutputStream()); 287 packet = DbgPacket.receive(in); 289 packet.handle(this); 290 } 291 catch (IOException ioe) { 292 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 293 } 294 295 List <Variable> vars = packet.getEvalValue(); 296 Variable ret = null; 297 298 if (vars != null) { 299 ret = vars.get(0); 300 } 301 return ret; 302 } 303 304 public void setVariableValue(DebugFrame frame, Variable v, Object value) { 305 DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST); 306 307 packet.setFlags(DbgConstants.DBGF_WAITACK); 308 DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, v.getDisplayName() + 309 "=" + value + ";"); 310 311 packet.addFrame(f); 312 packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), frame.getScope())); 313 314 try { 315 packet.send(socket.getOutputStream()); 316 packet = DbgPacket.receive(in); 318 packet.handle(this); 319 } 320 catch (IOException ioe) { 321 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 322 } 323 324 } 325 326 public Variable evaluateExpr(DebugFrame frame, String expr) { 327 if (expr == null) { 328 return null; 329 } 330 331 DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST); 332 333 packet.setFlags(DbgConstants.DBGF_WAITACK); 334 DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, expr); 335 336 packet.addFrame(f); 337 packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), frame.getScope())); 338 339 try { 340 packet.send(socket.getOutputStream()); 341 packet = DbgPacket.receive(in); 343 packet.handle(this); 344 } 345 catch (IOException ioe) { 346 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 347 } 348 349 List <Variable> vars = packet.getEvalValue(); 350 Variable ret = null; 351 352 if (vars != null) { 353 ret = vars.get(0); 354 } 355 return ret; 356 } 357 358 public void setSessionId(String sId) { 359 sessionId = sId; 360 } 361 362 public void setBreakpoints() { 363 Breakpoint[] breakpoints = DebuggerManager.getDebuggerManager().getBreakpoints(); 364 365 for (int i = 0; i < breakpoints.length; i++) { 366 System.err.println("mw DbgServerHandler.setBreakpoints() breakpoints[" + i + "]= " + breakpoints[i]); 367 368 if (breakpoints[i] instanceof PhpBreakpoint) { 369 PhpBreakpoint b = (PhpBreakpoint)breakpoints[i]; 370 371 Line line = b.getLine(); 372 FileObject sourceFile = line.getLookup().lookup(FileObject.class); 373 String targetFile = sourceMap.mapToServerPath(sourceFile); 374 375 if (targetFile != null) { 376 377 System.err.println("mw addBP! line= " + sourceFile + ":" + (line.getLineNumber() + 1)); 378 System.err.println("mw targetFile= " + targetFile); 379 380 DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST); 381 382 packet.setFlags(DbgConstants.DBGF_WAITACK); 383 384 DbgPacket.FrameRawdata mod_name = new DbgPacket.FrameRawdata(packet, targetFile); 385 packet.addFrame(mod_name); 386 DbgPacket.FrameRawdata condition = new DbgPacket.FrameRawdata(packet, b.getCondition()); 387 packet.addFrame(condition); 388 389 packet.addFrame(new DbgPacket.FrameBps(0, line.getLineNumber() + 1, 0, mod_name.getRawId(), 390 condition.getRawId(), b.isTemp(), 0, 391 (b.isEnabled() ? DbgConstants.BPS_ENABLED : DbgConstants.BPS_DISABLED))); 392 393 try { 394 packet.send(socket.getOutputStream()); 395 packet = DbgPacket.receive(in); 397 DbgPacket.FrameBpl f = (DbgPacket.FrameBpl)packet.getFirstFrame(); 398 b.setBpNo(f.getBpNo()); 399 } 400 catch (IOException ioe) { 401 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 402 } 403 } 404 } 405 } 406 } 407 408 public void updateSourceTree() { 409 DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST); 410 411 packet.setFlags(DbgConstants.DBGF_WAITACK); 412 413 DbgPacket.FrameSrcTree f = new DbgPacket.FrameSrcTree(); 414 packet.addFrame(f); 415 416 try { 417 packet.send(socket.getOutputStream()); 418 packet = DbgPacket.receive(in); 420 421 packet.handle(this); 422 } 423 catch (IOException ioe) { 424 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 425 } 426 } 427 428 public void resume() { 429 callStack = null; 430 getServer().getCallStackModel().setNeedsRefresh(); 431 getServer().getVariablesModel().clearModel(); 432 433 cont(DbgConstants.DBGA_CONTINUE); 434 } 435 436 public void stepInto() { 437 cont(DbgConstants.DBGA_STEPINTO); 438 } 439 440 public void stepOut() { 441 cont(DbgConstants.DBGA_STEPOUT); 442 } 443 444 public void stepOver() { 445 cont(DbgConstants.DBGA_STEPOVER); 446 } 447 448 private void cont(int step) { 449 currentLine = null; 450 hideCurrentLine(); 451 452 System.err.println("mw DbgServerHandler.cont(" + step + ")"); 453 454 sendPacket(new DbgPacket(step)); 455 setSuspended(false); 456 } 457 458 private void handleIncomingPackets(boolean block) throws IOException { 459 while (block || in.available() > 0) { 460 DbgPacket packet = DbgPacket.receive(in); 461 462 block = false; 463 packet.handle(this); 464 } 465 } 466 467 public DebuggerAnnotation getCurrentLineAnnotation() { 468 return currentLineAnnotation; 469 } 470 471 public void setCurrentLineAnnotation(DebuggerAnnotation annotation) { 472 this.currentLineAnnotation = annotation; 473 } 474 597 } 598 | Popular Tags |