HCC
HCC is a single-source, C/C++ compiler for heterogeneous computing. It's optimized with HSA (http://www.hsafoundation.com/).
hc_rt_debug.h
1 #pragma once
2 
3 #include <cstdlib>
4 #include <cstdio>
5 #define UNW_LOCAL_ONLY
6 #include <libunwind.h>
7 #ifndef USE_LIBCXX
8 #include <cxxabi.h>
9 #endif
10 #include <iostream>
11 #include <cstring>
12 #include <sstream>
13 #include <string>
14 #include <vector>
15 
16 
17 #define DB_API 0 /* 0x0001 HCC runtime API calls */
18 #define DB_CMD 1 /* 0x0002 Kernel and Copy Commands and Barriers */
19 #define DB_WAIT 2 /* 0x0004 Synchronization and waiting for commands to finish. */
20 #define DB_AQL 3 /* 0x0008 Decode and display AQL packets */
21 #define DB_QUEUE 4 /* 0x0010 Queue creation and desruction commands, and queue contents after each command push. */
22 #define DB_SIG 5 /* 0x0020 Signal creation, allocation, pool */
23 #define DB_LOCK 6 /* 0x0040 Locks and HCC thread-safety code */
24 #define DB_KERNARG 7 /* 0x0080 Show first 128 bytes of kernarg blocks passed to kernels */
25 #define DB_COPY 8 /* 0x0100 Copy debug */
26 #define DB_COPY2 9 /* 0x0200 Detailed copy debug */
27 #define DB_RESOURCE 10 /* 0x0400 Resource (signal/kernarg/queue) allocation and growth, and other unusual potentially performance-impacting events. */
28 #define DB_INIT 11 /* 0x0800 HCC initialization and shutdown. */
29 #define DB_MISC 12 /* 0x1000 misc debug, not yet classified. */
30 #define DB_AQL2 13 /* 0x2000 Show raw bytes of AQL packet */
31 #define DB_CODE 14 /* 0x4000 Show CreateKernel and code creation debug */
32 #define DB_CMD2 15 /* 0x8000 More detailed command info, including barrier commands created by hcc rt. */
33 // If adding new define here update the table below:
34 
35 extern unsigned HCC_DB;
36 
37 #define DBPARM(x) #x << "=" << x
38 
39 
40 // Keep close to debug defs above since these have to be kept in-sync
41 static std::vector<std::string> g_DbStr = {"api", "cmd", "wait", "aql", "queue", "sig", "lock", "kernarg", "copy", "copy2", "resource", "init", "misc", "aql2", "code", "cmd2"};
42 
43 
44 // Macro for prettier debug messages, use like:
45 // DBOUT(" Something happened" << myId() << " i= " << i << "\n");
46 #define COMPILE_HCC_DB 1
47 
48 #define DBFLAG(db_flag) (HCC_DB & (1<<(db_flag)))
49 
50 #define DBSTREAM std::cerr
51 
52 // Use str::stream so output is atomic wrt other threads:
53 #define DBOUT(db_flag, msg) \
54 if (COMPILE_HCC_DB && (HCC_DB & (1<<(db_flag)))) { \
55  std::stringstream sstream;\
56  sstream << " hcc-" << g_DbStr[db_flag] << " tid:" << hcc_tlsShortTid._shortTid << " " << msg ; \
57  DBSTREAM << sstream.str();\
58 };
59 
60 // Like DBOUT, but add newline:
61 #define DBOUTL(db_flag, msg) \
62 if (COMPILE_HCC_DB && (HCC_DB & (1<<(db_flag)))) { \
63  std::stringstream sstream;\
64  sstream << " hcc-" << g_DbStr[db_flag] << " tid:" << hcc_tlsShortTid._shortTid << " " << msg << "\n"; \
65  DBSTREAM << sstream.str();\
66 };
67 
68 // get a the current filename without the path
69 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/')+1 : __FILE__)
70 
71 // Class with a constructor that gets called when new thread is created:
72 struct ShortTid {
73  ShortTid() ;
74  int _shortTid;
75 };
76 
77 extern thread_local ShortTid hcc_tlsShortTid;
78 
79 namespace hc {
80 
81 
82  static std::string get_backtrace() {
83  constexpr int buffer_size = 512;
84 
85  std::string bt("");
86 
87  unw_cursor_t cursor;
88  unw_context_t context;
89  unw_getcontext(&context);
90  unw_init_local(&cursor, &context);
91 
92  bt += std::string("Backtrace:\n");
93 
94  while(unw_step(&cursor) > 0) {
95  // get the program counter
96  unw_word_t pc;
97  unw_get_reg(&cursor, UNW_REG_IP, &pc);
98  if (pc == 0x00)
99  break;
100 
101  // get the function name
102  char func[buffer_size];
103  char* demangled = nullptr;
104  const char* print_func_name;
105  unw_word_t offp;
106  if (unw_get_proc_name(&cursor, func, sizeof(func), &offp) == 0) {
107  int status;
108 #ifndef USE_LIBCXX
109  demangled = abi::__cxa_demangle(func, nullptr, nullptr, &status);
110 #endif
111  print_func_name = demangled ? demangled : func;
112  }
113  else {
114  print_func_name = "<unknown function>";
115  }
116 
117  char loc[buffer_size];
118  std::snprintf(loc, buffer_size, "0x%016lx:\t%s + 0x%lx\n", pc, print_func_name, offp);
119  bt += std::string(loc);
120 
121  if (demangled)
122  free(demangled);
123  }
124  return bt;
125  }
126 
127  static void print_backtrace() {
128  std::string bt = get_backtrace();
129  std::printf("\n%s\n", bt.c_str());
130  }
131 
132 } // namespace hc
Definition: hc_rt_debug.h:72
Heterogeneous C++ (HC) namespace.
Definition: grid_launch.h:10