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.

trace_base.cc 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright 2020 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/trace_base.h"
  17. #include <iostream>
  18. #include <fstream>
  19. #include <map>
  20. #include <unordered_map>
  21. #include <vector>
  22. #include <string>
  23. #include <sstream>
  24. #include <utility>
  25. #include <stack>
  26. #include <algorithm>
  27. #include "utils/graph_utils.h"
  28. namespace mindspore {
  29. // namespace to support debug trace infomation
  30. namespace trace {
  31. std::vector<DebugInfoPtr> GetSourceCodeDebugInfoVec(DebugInfoPtr debug_info) {
  32. std::vector<DebugInfoPtr> debug_with_loc_vec;
  33. while (debug_info != nullptr) {
  34. if (debug_info->location() != nullptr) {
  35. debug_with_loc_vec.push_back(debug_info);
  36. }
  37. if (debug_info->trace_info() != nullptr) {
  38. debug_info = debug_info->trace_info()->debug_info();
  39. } else {
  40. break;
  41. }
  42. }
  43. return debug_with_loc_vec;
  44. }
  45. DebugInfoPtr GetSourceCodeDebugInfo(const DebugInfoPtr &info) {
  46. auto debug_with_loc_vec = GetSourceCodeDebugInfoVec(info);
  47. if (debug_with_loc_vec.size() > 0) {
  48. return debug_with_loc_vec[0];
  49. } else {
  50. return info;
  51. }
  52. }
  53. std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
  54. if (info == nullptr) {
  55. return "";
  56. }
  57. auto src_info = GetSourceCodeDebugInfo(info);
  58. if (src_info->location() != nullptr) {
  59. return src_info->location()->ToString(tip);
  60. }
  61. return "";
  62. }
  63. // a trace info identifies a node transform, so we can trace the node transform through
  64. // a link of trace info and debug info
  65. std::string GetInfoWithAction(const std::vector<DebugInfoPtr> &info_vec, SourceLineTip tip) {
  66. if (info_vec.size() < 1) {
  67. return "";
  68. }
  69. if (info_vec.size() == 1) {
  70. return info_vec[0]->location()->ToString(tip);
  71. }
  72. std::string traced_info = info_vec[0]->location()->ToString(tip);
  73. for (size_t i = 1; i < info_vec.size(); i++) {
  74. auto action_name = info_vec[i - 1]->trace_info()->GetActionBetweenNode(info_vec[i]);
  75. if (action_name == "") {
  76. break;
  77. }
  78. traced_info = traced_info + action_name + info_vec[i]->location()->ToString(tip);
  79. }
  80. return traced_info;
  81. }
  82. std::string GetTracedDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
  83. if (info == nullptr) {
  84. return "";
  85. }
  86. auto info_vec = GetSourceCodeDebugInfoVec(info);
  87. if (info_vec.size() == 0) {
  88. return "";
  89. } else if (info_vec.size() == 1) {
  90. return info_vec[0]->location()->ToString(tip);
  91. } else if (info_vec.size() > 1) {
  92. return GetInfoWithAction(info_vec, tip);
  93. }
  94. return "";
  95. }
  96. std::string GetDebugInfo(const DebugInfoPtr &info, const std::string &prefix, SourceLineTip tip) {
  97. std::ostringstream oss;
  98. if (info == nullptr) {
  99. return "";
  100. }
  101. auto debug_info = GetTracedDebugInfo(info, tip);
  102. if (tip == kSourceLineTipDiscard) {
  103. std::replace(debug_info.begin(), debug_info.end(), '\r', '/');
  104. std::replace(debug_info.begin(), debug_info.end(), '\n', '/');
  105. }
  106. oss << prefix << debug_info;
  107. return oss.str();
  108. }
  109. } // namespace trace
  110. } // namespace mindspore