You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

info.cc 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "debug/info.h"
  17. #include <utility>
  18. #include <fstream>
  19. #include <sstream>
  20. #include "ir/anf.h"
  21. #include "pipeline/parse/parse.h"
  22. #include "pipeline/parse/python_adapter.h"
  23. namespace mindspore {
  24. std::string HighLightLine(const std::string &line, int col_begin, int col_end, SourceLineTip tip) {
  25. std::string temp_line = line;
  26. if (col_begin < col_end && col_begin != -1 && col_end <= SizeToInt(temp_line.length()) &&
  27. tip != kSourceLineTipDiscard) {
  28. std::string start = temp_line.substr(0, IntToSize(col_begin));
  29. std::string trimmed = temp_line.substr(IntToSize(col_begin), IntToSize(col_end - col_begin));
  30. std::string end = temp_line.substr(IntToSize(col_end), IntToSize(SizeToInt(temp_line.length()) - col_end));
  31. std::stringstream oss;
  32. std::stringstream tip_ss;
  33. std::string start_spaces(start.length(), ' ');
  34. if (tip == kSourceLineTipInLine) {
  35. temp_line = start + "<" + trimmed + ">" + end;
  36. } else if (tip == kSourceLineTipNextLine) {
  37. tip_ss << start_spaces << "^";
  38. }
  39. oss << temp_line << "\n" << tip_ss.str();
  40. return oss.str();
  41. }
  42. return temp_line;
  43. }
  44. // Generate debug information for the location node .
  45. // print the file name, line no and column no, and part of the content
  46. std::string Location::ToString(SourceLineTip tip) {
  47. std::stringstream debug_info_ss;
  48. debug_info_ss << " In file " << file_name_ << "(" << line_ << ")" << std::endl;
  49. if (line_ <= 0) {
  50. return debug_info_ss.str();
  51. }
  52. char path[PATH_MAX + 1] = {0x00};
  53. #if defined(_WIN32) || defined(_WIN64)
  54. if (file_name_.size() > PATH_MAX || _fullpath(path, file_name_.c_str(), PATH_MAX) == nullptr) {
  55. return debug_info_ss.str();
  56. }
  57. #else
  58. if (file_name_.size() > PATH_MAX || realpath(file_name_.c_str(), path) == nullptr) {
  59. return debug_info_ss.str();
  60. }
  61. #endif
  62. auto src_path = std::string(path);
  63. std::ifstream file(src_path);
  64. if (!file.is_open()) {
  65. return debug_info_ss.str();
  66. }
  67. int line_num = 0;
  68. std::string line;
  69. (void)getline(file, line);
  70. while (line_num != line_ - 1) {
  71. (void)getline(file, line);
  72. line_num++;
  73. }
  74. file.close();
  75. debug_info_ss << HighLightLine(line, column_, column_end_, tip) << std::endl;
  76. return debug_info_ss.str();
  77. }
  78. void TraceContext::ProcessAttributeFromContext() {
  79. trace_info_ = nullptr;
  80. location_ = nullptr;
  81. func_name_ = "";
  82. // if there is trace context, get info from previous context
  83. if (!TraceManager::trace_context_stack_.empty()) {
  84. TraceContextPtr top = TraceManager::trace_context_stack_.top();
  85. trace_info_ = top->trace_info_;
  86. location_ = top->location_;
  87. func_name_ = top->func_name_;
  88. }
  89. }
  90. DebugInfo::DebugInfo() {
  91. InitValueFromContext();
  92. unique_id_ = gen_unique_id();
  93. debug_id_ = -1;
  94. name_ = "";
  95. }
  96. DebugInfo::DebugInfo(const std::string &name) {
  97. InitValueFromContext();
  98. unique_id_ = gen_unique_id();
  99. debug_id_ = -1;
  100. name_ = name;
  101. }
  102. DebugInfo::DebugInfo(const LocationPtr &loc) {
  103. InitValueFromContext();
  104. unique_id_ = gen_unique_id();
  105. debug_id_ = -1;
  106. location_ = loc;
  107. }
  108. int64_t DebugInfo::debug_id() {
  109. // cppcheck-suppress variableScope
  110. static int64_t cur_debug_id = 0;
  111. if (debug_id_ == -1) {
  112. debug_id_ = cur_debug_id;
  113. cur_debug_id++;
  114. }
  115. return debug_id_;
  116. }
  117. int64_t DebugInfo::unique_id_through_copy() const {
  118. TraceInfoPtr trace_info = const_cast<DebugInfo *>(this)->trace_info();
  119. if (trace_info != nullptr) {
  120. if (trace_info->isa<TraceCopy>() && trace_info->debug_info() != nullptr) {
  121. return trace_info->debug_info()->unique_id_through_copy();
  122. }
  123. }
  124. return unique_id();
  125. }
  126. std::string DebugInfo::debug_name() {
  127. if (!name_.empty()) {
  128. return name_;
  129. }
  130. std::string debug_name = std::to_string(debug_id());
  131. name_ = debug_name;
  132. return debug_name;
  133. }
  134. std::string NodeDebugInfo::debug_name() {
  135. if (!name_.empty()) {
  136. return name_;
  137. }
  138. std::string prefix = "";
  139. if (node_.lock() != nullptr) {
  140. std::ostringstream oss;
  141. oss << "[" << node_.lock()->type_name() << "]";
  142. prefix = oss.str();
  143. }
  144. name_ = prefix + DebugInfo::debug_name();
  145. return name_;
  146. }
  147. std::string GraphDebugInfo::debug_name() {
  148. std::string prefix = "";
  149. return prefix + DebugInfo::debug_name();
  150. }
  151. LocationPtr GraphDebugInfo::location() {
  152. // function may have decorator which is included in its location
  153. if (deco_loc_ != nullptr) {
  154. LocationPtr loc = std::make_shared<Location>(*DebugInfo::location());
  155. loc->set_line(loc->line() + (deco_loc_->line_end() - deco_loc_->line() + 1));
  156. return loc;
  157. }
  158. return DebugInfo::location();
  159. }
  160. void GraphDebugInfo::set_deco_location(const LocationPtr &deco_list_loc) { deco_loc_ = deco_list_loc; }
  161. TraceContextPtr TraceManager::CurrentContextInfo() {
  162. if (!TraceManager::trace_context_stack_.empty()) {
  163. return TraceManager::trace_context_stack_.top();
  164. }
  165. return nullptr;
  166. }
  167. void TraceManager::DebugTrace(const std::string &func_name, const LocationPtr &location) {
  168. TraceContextPtr context = std::make_shared<TraceContext>(location);
  169. context->set_func_name(func_name);
  170. TraceManager::trace_context_stack_.push(context);
  171. }
  172. void TraceManager::DebugTrace(const LocationPtr &location) {
  173. TraceContextPtr context = std::make_shared<TraceContext>(location);
  174. TraceManager::trace_context_stack_.push(context);
  175. }
  176. void TraceManager::DebugTrace(const TraceInfoPtr &trace_info) {
  177. if (trace_info == nullptr) {
  178. MS_LOG(EXCEPTION) << "DebugTrace wrong traced info is null";
  179. }
  180. TraceContextPtr context = std::make_shared<TraceContext>(trace_info);
  181. if (trace_info->debug_info() == nullptr) {
  182. MS_LOG(EXCEPTION) << "Trace debug info is null";
  183. }
  184. TraceManager::trace_context_stack_.push(context);
  185. }
  186. void TraceManager::DebugTrace(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info) {
  187. if (trace_info == nullptr) {
  188. MS_LOG(EXCEPTION) << "DebugTrace wrong traced info is null";
  189. }
  190. auto cloned_info = trace_info->clone();
  191. cloned_info->set_debug_info(debug_info);
  192. if (cloned_info->debug_info() == nullptr) {
  193. MS_LOG(EXCEPTION) << "Trace debug info is null with cloned trace";
  194. }
  195. TraceContextPtr context = std::make_shared<TraceContext>(cloned_info);
  196. TraceManager::trace_context_stack_.push(context);
  197. }
  198. void TraceManager::EndTrace() { TraceManager::trace_context_stack_.pop(); }
  199. std::stack<TraceContextPtr> TraceManager::trace_context_stack_;
  200. } // namespace mindspore