debug node

 The worst has happened. Your Node.js app is stuck in a good old-fashioned infinite loop. Express is listening for connections, but never responds. top shows 100% CPU.

You chose Node.js for its single-threaded performance and most days you’re very happy about that. This is not one of those days. Since there is just one thread of execution, all requests reaching this particular process are stuck.

Sure, you’re running one process per core and even on multiple servers. But if a common operation is the cause, all of the processes encounter it before long, and the application is effectively down. You are not having a good day.

No problem, you say as you sync your database down to a dev environment and try to reproduce the issue. Here in dev, you can console.log your way through it if you have to, following a “divide and conquer” strategy.

Except… you can’t. Something is happening in production that you have never thought of, and you don’t know what it is. So you can’t reproduce it. You need to debug this in production. 🙀

You could redeploy your app with lots of additional logging, and that’s not a bad thing. But what if you could find out exactly what code is running right now? Then you could see the problem immediately and save a great deal of downtime.

There’s a way: the Node.js command line debugger.

First, ssh into the server or open a shell in your container, if that’s how you roll. Then run top and find your runaway process. It’ll sort right to the top of the list:

runaway process
Process 4702, using close to 100% of CPU.

Gee, think maybe that’s the one?

Now, let’s tell the process to listen for debugging requests.

Yes, we’re using a command called kill. No, we’re not killing the process. We’re sending it a different signal.

kill -SIGUSR1 4702

Once you do this, the process is open to a debugger connection. In fact, it will print a special URL to its console log, and you can open that URL in Chrome to debug the process! But, maybe you don’t want to drill a hole through a firewall and a container configuration just to make that connection. Yeah, me neither.

So let’s debug at the command line instead:

node inspect -p 4702

You’ll see this prompt:

debug>

Holy smokes, you’re in the debugger! Let’s print a stack trace to see where you are in the code:

backtrace

But you just get back:

null

Hmm, that’s not very useful. Let’s type help and see if there’s anything about how to pause the process… nope!

Fortunately, I was able to find the command eventually. Sure enough, you just type:

pause

And you get back:

