Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestions for implementing flang's built-in functions "backtrace" #117434

Open
dty2 opened this issue Nov 23, 2024 · 5 comments
Open

Suggestions for implementing flang's built-in functions "backtrace" #117434

dty2 opened this issue Nov 23, 2024 · 5 comments
Labels
flang Flang issues not falling into any other category

Comments

@dty2
Copy link

dty2 commented Nov 23, 2024

I notice that gfortran's have a intrinsic function "abort" that can abort and print a backtrace when called.
flang also has it, but it only aborts without printing a backtrace.
So, I modified it for flang, and my implemention is different from gfortran's. So I would like to get some suggestions...
the source is

! L2 Norm of a vector
function vector_norm(n,vec) result(norm)
  implicit none
  integer, intent(in) :: n
  real, intent(in) :: vec(n)
  real :: norm

  norm = sqrt(sum(vec**2))
  call backtrace

end function vector_norm

! Print matrix A to screen
subroutine print_matrix(n,m,A)
  implicit none
  integer, intent(in) :: n
  integer, intent(in) :: m
  integer, intent(in) :: A(n, m)

  real :: v(9)
  real :: vector_norm
  integer :: i

  v(:) = 9

  do i = 1, n
    print *, A(i, 1:m)
  end do

  print *, 'Vector norm = ', vector_norm(9,v)
end subroutine print_matrix

program test_backtrace
  integer :: k(10, 10)
  k = 9
  call print_matrix(2, 3, k)
end program test_backtrace

And, gfortran's backtrace print like:

#0  0x650cc12924a1 in vector_norm_
        at /home/hunter/plct/fortran/test.f90:10
#1  0x650cc12923ed in print_matrix_
        at /home/hunter/plct/fortran/test.f90:32
#2  0x650cc1292527 in test_backtrace
        at /home/hunter/plct/fortran/test.f90:40
#3  0x650cc1292577 in main
        at /home/hunter/plct/fortran/test.f90:41

And my implemention print like:

./test(_FortranABacktrace+0x32) [0x5b30328e1312]
./test(vector_norm_+0x129) [0x5b30328b38f9]
./test(print_matrix_+0x252) [0x5b30328b3b62]
./test(_QQmain+0x105) [0x5b30328b3c95]
./test(main+0x12) [0x5b30328b3cb2]
/usr/lib/libc.so.6(+0x25e08) [0x786915b75e08]
/usr/lib/libc.so.6(__libc_start_main+0x8c) [0x786915b75ecc]
./test(_start+0x25) [0x5b30328b36f5]
@github-actions github-actions bot added the flang Flang issues not falling into any other category label Nov 23, 2024
@tarunprabhu
Copy link
Contributor

Does clang have similar functionality, and is this output comparable to that?

@dty2
Copy link
Author

dty2 commented Nov 23, 2024

@tarunprabhu
Thanks for your reply. Yes, the built-in function abort of gfortran does have the function of outputting backtrace.

You can see the description of abort in gfortran here https://gcc.gnu.org/onlinedocs/gfortran/ABORT.html.
And I found TODO in the source code of flang's abort implementation, which also clearly stated the lack of backtrace function.

The version I implemented is based on backtrace and backtrace_symbol. I directly output all the backtrace content. Therefore, my version of the implementation seems to have a lot more content than the gfortran version.

And you need to add -rdynamic to output the function name (because only in this way will the connector called by flang add the required symbols to the dynamic symbol table, while gfortran adds symbol debugging information through -g)

@tarunprabhu
Copy link
Contributor

So I would like to get some suggestions...

What kind of feedback are you looking for? Do you intend to contribute your code to flang?

@dty2
Copy link
Author

dty2 commented Nov 24, 2024

@tarunprabhu Oh, my bad, I didn't make it clear enough at first. I just plan to contribute code to flang, and now all I need is a pull request. But I'm a newbie and have never made a pull request. Therefore, in order to avoid causing trouble to everyone, I plan to ask for your opinions in advance. The output difference between gfortran's implementation and my implementation is compared at the top. You can find that I have implemented the core function of outputting backtrace, but the format and content are somewhat different from gfortran, so the suggestions I want are about the output content and format. For example, you can give me suggestions like: "Your output format is not regular, you should add a line number to each line, like gfortran, so it will look clearer.", or suggestions like: "Your output is a bit redundant, you don't need to output content related to function addresses."

@tarunprabhu
Copy link
Contributor

Thank you for being considerate and asking first. The Flang discourse is a better place to get feedback such as this. You can post an RFC there asking if the format is reasonable or if the community would prefer a different output and that you intend to contribute the code to flang. Once get feedback saying that it is ok to proceed with a PR, please do so. There may be more discussion on the PR as well, and several rounds of revisions may be necessary. These are sometimes quick, but can also take some time, so please be prepared for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

2 participants