1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/common_runtime/debugger_state_interface.h"
17
18#include "tensorflow/core/lib/core/errors.h"
19
20namespace tensorflow {
21
22// static
23DebuggerStateFactory* DebuggerStateRegistry::factory_ = nullptr;
24
25// static
26DebugGraphDecoratorFactory* DebugGraphDecoratorRegistry::factory_ = nullptr;
27
28const string SummarizeDebugTensorWatches(
29 const protobuf::RepeatedPtrField<DebugTensorWatch>& watches) {
30 std::ostringstream oss;
31
32 for (const DebugTensorWatch& watch : watches) {
33 string tensor_name =
34 strings::StrCat(watch.node_name(), ":", watch.output_slot());
35 if (watch.tolerate_debug_op_creation_failures()) {
36 oss << "(TOL)"; // Shorthand for "tolerate".
37 }
38 oss << tensor_name << "|";
39
40 for (const string& debug_op : watch.debug_ops()) {
41 oss << debug_op << ",";
42 }
43
44 oss << "@";
45 for (const string& debug_url : watch.debug_urls()) {
46 oss << debug_url << ",";
47 }
48
49 oss << ";";
50 }
51
52 return oss.str();
53}
54
55// static
56void DebuggerStateRegistry::RegisterFactory(
57 const DebuggerStateFactory& factory) {
58 delete factory_;
59 factory_ = new DebuggerStateFactory(factory);
60}
61
62// static
63Status DebuggerStateRegistry::CreateState(
64 const DebugOptions& debug_options,
65 std::unique_ptr<DebuggerStateInterface>* state) {
66 if (factory_ == nullptr || *factory_ == nullptr) {
67 return errors::Internal(
68 "Creation of debugger state failed. "
69 "It appears that TFDBG is not linked in this TensorFlow build.");
70 } else {
71 *state = (*factory_)(debug_options);
72 return OkStatus();
73 }
74}
75
76// static
77void DebugGraphDecoratorRegistry::RegisterFactory(
78 const DebugGraphDecoratorFactory& factory) {
79 delete factory_;
80 factory_ = new DebugGraphDecoratorFactory(factory);
81}
82
83// static
84Status DebugGraphDecoratorRegistry::CreateDecorator(
85 const DebugOptions& options,
86 std::unique_ptr<DebugGraphDecoratorInterface>* decorator) {
87 if (factory_ == nullptr || *factory_ == nullptr) {
88 return errors::Internal(
89 "Creation of graph decorator failed. "
90 "It appears that TFDBG is not linked in this TensorFlow build.");
91 } else {
92 *decorator = (*factory_)(options);
93 return OkStatus();
94 }
95}
96
97} // end namespace tensorflow
98