break in file:///somewhere/something.js:555
>555 for (prop in inputObject) {

Yes! We have our first hint. The app was executing line 555 in file something.js.

That might be enough to see the bug right away. But usually we need more information than that. You can type backtrace to get a complete stack trace:

#0 someFunctionName file:///somewhere/somefile.js:444:22

… And so on.

By now you often have enough information to solve the problem. But if not, you can set breakpoints and continue execution to see if they are indeed hit over and over:

setBreakpoint('file:///somewhere/somefile.js', 444)
cont

Sure enough, you come right back to that line of code when cont continues execution. Hit cont a few times and it becomes clear you’re looping there. You can set breakpoints on other lines to figure out if it’s stuck in this inner loop here, or that outer one there, and so on.

This is helpful, but you still don’t know why you’re stuck in the loop. So let’s watch a variable to find out its value the next time you reach a breakpoint:

watch('counter')

And when you reach the breakpoint again, you see:

break in file:///somewhere/something.js:444

Gotcha! The counter variable is NaN (“not a number”), so it can’t be incremented and will never reach the end condition for a for loop or while loop.

Now you have enough information to go back to your dev environment, fix the bug, test a fix (forcing the NaN case as a test if you have to!) and ship it to production.

To close the debugger, just press control-D to exit.

nginx loadbalancer tcp/udp syslog part 1

 http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_bind

https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/#proxy_pass


Directives

Syntax:proxy_bind address [transparent] | off;
Default:—
Context:stream, server

This directive appeared in version 1.9.2.

Makes outgoing connections to a proxied server originate from the specified local IP address. Parameter value can contain variables (1.11.2). The special value off cancels the effect of the proxy_bind directive inherited from the previous configuration level, which allows the system to auto-assign the local IP address.

The transparent parameter (1.11.0) allows outgoing connections to a proxied server originate from a non-local IP address, for example, from a real IP address of a client:

proxy_bind $remote_addr transparent;

In order for this parameter to work, it is usually necessary to run nginx worker processes with the superuser privileges. On Linux it is not required (1.13.8) as if the transparent parameter is specified, worker processes inherit the CAP_NET_RAW capability from the master process. It is also necessary to configure kernel routing table to intercept network traffic from the proxied server.




tar and netcat

 

stdin and stdout with tar

While archiving, we can specify stdout as the output file so another command in a pipe can read it as stdin and process the archive.

This technique will transfer data through a Secure Shell (SSH) connection, for example:

    $ tar cvf - files/ | ssh user@example.com "tar xv -C Documents/"

In the preceding example, the files/directory is added to a tar archive which is output to stdout (denoted by -) and extracted to the Documents folder on the remote system.

Receiving server(open port 44023)

root@Receiver:/StoragePool/data/logs/Firewalls/2020/11# nc -l 44023 | pv | tar x

2.27GiB 0:01:16 [46.6MiB/s] [                                                                                                                                         <=>                   ]




Sending server (send dir named 12 to 22)

root@Sender:/opt/immune/storage/logs/Firewalls/2020/11# ls

01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22

root@Sender:/opt/immune/storage/logs/Firewalls/2020/11# tar cf - 13  14  15  16  17  18  19  20  21 | nc 172.16.104.39 44023 

The sender server should run the following command


$ tar cf - logs/ | netcat 192.168.0.1 9000

and the receiveing server should listen with with this command

$ netcat -l -p 9000 | tar x

zfs send receive using netcat

zfs sender part

zfs send tank/test@snap100 | nc -w 20 10.0.1.2 8023

This means that the dataset “test” from my ZFS pool “tank” and snapshot “manual-2020-08-27_19-18” are being sent via netcat to host 10.0.1.2 on port 8023.


zfs receive side

nc -l 8023 -w 120 | zfs receive tank/test

This means that netcat is listening on port 8023 for the dataset “test” and adding it to the ZFS pool “tank”.


to see progress use pv eg.

nc -l 8023 -w 120 | pv | zfs receive tank/test


Send Incremenatl Snapshots

root@141:# zfs list -t snapshot

NAME                  USED  AVAIL  REFER  MOUNTPOINT

LP_Pool/storage@100  2.16M      -  1.38G  -             ## this is already sent 

LP_Pool/storage@101   136K      -  1.89G  -             # new snap 1

LP_Pool/storage@102      0      -  2.91G  -             # new snap 2


root@141:# zfs send -I LP_Pool/storage@100 LP_Pool/storage@102 | nc -w 120 10.45.9.148 44022


It will send @100 @101 and @102.

the recv command will be same

Speed will depend on disk on sendeer side, Eg. below stats show that disk performance is saturated.

iostat -xz 1


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          49.70    0.00    8.48    0.00    0.00   41.82


Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

sda               0.00     0.00   13.59    0.00    73.79     0.00    10.86     2.71  196.29  196.29    0.00  73.71 100.19

sdb               0.00     0.00   14.56    0.00   166.99     0.00    22.93     2.58  177.33  177.33    0.00  68.80 100.19


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          48.51    0.00   10.45    2.99    0.00   38.06


Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

sda               0.00     0.00   15.00    0.00   124.00     0.00    16.53     2.52  181.33  181.33    0.00  65.33  98.00

sdb               0.00     0.00   15.00    0.00   120.00     0.00    16.00     2.66  189.87  189.87    0.00  65.33  98.00


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          38.69    0.00   13.69    0.00    0.00   47.62


Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

sda               0.00     0.00   25.00    0.00   188.00     0.00    15.04     2.57  105.12  105.12    0.00  36.96  92.40

sdb               0.00     0.00   26.00    0.00   192.00     0.00    14.77     2.85  107.38  107.38    0.00  39.38 102.40

dstat

 dstat -- 

output

...network recv=123B | send 297k ==> this means 

download 123 Bytes , upload or sent is 297 KiloBYTES

kdump on ubuntu 20.04 part 2

 Debugging the Panic with crash

Time to figure out what went wrong and to find where in the source code the problem occurs.

To be able to start crash, we need a kernel with debugging symbols built in. Now, this kernel has to exactly match the production kernel that we crashed, and cannot be compressed. For Ubuntu systems, we are looking for the kernel ddeb.


We can add the ddeb repository to our system with:

$ sudo tee /etc/apt/sources.list.d/ddebs.list << EOF

deb http://ddebs.ubuntu.com/  $(lsb_release -cs) main restricted universe multiverse

deb http://ddebs.ubuntu.com/  $(lsb_release -cs)-security main restricted universe multiverse

deb http://ddebs.ubuntu.com/  $(lsb_release -cs)-updates  main restricted universe multiverse

deb http://ddebs.ubuntu.com/  $(lsb_release -cs)-proposed main restricted universe multiverse

EOF

if you get below error

Reading package lists... Done

E: The repository 'http://ddebs.ubuntu.com xenial-security Release' does not have a Release file.

N: Updating from such a repository can't be done securely, and is therefore disabled by default.

N: See apt-secure(8) manpage for repository creation and user configuration details.


add [trusted=yes] for example


/# cat /etc/apt/sources.list.d/ddebs.list 

deb [trusted=yes] http://ddebs.ubuntu.com/ xenial          main restricted universe multiverse

deb [trusted=yes] http://ddebs.ubuntu.com/ xenial-security main restricted universe multiverse

deb [trusted=yes] http://ddebs.ubuntu.com/ xenial-updates  main restricted universe multiverse

deb [trusted=yes] http://ddebs.ubuntu.com/ xenial-proposed main restricted universe multiverse

We can then import the GPG key for the repo, refresh package lists and install the vmlinux package with:-

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C8CAB6595FDFF622

$ sudo apt update

$ sudo apt install linux-image-$(uname -r)-dbgsym

Starting crash ....................

# crash <vmlinux>  <dumpfile>

# crash /usr/lib/debug/boot/vmlinux-5.4.0-42-generic /var/crash/202008290918/dump.202008290918 


crash 7.2.8

Copyright (C) 2002-2020  Red Hat, Inc.

Copyright (C) 2004, 2005, 2006, 2010  IBM Corporation

Copyright (C) 1999-2006  Hewlett-Packard Co

Copyright (C) 2005, 2006, 2011, 2012  Fujitsu Limited

Copyright (C) 2006, 2007  VA Linux Systems Japan K.K.

Copyright (C) 2005, 2011  NEC Corporation

Copyright (C) 1999, 2002, 2007  Silicon Graphics, Inc.

Copyright (C) 1999, 2000, 2001, 2002  Mission Critical Linux, Inc.

This program is free software, covered by the GNU General Public License,

and you are welcome to change it and/or distribute copies of it under

certain conditions.  Enter "help copying" to see the conditions.

This program has absolutely no warranty.  Enter "help warranty" for details.

 

GNU gdb (GDB) 7.6

Copyright (C) 2013 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-unknown-linux-gnu"...


WARNING: kernel relocated [556MB]: patching 114875 gdb minimal_symbol values

      KERNEL: /usr/lib/debug/boot/vmlinux-5.4.0-42-generic             

    DUMPFILE: /var/crash/202008290918/dump.202008290918  [PARTIAL DUMP]

        CPUS: 4

        DATE: Sat Aug 29 09:16:55 2020

      UPTIME: 00:04:40

LOAD AVERAGE: 1.97, 1.79, 0.80

       TASKS: 504

    NODENAME: user1-HP-Pavilion-dv4-Notebook-PC

     RELEASE: 5.4.0-42-generic

     VERSION: #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020

     MACHINE: x86_64  (2494 Mhz)

      MEMORY: 7.9 GB

       PANIC: "Kernel panic - not syncing: sysrq triggered crash"

         PID: 4488

     COMMAND: "bash"

        TASK: ffff89dec9895d00  [THREAD_INFO: ffff89dec9895d00]

         CPU: 3

       STATE: TASK_RUNNING (PANIC)


crash> log

[    0.000000] microcode: microcode updated early to revision 0x21, date = 2019-02-13

[    0.000000] Linux version 5.4.0-42-generic (buildd@lgw01-amd64-038) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 (Ubuntu 5.4.0-42.46-generic 5.4

.44)

[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-42-generic root=UUID=eb00fc0a-fab7-42a7-a272-7ba7b07dd94d ro quiet splash crashkernel=512M-:192M vt.handoff=7

[    0.000000] KERNEL supported cpus:

[    0.000000]   Intel GenuineIntel

[    0.000000]   AMD AuthenticAMD

[    0.000000]   Hygon HygonGenuine

[    0.000000]   Centaur CentaurHauls

[    0.000000]   zhaoxin   Shanghai  

[    0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'

[    0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'

[    0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'

[    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256

[    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.

[    0.000000] BIOS-provided physical RAM map:

[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x0000000000087fff] usable

[    0.000000] BIOS-e820: [mem 0x0000000000088000-0x00000000000bffff] reserved

[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000001fffffff] usable

[    0.000000] BIOS-e820: [mem 0x0000000020000000-0x00000000201fffff] reserved

[    0.000000] BIOS-e820: [mem 0x0000000020200000-0x0000000040003fff] usable

[    0.000000] BIOS-e820: [mem 0x0000000040004000-0x0000000040004fff] reserved

[    0.000000] BIOS-e820: [mem 0x0000000040005000-0x00000000aa26efff] usable

Note that the crashed process is shown with ‘>’.

The " log " command brings up the contents of dmesg, for that particular session.

[  132.749323] audit: type=1400 audit(1598706924.030:74): apparmor="DENIED" operation="open" profile="snap.snap-store.ubuntu-software" name="/var/lib/snapd/hostfs/usr/share/icons/Yaru/8x8/emblems/" pid=3997 comm="pool-org.gnome." requested_mask="r" denied_mask="r" fsuid=1000 ouid=0

[  132.751313] audit: type=1400 audit(1598706924.030:75): apparmor="DENIED" operation="open" profile="snap.snap-store.ubuntu-software" name="/var/lib/snapd/hostfs/usr/share/icons/Yaru/icon-theme.cache" pid=3997 comm="pool-org.gnome." requested_mask="r" denied_mask="r" fsuid=1000 ouid=0

[  223.805844] sysrq: Trigger a crash

[  223.805853] Kernel panic - not syncing: sysrq triggered crash

[  223.805861] CPU: 3 PID: 4488 Comm: bash Kdump: loaded Tainted: G           OE     5.4.0-42-generic #46-Ubuntu

[  223.805865] Hardware name: Hewlett-Packard HP Pavilion dv4 Notebook PC/1836, BIOS F.22 09/05/2012

[  223.805868] Call Trace:

[  223.805885]  dump_stack+0x6d/0x9a

[  223.805895]  panic+0x101/0x2e3

[  223.805903]  sysrq_handle_crash+0x15/0x20

[  223.805908]  __handle_sysrq.cold+0x48/0x107

[  223.805914]  write_sysrq_trigger+0x28/0x40

[  223.805920]  proc_reg_write+0x43/0x70

[  223.805926]  __vfs_write+0x1b/0x40

[  223.805931]  vfs_write+0xb9/0x1a0

[  223.805937]  ksys_write+0x67/0xe0

[  223.805942]  __x64_sys_write+0x1a/0x20

[  223.805950]  do_syscall_64+0x57/0x190

[  223.805958]  entry_SYSCALL_64_after_hwframe+0x44/0xa9

[  223.805965] RIP: 0033:0x7f1d2cb70057

[  223.805971] Code: 64 89 02 48 c7 c0 ff ff ff ff eb bb 0f 1f 80 00 00 00 00 f3 0f 1e fa 64 8b 04 25 18 00 00 00 85 c0 75 10 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 51 c3 48 83 ec 28 48 89 54 24 18 48 89 74 24

[  223.805976] RSP: 002b:00007ffc65c9e978 EFLAGS: 00000246 ORIG_RAX: 0000000000000001

[  223.805982] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 00007f1d2cb70057

[  223.805986] RDX: 0000000000000002 RSI: 0000558c871b6900 RDI: 0000000000000001

[  223.805989] RBP: 0000558c871b6900 R08: 000000000000000a R09: 0000000000000001

[  223.805993] R10: 0000558c851b2017 R11: 0000000000000246 R12: 0000000000000002

[  223.805997] R13: 00007f1d2cc4b6a0 R14: 00007f1d2cc4c4a0 R15: 00007f1d2cc4b8a0


for more complex bugs, the bt command is interesting.

bt normally shows the backtrace of the task which caused the system crash, but you can select other tasks and fetch their backtraces as well.

The output for bt is:

crash> bt

PID: 4488   TASK: ffff89dec9895d00  CPU: 3   COMMAND: "bash"

 #0 [ffffaaed81ee7c68] machine_kexec at ffffffffa3c700e3

 #1 [ffffaaed81ee7cc8] __crash_kexec at ffffffffa3d58cd2

 #2 [ffffaaed81ee7d98] panic at ffffffffa3ca1962

 #3 [ffffaaed81ee7e18] sysrq_handle_crash at ffffffffa4290d15

 #4 [ffffaaed81ee7e28] __handle_sysrq.cold at ffffffffa42916a8

 #5 [ffffaaed81ee7e60] write_sysrq_trigger at ffffffffa4291548

 #6 [ffffaaed81ee7e78] proc_reg_write at ffffffffa3f715a3

 #7 [ffffaaed81ee7e98] __vfs_write at ffffffffa3edab1b

 #8 [ffffaaed81ee7ea8] vfs_write at ffffffffa3edb829

 #9 [ffffaaed81ee7ee0] ksys_write at ffffffffa3edddb7

#10 [ffffaaed81ee7f20] __x64_sys_write at ffffffffa3edde4a

#11 [ffffaaed81ee7f30] do_syscall_64 at ffffffffa3c044c7

#12 [ffffaaed81ee7f50] entry_SYSCALL_64_after_hwframe at ffffffffa480008c

    RIP: 00007f1d2cb70057  RSP: 00007ffc65c9e978  RFLAGS: 00000246

    RAX: ffffffffffffffda  RBX: 0000000000000002  RCX: 00007f1d2cb70057

    RDX: 0000000000000002  RSI: 0000558c871b6900  RDI: 0000000000000001

    RBP: 0000558c871b6900   R8: 000000000000000a   R9: 0000000000000001

    R10: 0000558c851b2017  R11: 0000000000000246  R12: 0000000000000002

    R13: 00007f1d2cc4b6a0  R14: 00007f1d2cc4c4a0  R15: 00007f1d2cc4b8a0

    ORIG_RAX: 0000000000000001  CS: 0033  SS: 002b

crash>