xref: /haiku/src/kits/debugger/settings/BreakpointSetting.cpp (revision 899e0ef82b5624ace2ccfa5f5a58c8ebee54aaef)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2013-2014, Rene Gollent, rene@gollent.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "BreakpointSetting.h"
9 
10 #include <Message.h>
11 
12 #include "ArchivingUtils.h"
13 #include "FunctionID.h"
14 #include "LocatableFile.h"
15 #include "UserBreakpoint.h"
16 
17 
18 BreakpointSetting::BreakpointSetting()
19 	:
20 	fFunctionID(NULL),
21 	fSourceFile(),
22 	fSourceLocation(),
23 	fRelativeAddress(0),
24 	fEnabled(false),
25 	fHidden(false),
26 	fConditionExpression()
27 {
28 }
29 
30 
31 BreakpointSetting::BreakpointSetting(const BreakpointSetting& other)
32 	:
33 	fFunctionID(other.fFunctionID),
34 	fSourceFile(other.fSourceFile),
35 	fSourceLocation(other.fSourceLocation),
36 	fRelativeAddress(other.fRelativeAddress),
37 	fEnabled(other.fEnabled),
38 	fHidden(other.fHidden),
39 	fConditionExpression(other.fConditionExpression)
40 {
41 	if (fFunctionID != NULL)
42 		fFunctionID->AcquireReference();
43 }
44 
45 
46 BreakpointSetting::~BreakpointSetting()
47 {
48 	_Unset();
49 }
50 
51 
52 status_t
53 BreakpointSetting::SetTo(const UserBreakpointLocation& location, bool enabled,
54 	bool hidden, const BString& conditionExpression)
55 {
56 	_Unset();
57 
58 	fFunctionID = location.GetFunctionID();
59 	if (fFunctionID != NULL)
60 		fFunctionID->AcquireReference();
61 
62 	if (LocatableFile* file = location.SourceFile())
63 		file->GetPath(fSourceFile);
64 
65 	fSourceLocation = location.GetSourceLocation();
66 	fRelativeAddress = location.RelativeAddress();
67 	fEnabled = enabled;
68 	fHidden = hidden;
69 	fConditionExpression = conditionExpression;
70 
71 	return B_OK;
72 }
73 
74 
75 status_t
76 BreakpointSetting::SetTo(const BMessage& archive)
77 {
78 	_Unset();
79 
80 	fFunctionID = ArchivingUtils::UnarchiveChild<FunctionID>(archive,
81 		"function");
82 	if (fFunctionID == NULL)
83 		return B_BAD_VALUE;
84 
85 	archive.FindString("sourceFile", &fSourceFile);
86 
87 	int32 line;
88 	if (archive.FindInt32("line", &line) != B_OK)
89 		line = -1;
90 
91 	int32 column;
92 	if (archive.FindInt32("column", &column) != B_OK)
93 		column = -1;
94 
95 	fSourceLocation = SourceLocation(line, column);
96 
97 	if (archive.FindUInt64("relativeAddress", &fRelativeAddress) != B_OK)
98 		fRelativeAddress = 0;
99 
100 	if (archive.FindBool("enabled", &fEnabled) != B_OK)
101 		fEnabled = false;
102 
103 	if (archive.FindBool("hidden", &fHidden) != B_OK)
104 		fHidden = false;
105 
106 	if (archive.FindString("condition", &fConditionExpression) != B_OK)
107 		fConditionExpression.Truncate(0);
108 
109 	return B_OK;
110 }
111 
112 
113 status_t
114 BreakpointSetting::WriteTo(BMessage& archive) const
115 {
116 	if (fFunctionID == NULL)
117 		return B_BAD_VALUE;
118 
119 	archive.MakeEmpty();
120 
121 	status_t error;
122 	if ((error = ArchivingUtils::ArchiveChild(fFunctionID, archive, "function"))
123 			!= B_OK
124 		|| (error = archive.AddString("sourceFile", fSourceFile)) != B_OK
125 		|| (error = archive.AddInt32("line", fSourceLocation.Line())) != B_OK
126 		|| (error = archive.AddInt32("column", fSourceLocation.Column()))
127 			!= B_OK
128 		|| (error = archive.AddUInt64("relativeAddress", fRelativeAddress))
129 			!= B_OK
130 		|| (error = archive.AddBool("enabled", fEnabled)) != B_OK
131 		|| (error = archive.AddBool("hidden", fHidden)) != B_OK
132 		|| (error = archive.AddString("condition", fConditionExpression))
133 			!= B_OK) {
134 		return error;
135 	}
136 
137 	return B_OK;
138 }
139 
140 
141 BreakpointSetting&
142 BreakpointSetting::operator=(const BreakpointSetting& other)
143 {
144 	if (this == &other)
145 		return *this;
146 
147 	_Unset();
148 
149 	fFunctionID = other.fFunctionID;
150 	if (fFunctionID != NULL)
151 		fFunctionID->AcquireReference();
152 
153 	fSourceFile = other.fSourceFile;
154 	fSourceLocation = other.fSourceLocation;
155 	fRelativeAddress = other.fRelativeAddress;
156 	fEnabled = other.fEnabled;
157 	fHidden = other.fHidden;
158 	fConditionExpression = other.fConditionExpression;
159 
160 	return *this;
161 }
162 
163 
164 void
165 BreakpointSetting::_Unset()
166 {
167 	if (fFunctionID != NULL) {
168 		fFunctionID->ReleaseReference();
169 		fFunctionID = NULL;
170 	}
171 
172 	fSourceFile.Truncate(0);
173 	fSourceLocation = SourceLocation();
174 	fRelativeAddress = 0;
175 	fEnabled = false;
176 	fConditionExpression.Truncate(0);
177 }
178