20
1/20 Outline Introduction Debugging Common Acronymes Debugging Debugging with NetBeans IDE Andreas Ruppen [email protected] University of Fribourg Department of Informatics Software Engineering Group April 18, 2014

Debugging with NetBeans IDE

  • View
    606

  • Download
    2

Embed Size (px)

DESCRIPTION

Short introduction to Debugging in general and small insights on how to use NetBeans interactive debugger with Java Code.

Citation preview

Page 1: Debugging with NetBeans IDE

1/20

Outline Introduction Debugging Common Acronymes

DebuggingDebugging with NetBeans IDE

Andreas [email protected]

University of FribourgDepartment of Informatics

Software Engineering Group

April 18, 2014

Page 2: Debugging with NetBeans IDE

2/20

Outline Introduction Debugging Common Acronymes

1 Introduction

2 Debugging

3 Common Acronymes

Page 3: Debugging with NetBeans IDE

3/20

Outline Introduction Debugging Common Acronymes

Generalities I

Definition (Debugging)

Debugging is the process of identifying and eliminating bugs ina software system. Debugging can be a quite complex processespecially when systems are distributed, deployed or tightlycoupled.

GeneralitiesIn general debugging is done with the aid of some software.This is what we call interactive debugging. This software cancontrol the program flow, watch log files or memory dumps andmuch more.

Page 4: Debugging with NetBeans IDE

4/20

Outline Introduction Debugging Common Acronymes

Generalities II

Origin (Wikipedia)The terms "bug" and "debugging" are both popularly attributedto Admiral Grace Hopper in the 1940s. While she was workingon a Mark II Computer at Harvard University, her associatesdiscovered a moth stuck in a relay and thereby impedingoperation, whereupon she remarked that they were "debugging"the system. However the term "bug" in the meaning of technicalerror dates back at least to 1878 and Thomas Edison.

Page 5: Debugging with NetBeans IDE

5/20

Outline Introduction Debugging Common Acronymes

Generalities III

Figure : First Bug (from wikipedia.org)

Page 6: Debugging with NetBeans IDE

6/20

Outline Introduction Debugging Common Acronymes

Difficulties I

The debugging skills of the programmer are a major factorwhen it comes to eliminating bugs.Software tends to become more and more complex whichmakes debugging harder.Mobile devices also introduces new challenges fordebugging. The code is run and developed on differentmachines.High-level languages such as Java are easier to debugthan low level languages like C.Bugs may occur through memory corruption which isdifficult to track. This comes from the fact that the thrownerror is not necessary the origin of the problem.

Page 7: Debugging with NetBeans IDE

7/20

Outline Introduction Debugging Common Acronymes

Types of Debugging

Interactive Debugging Software

Debugging software monitors the flow of the program. It canalso modify this flow, inspect and change attribute values,pausing the execution at predefined locations, influence theoutcome of branches and switch statements etc. . .

Static Debugging Software

Instead of running the software and closely observe itsexecution, some tools look statically at the code to identifypotential problems. These static code analysis tools comehandy to identify problems which are hard to reproduce likememory leaks. It is most useful to pinpoint erroneous pointers,infinite recursive structures.

Page 8: Debugging with NetBeans IDE

8/20

Outline Introduction Debugging Common Acronymes

Debugging techniques Ifor interactive debugging

A first step in debugging is to reproduce the problem. Thissounds trivial but can become quite complex in the case ofmemory leaks, distributed systems, multithreaded systemsetc. . . A good bug report always includes the precise stepsto reproduce the problem.Once the problem reproduced, to make debugging easier,the input is simplified (the bug could come from a too largeinput file for example, if the smaller file passes the problemis found). These simplifications are the most time mademanually (divide-and-conquer). Smaller input also allowsfaster debugging (think of a large for-loop iterating overeach line of a file).

Page 9: Debugging with NetBeans IDE

9/20

Outline Introduction Debugging Common Acronymes

Debugging techniques IIfor interactive debugging

From this point on, a developer has several choices toprecisely identify the problem:

He can use a debugging tool (like gdb or one integrated inthe IDE) to examine the memory and the local variables atsome interesting points of the program.Another method is to use tracing (think about last weekslogging).Post-mortem debugging, which consists of memory dumpanalysisRemote debugging for deployed applications.

