# How to print a human-readable backtrace with \`nvfortran\`?

**URL:** <https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843>\
**Category:** Uncategorized\
**Created:** [December 2, 2022, 2:36am UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843 "2022-12-02T02:36:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [December 2, 2022, 2:36am UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/1 "2022-12-02T02:36:55Z")

</div>

How to print a human-readable backtrace with `nvfortran` when an exception occurs?

First of all, let me acknowledge that this question is better asked on the NVIDIA Developer Forum. However, I cannot log in the forum because the verification email takes an eternity to arrive. So I try here in case someone knows the answer.

Here is a minimal example.

```auto
! test.f90                                                                                                                     
program test                                                                                                                         
implicit none                                                                                                                        
                                                                                                                                     
real :: a                                                                                                                            
                                                                                                                                     
a = tiny(a)                                                                                                                          
                                                                                                                                     
write (*, *) 1.0 / (a**2)                                                                                                                                                                                    
                                                                                                                                     
end program test                                                                                                                     

```

According to the [`nvfortran` manual](https://docs.nvidia.com/hpc-sdk/pdf/hpc2211ug.pdf), I do the following.

```auto
$ uname -a && nvfortran --version 

Linux 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

nvfortran 22.11-0 64-bit target on x86-64 Linux -tp zen2 
NVIDIA Compilers and Tools
Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

$ export NVCOMPILER_TERM=trace && nvfortran -g -Kieee -Ktrap=divz test.f90 && ./a.out 

Error: floating point exception, floating point divide by zero
   rax 0x0000000000000000, rbx 0x00007fff3100e178, rcx 0x0000000000000000
   rdx 0x0000000000000000, rsp 0x00007fff3100e030, rbp 0x00007fff3100e040
   rsi 0x00007fb93c4fbf50, rdi 0x0000000000000006, r8 0x0000000000004240
   r9 0x0000000000004100, r10 0x00007fb93be156d0, r11 0x00007fb93bee3640
   r12 0x00007fff3100e178, r13 0x0000000000401200, r14 0x0000000000403d98
   r15 0x00007fb93c573040
  /usr/lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7fb93a41a520]
  ./a.out() [0x401233]
  /usr/lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7fb93a401d90]
  /usr/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7fb93a401e40]
  ./a.out() [0x401125]

```

Am I supposed to read and understand the above-printed backtrace or did I overlook something?

Thanks.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [December 2, 2022, 3:25am UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/2 "2022-12-02T03:25:02Z")

</div>

@zaikunzhang , have you made an inquiry at the Discord site for Nvidia’s HPC compilers with suitable tag(s) for `nvfortran`?

> **[nvc, nvc++ and nvfortran](https://forums.developer.nvidia.com/c/accelerated-computing/hpc-compilers/nvc-nvc-and-nvfortran/313)**
>
> Greetings and welcome to the NVIDIA HPC Compilers forum. Post your questions and comments here pertaining to the NVIDIA C, C++ and Fortran HPC compilers, and to their interoperation with NVCC and other HPC SDK components.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [December 2, 2022, 3:27am UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/3 "2022-12-02T03:27:21Z")

</div>

> [@zaikunzhang](#):
>
> First of all, let me acknowledge that this question is better asked on the NVIDIA Developer Forum. However, I cannot log in the forum because the verification email takes an eternity to arrive. So I try here in case someone knows the answer.

Hello @FortranFan , ~~I did (want to) try, but did not succeed in logging in~~. [Done, but no response received yet](https://forums.developer.nvidia.com/t/how-to-print-a-human-readable-backtrace-with-nvfortran-when-an-exception-occurs/235987).

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [December 2, 2022, 2:05pm UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/4 "2022-12-02T14:05:27Z")

</div>

Unless you have a typo in your example nvfortran uses -traceback not -trace. Also, try -O0 -g. You best bet though is to let Ubuntu generate a core file and use gdb.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [December 2, 2022, 2:17pm UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/5 "2022-12-02T14:17:12Z")

</div>

Thanks @rwmsu .

> [@rwmsu](#):
>
> Unless you have a typo in your example nvfortran uses -traceback not -trace. Also, try -O0 -g. You best bet though is to let Ubuntu generate a core file and use gdb.

Sorry, I do not think there is a typo. I searched through the [`nvfortran` manual](https://docs.nvidia.com/hpc-sdk/pdf/hpc2211ug.pdf) but found no introduction to `-traceback`. Nevertheless, I tried it and got the following.

```auto
$ export NVCOMPILER_TERM=trace && nvfortran -g -O0 -traceback -Kieee -Ktrap=divz nv.f90 && ./a.out 
Error: floating point exception, floating point divide by zero
   rax 0x0000000000000000, rbx 0x00007ffc58a2d398, rcx 0x0000000000000000
   rdx 0x0000000000000000, rsp 0x00007ffc58a2d250, rbp 0x00007ffc58a2d260
   rsi 0x00007fc0194fbf50, rdi 0x0000000000000006, r8 0x0000000000004240
   r9 0x0000000000004100, r10 0x00007fc018e156d0, r11 0x00007fc018ee3640
   r12 0x00007ffc58a2d398, r13 0x0000000000401200, r14 0x0000000000403d98
   r15 0x00007fc019662040
  /usr/lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7fc01741a520]
  ./a.out(MAIN_: MAIN_ at /home/zaikunzhang/tmp/nv.f90:9) [0x401308]
  ./a.out(main+0x33) [0x401233]
  /usr/lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7fc017401d90]
  /usr/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7fc017401e40]
  ./a.out(_start+0x25) [0x401125]

```

In case you said about the setting of `NVCOMPILER_TERM=trace`, I just followed the following section of the manual.

 ![Screenshot_2022-12-02_22-12-04](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a5e31b010084176a96af16ada87b0f3f09a7ed3b.png)

I agree that `gdb` + core would be a good solution. However, since my tests are conducted remotely via GitHub Actions in a non-interactive way, `gdb` is not really usable. I can only access the log files of the tests. It would be ideal if the compiler could print a human-readable backtrace (which can be recorded in the log), as most compilers do when invoked with `-g`.

I guess `nvfortran` also intends to do so, but either I have not found the correct way of invoking it, or the backtrace is not really human-readable (or I am not considered a human …).

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [December 2, 2022, 10:58pm UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/6 "2022-12-02T22:58:49Z")

</div>

Note that with the -traceback option one of the lines now identifies the file and line that the error occurred on, which is typically all you get from a traceback.

I found documentation for nvfortran that indicated that was the default, but would be turned off if certain compiler optimizations were invoked, but that does not seem to be correct from your results.

My own scripts and fpm --profile debug both add it:

```nohighlight
-Minform=inform -Mbackslash -Mbounds -Mchkptr -Mchkstk -traceback

```

Note that gdb(1) has many options for running in batch mode, and you can launch the application with gdb. Without trying it (so might be typos, but close) something like

```bash
gdb -batch -ex run -ex where -ex list -args "COMMAND ARGUMENTS" 

```

may be enough. The -x option allows you to set up much more elaborate scripts. I used a script that was used in batch scripts for years that did something along those lines; but there was a trick to short-circuit it when it succeeded so subsequent commands were not run when the run was successful that I do not remember at the moment. The gdb(1) web page almost certainly has examples of running in batch. Assuming it is rerunnable and has a short run time on Linux/Unix  
you can also say to run command and then only run it again if the initial run fails quite simply as  
well, using something like

```bash
CMD ARGS || gdb CMD -args ARGS -ex run -ex where -batch

```

as an alternative to allowing and processing a dump. The details can vary, bugt gdb(1) is definitely usable in batch mode.

The new line in your output

```nohighlight
/a.out(MAIN_: MAIN_ at /home/zaikunzhang/tmp/nv.f90:9) [0x401308]

```

now indicates to look at line nine in file nv.f90.

PS:

It was a long time ago I first heard “To err is Human. To really foul things up requires a computer.”. That has really held up well over the years.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [December 3, 2022, 7:18am UTC](https://fortran-lang.discourse.group/t/how-to-print-a-human-readable-backtrace-with-nvfortran/4843/7 "2022-12-03T07:18:05Z")

</div>

> [@urbanjost](#):
>
> Note that with the -traceback option one of the lines now identifies the file and line that the error occurred on, which is typically all you get from a traceback.

Sorry, I overlooked this particular line. It is sufficient for my use.

Thank you @urbanjost very much for taking time to introduce the batch mode of gdb. This is very useful and I will try it in my tests later. Thanks.
