-2

There are several good questions and answers about how to print the stack trace from a function in C/C++ like https://stackoverflow.com/a/54365144/2348209, but it seems that this way is not usable in Cuda. While I was able to use the following snippet in C/C++, I could not use it in Cuda.

__device__
void print_trace(void) {
        void *array[10];
        size_t size;
        char **strings;
        size_t i;
        size = backtrace(array, 10);
        strings = backtrace_symbols(array, size);
        printf("Obtained %zd stack frames.\n", size);
        for (i = 0; i < size; i++)
                printf("%s\n", strings[i]);

        free(strings);
}

and received the following error:

calling a __host__ function (*backtrace*) from a __device__ function is not allowed
calling a __host__ function (*backtrace_symbols*) from a __device__ function is not allowed

I was wondering if anybody knows a way to print the stack trace in Cuda at least to see what's the caller function.

talonmies
  • 70,661
  • 34
  • 192
  • 269
user2348209
  • 136
  • 11
  • The device source debugger can print stack traces – talonmies Apr 03 '20 at 04:10
  • @talonmies thank you for your suggestion. But the issue is that the kernel is called through another library with a function pointer which I don't have access to that library source code and so I cannot use the debugger with that. When I want to step into that call, the debugger passes that call and stops after that line. – user2348209 Apr 03 '20 at 16:05
  • 1
    Then you are out of luck – talonmies Apr 03 '20 at 16:14

1 Answers1

-2

The reason for this is that backtrace() is a function defined for use on a CPU (on the host). You cannot run this type of function on a GPU (on a cuda device).

One method for debugging that I've used in the past is to define a wrapper to use on every cuda call.

#define cudaErrorCheck(result) { cudaCheck((result), __FILE__, __LINE__); }
inline void cudaCheck(cudaError_t error, const char *file,
                      int line, bool abort = true) {
  if (error != cudaSuccess) {
    std::cerr << "__________CUDA_ERROR_____________" << endl;
    fprintf(stderr, "CUDA ERROR: %s %s %d\n",
            cudaGetErrorString(error), file, line);
    if (abort) {
      exit(error);
    }
  }
}

Then whenever you call a function that you want more information on what went wrong, if something did go wrong.

For example...

  cudaErrorCheck(cudaMemcpy(d_a, a, size, cudaMemcpyHostToDevice));
  cudaErrorCheck(cudaMemcpy(d_b, b, size, cudaMemcpyHostToDevice));
  cudaErrorCheck(cudaDeviceSynchronize());

I would check out the cuda-memcheck tool if you want more information on stack backtraces.

SpaceKatt
  • 976
  • 6
  • 12
  • 1
    Thank you for your answer but this doesn't answer my question. I need to find the stack trace and knowing the function is returning SUCCESS or not is not relevant here. – user2348209 Apr 03 '20 at 01:26
  • Okay, I edited my answer to include a link which may help further. – SpaceKatt Apr 03 '20 at 19:21
  • thank you for the edit, but as I have mentioned in another comment under my question, I cannot use a debugger for this case. I am only interested in print solutions, or anything that I can get the caller function right from the function itself by running the code. – user2348209 Apr 03 '20 at 20:06