Page 10: Debugging with NetBeans IDE

10/20

Outline Introduction Debugging Common Acronymes

Debugging in NetBeans IDEUse Case

What is debugging?Run through the code with the interpreter.Allows to see whether the execution path is as expected.

Using the DebuggerWe can use the debugger:

to verify if a programm behaves as we except.to identify the nature of a runtime-error.to force the programm entering a given state.

Page 11: Debugging with NetBeans IDE

11/20

Outline Introduction Debugging Common Acronymes

Debugging in NetBeans IDEHowto

Fist stepsOpen a project in NetBeans IDE.Define the Breakpoints at some interesting points.Start the Debugger

Debugging modeRuns the code with the Java virtual machine.Everything that is possible in normal mode, should also bepossible in debugging mode.Execution stops for user action at the defined breakpoints.

Page 12: Debugging with NetBeans IDE

12/20

Outline Introduction Debugging Common Acronymes

Breakpoints IDefining Breakpoints

Definition (Breakpoint)A Breakpoint is an indication for the Java Debugger. They areignored by the Java virtual machine when running in normalmode. However when running in debug mode, the executionstops at every breakpoint and waits for a user action before itcontinues to the next break points.

Page 13: Debugging with NetBeans IDE

13/20

Outline Introduction Debugging Common Acronymes

Breakpoints IIDefining Breakpoints

ActionsWhen the execution of the programm stops at a breakpointfollowing actions are possible:

Inspection or modification of dynamic variables.Step-by-step execution.Go to the next breakpoint (or to the end of the application ifthere are no more breakpoints).

Page 14: Debugging with NetBeans IDE

14/20

Outline Introduction Debugging Common Acronymes

Breakpoints in NetBeans IDEDefining breakpoints

Example (Usage)

Define a Breakpoint byclicking on thecorresponding line.The line number isreplaced by a redsquare.It’s not possible to definebreakpoints on nonexecutable lines (i.e.commentary lines)

Page 15: Debugging with NetBeans IDE

15/20

Outline Introduction Debugging Common Acronymes

Running the DebuggerThe ANT way

Debug Target

The ANT file which comes with the exercises provides adebug target.This target can be launched manually (by right clicking onthe build.xml file in NetBeans IDE).It is possible to launch the debugger from command linewith the following arguments

1 −Xrunjdwp : t r a n s p o r t =dt_socket , server=y , suspend=n , address=5432

which makes it listening on port 5432 for a debugging toollike the one integrated in NetBeans IDE.

Page 16: Debugging with NetBeans IDE

16/20

Outline Introduction Debugging Common Acronymes

Running the DebuggerThe NetBeans IDE way

Debugging with and IDE

It is possible (and even recommended) to do thedebugging tasks directly from the IDE.NetBeans IDE offers a great debugging support.To launch the debugger, either click the debug icon in thelaunch bar (the 6th one), or select the debug target fromthe ANT script or select debug from the right click on theproject.Once started the program starts as normal but theexecution stops at the first breakpoint.

Page 17: Debugging with NetBeans IDE

17/20

Outline Introduction Debugging Common Acronymes

The debugging console

NetBeans IDE layoutWhen the debugger isstarted in NetBeans IDEa new tab becomesvisible.This tab is updated oneach step of thedebugger.It shows the value of all fields of the current class.Besides inspecting the value of each field, it is alsopossible to change them.

Page 18: Debugging with NetBeans IDE

18/20

Outline Introduction Debugging Common Acronymes

Using the NetBeans IDE Debugger

Associated GUI ElementsWhen the debugger stopson a line, this line ishighlighted green.All other debugging GUI elements relate to this line or theclass containing this line.

Page 19: Debugging with NetBeans IDE

19/20

Outline Introduction Debugging Common Acronymes

Using the NetBeans IDE Debugger

Associated GUI ElementsWhen everyhting seems ok to continue, NetBeans IDE offersseveral buttons for going on:

Stop (the debugger)PauseContinue (to nextBreakpoint)Step OverStep over Expression

Step intoStep outRun to cursorApply Code ChangeTake GUI Snapshot

Page 20: Debugging with NetBeans IDE

20/20

Outline Introduction Debugging Common Acronymes

Used acronyms

IDE Integrated Development Environmentgdb GNU Debugger

GNU GNU is not Unix