It can be used to debug code running locally in Firefox or to debug code running remotely, for example in a Firefox OS device or Firefox on Android. This guide assumes you're debugging locally, but it's mostly applicable to remote debugging as well. See the guide to remote debugging for the differences.
To open the Debugger select "Debugger" from the Web Developer submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on Mac OS X), or by pressing its Control-Shift-S (Command-Option-S on the Mac) keyboard shortcut.
The Toolbox will appear at the bottom of the browser window, with the Debugger activated. Here's what it looks like when you first open it:
And here's what it looks like in the middle of a debugging session:
In this guide we'll first have a quick tour of the debugger's user interface, then we'll describe how to perform some common debugging tasks.
The debugger user interface
The debugger's UI is split into four main sections, which we'll cover one at a time:
- the toolbar
- the source list pane
- the source pane
- the variables pane
The left-hand side of the debugger lists all the JS source files loaded into the page, and enables you to select one to debug.
Source files are grouped under different headings according to where they are loaded from. For example, this is part of the source list pane when a page on developer.mozilla.org is loaded:
This corresponds to the following
script tags in the page:
You can select any of these files and they will be loaded into the Source pane.
Any breakpoints you've set in a source file are listed under the filenameThe check box enables you to enable/disable the breakpoint. Right-clicking on the breakpoint's entry in the list shows a context menu enabling you to:
- enable, disable or remove this breakpoint, all breakpoints, or all breakpoints except this one
- make this breakpoint conditional (or edit its conditions, if it is already conditional)
In the source pane, the context menu enables you to:
- set a breakpoint
- set a conditional breakpoint
- add a watch expression for the selection
- search or filter using the script filter
The toolbar consists of four sections:
- a row of buttons to control movement through a script
- a visualisation of the call stack
- the script filter
- buttons to expand/collapse the variables pane and to control debugger settings
The four buttons on the left perform the following functions:
- Pause/Resume (F6): pauses or resumes execution of the script you're debugging. When it's blue and "pressed", as above, that means the script is paused, either because you've paused it with this button or because you've hit a breakpoint.
- Step Out (Shift-F8): runs the script until the current function exits.
The call stack visualisation shows the call stack at the point execution is paused.
The script filter enables you to search in all three of the debugger's panes. By prefixing the filter expression with one of several special characters, the filter provides various different functions.
|None||Filter the scripts shown in the source list pane.|
|!||Search for the string across all files.|
|@||Search for function definitions, across all files, containing the string.|
|#||Search for the string in the file currently open in the source pane.|
|:||Go to the line given in the file currently open in the source pane.|
Filter the variables shown in the variables pane.
These options are shown in a pop-up when you click in the filter, and they're also accessible from the context menu in the source pane.
At the right-hand end of the toolbar are two more buttons. The first of these shows and hides the variables pane, and the second enables you to toggle various debugger settings:
- Show panes on startup: when this option is enabled, the debugger's variables pane is visible when you first start the debugger.
- Show variables filter box: enabling this option adds a "Filter variables" search box to the variables panel, so that you can filter the displayed list of variables.
The variables pane is where you can examine, and modify, the internal state of the script as it's executing:
Variables are grouped by scope: in Function scope you'll see the built-in
this variables as well as local variables defined by the function like
greeting. Similarly, in Global scope you'll see global variables you've defined, like
greetme, as well as built-in globals like
Each object can be expanded using a disclosure triangle to show its members.
Pointing your cursor at a variable's name displays a tooltip that provides additional information about the variable; for example, pointing at the
greeting object displays "
configurable enumerable writable". See
Object.defineProperty() for details on what these property descriptors mean.
You can change a variable's value by clicking on its current value and entering a new one; for example, if you click on
"Hi, Dr. Nick!" next to
greeting, you can edit the value.
Then start running your code. The watch expression does nothing until you begin to step through your code, so nothing happens until you reach a breakpoint. At that point, a box showing your active watch expressions and their current values will appear:
You can step through your code, watching the value of the expression as it changes; each time it does, the box will flash briefly yellow. You can remove a watch expression by clicking the "x" icon next to it, and, of course, you can have more than one watch expression at a time.
How do I...?
Open the debugger
To open the debugger, select "Debugger" from the Web Developer submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on Mac OS X), or press Control-Shift-S (Command-Option-S on the Mac).
Find a source file
Find inside a file
Set a breakpoint
To set a breakpoint in a file open in the source pane:
- either click on the line number for the line you want to break at
- or activate the context menu while on the line you want to break at, and select "Add breakpoint"
Each breakpoint is shown in three places in the debugger:
- they're listed in the source list pane underneath the file's name
- the line in the source pane is marked with a blue circle
- the gray vertical bar at the right of the source pane shows each breakpoint in the file as a small blue bar
The screenshot below shows breakpoints at lines 7 and 65 of the file:
Set a conditional breakpoint
To set a conditional breakpoint, activate the context menu while on the line you want to break at, and select "Add conditional breakpoint". Then enter the conditional expression in the popup that appears:
To edit the condition, or to add a condition to a normal breakpoint, activate the context menu and select "Configure conditional breakpoint":
Disable a breakpoint
To disable a breakpoint:
- either: uncheck the check box next to the breakpoint's entry in the source list pane
- or: activate the context menu while your mouse pointer is over the breakpoint's entry in the source list pane, and select "Disable breakpoint"
Step through my code
When your code stops at a breakpoint, you can step through it using the four buttons on the left of the toolbar:
In order, the buttons are:
- Play: run to the next breakpoint
- Step over: advance to the next line in the same function.
- Step into: advance to the next line in the function, unless on a function call, in which case enter the function being called
- Step out: run to the end of the current function
Use a source map
When the code has stopped at a breakpoint, you can examine its state in the variables pane of the debugger:
Variables in global scope and in function, block, "with" scopes, etc. are listed separately, and you can expand objects to see their properties. You can also filter the variables shown using the "*" prefix in the script filter:
When the code has stopped at a breakpoint, you can modify variables in the variables pane of the debugger. Just click on the variable's current value and you'll be able to type there:
Watch an expression
Debug mobile devices
To learn how to debug mobile devices, see the guide to remote debugging.
Access debugging in add-ons
The following items are accessible in the context of chrome://browser/content/debugger.xul (or, in version 23 beta, chrome://browser/content/devtools/debugger.xul):
- window.addEventListener("Debugger:EditorLoaded") - called when the read-only script panel loaded.
Unfortunately there is not yet any API to evaluate watches/expressions within the debugged scope, or highlight elements on the page that are referenced as variables in the debugged scope. (currently a work in progress, see bug 653545.)