Skip to content

Commit

Permalink
Updating text
Browse files Browse the repository at this point in the history
  • Loading branch information
donovanh committed Jun 10, 2021
1 parent 0ae30a1 commit 4afbe00
Showing 1 changed file with 11 additions and 49 deletions.
60 changes: 11 additions & 49 deletions diagnostics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@ author: donovan
date: 1903-10-09
---

Goals of this lesson:
In this lesson we will take a look at ways we can diagnose issues in our code. To do this we'll use the built-in Node.js debugger, as well as the Chrome dev tools.

* Learn how to set up Node Debugger
* Step through breakpoints using Repl
* Inspecting in the Chrome debugger
* Explore more powerful debugging options
* Generate diagnostic deports to catch errors
* Finding memory leaks

Pre-requisites:

INTRO and expected outcomes
After this lesson you should be confident investigating errors in a methodical fashion, without relying on `console.log` so much.

## Node.js Debugger

Expand Down Expand Up @@ -58,7 +49,7 @@ Wait, I counted too far!

Something has gone wrong! Let's use the debugger tool to find out what.

### debugger;
### The `debugger` command

In the above code you might have noticed the `debugger;` code on line 3. During normal operation this is ignored, but we can make use of this by running `inspect`:

Expand All @@ -70,7 +61,7 @@ This will enter `debug` mode where we can use command to inspect what is going o

Firstly we can use `list()` to take a look at the place in the code we are currently exectuting. At first it'll be on line 16 `startACount()`. All good so far. We can then continue using `cont` to continue exectuting until we reach the break point.

By entering the `cont` command we should now see:
By entering the continue (`cont`) command we should now see:

```
1 function count() {
Expand Down Expand Up @@ -113,17 +104,6 @@ So we've managed to get to the moment when the script fails. Enter the `repl` co

You can quit the debugger by typing `.exit` or by pressing `Cmd+C` twice.

### Watchers



### Homework

Feel free to try moving the `debugger;` statement around in the above. You can also try using the `help` command in the `debug` mode to see other options.

Try breaking the code and then using the `debug` mode to narrow down the issue. Inspecting variables using the REPL can be helpful to find logic errors or catch situations where variables contain unexpected values.


## Debugging using Node.js with Chrome DevTools

We don't just need the command line to debug our code. We can also make use of Chrome's debugging tools to help navigate our code.
Expand Down Expand Up @@ -204,36 +184,18 @@ To create the report, we can run the following command against out `count.js` fi
node --report-uncaught-exception count.js
```

This generates [a report in JSON](/diagnostics/report.20210505.160745.11097.0.001.json) that you can read to try to work out what has happened.

This generates a report in JSON that you can read to try to work out what has happened.

It includes a lots of information on the JavaScript and native stack traces, heap statistics, platform information, resource usage and local environmental variables.

Along with debugging the error messages from the script, this can be helpful in determining the state of the application when an uncaught exception occurred.

With the report option enabled, diagnostic reports can be triggered on unhandled exceptions, fatal errors and user signals, in addition to triggering programmatically through API calls. Read more on the official [diagnostic report docs page](https://nodejs.org/api/report.html).

### Homework

## Async hooks

- A lot of services track everything going on in Node by monkey patching anything that is asynchronous, to keep track of processes over time
- Introducing AsyncHooks from https://github.com/nodejs/diagnostics

* note: don't use console.log inside async_hooks methods as it is asynchronous and will cause infinite `init` calls, instead use `process._rawDebug(msg)`


## Investigating memory leaks

// TODO: create example with a memory leak

```
Monitor values from process.memoryUsage() over time.
```

Make heap snapshots

https://www.npmjs.com/package/heapdump

Compare in chromium/chrome
Feel free to try moving the `debugger;` statement around in the above. You can also try using the `help` command in the `debug` mode to see other options.

// TODO: More on debugging memory leaks
Try breaking the code and then using the `debug` mode to narrow down the issue. Inspecting variables using the REPL can be helpful to find logic errors or catch situations where variables contain unexpected values.

## Summary
Try out the same exercise using the Chrome devtools and see how it differs. By being familiar with each you will find it easier to know what tools to use to debug any issues in future.

0 comments on commit 4afbe00

Please sign in to comment.