Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BreakpointPad.cs « MonoDevelop.Debugger « MonoDevelop.Debugger « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a9344dbe6e539c95cfbf8f86d70f29e7340d246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// BreakpointPad.cs
//
// Author:
//   Alfonso Santos Luaces <asantosluaces@gmail.com>
//
// Copyright (c) 2008 Alfonso Santos Luaces
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using Gtk;
using System;
using System.ComponentModel;

using Stock = MonoDevelop.Ide.Gui.Stock;

using Mono.Debugging.Client;

using MonoDevelop.Ide;
using MonoDevelop.Core;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Ide.Commands;
using MonoDevelop.Components;
using MonoDevelop.Components.Commands;
using MonoDevelop.Components.Docking;
using MonoDevelop.Ide.Gui.Components;
using MonoDevelop.Components.AutoTest;

namespace MonoDevelop.Debugger
{
	public class BreakpointPad : PadContent
	{
		BreakpointStore breakpoints;
		
		PadTreeView tree;
		TreeStore store;
		Widget control;
		ScrolledWindow sw;
		CommandEntrySet menuSet;
		TreeViewState treeState;

		ActionCommand gotoCmd;
		
		enum Columns
		{
			Icon,
			Selected,
			FileName,
			Breakpoint,
			Condition,
			TraceExp,
			HitCount,
			LastTrace
		}
		
		enum LocalCommands
		{
			GoToFile,
			Properties
		}
		
		protected override void Initialize (IPadWindow window)
		{
			Id = "MonoDevelop.Debugger.BreakpointPad";
			// Toolbar and menu definitions
			
			gotoCmd = new ActionCommand (LocalCommands.GoToFile, GettextCatalog.GetString ("Go to Breakpoint"));
			var propertiesCmd = new ActionCommand (LocalCommands.Properties, GettextCatalog.GetString ("Edit Breakpoint…"), Stock.Properties);

			// The toolbar registers the Properties command with the CommandManager for us,
			// but gotoCmd isn't used in the toolbar so we need to register it ourselves for the menu to work
			IdeApp.CommandService.RegisterCommand (gotoCmd);

			menuSet = new CommandEntrySet ();
			menuSet.Add (propertiesCmd);
			menuSet.Add (gotoCmd);
			menuSet.AddSeparator ();
			menuSet.AddItem (DebugCommands.EnableDisableBreakpoint);
			menuSet.AddItem (DebugCommands.DisableAllBreakpoints);
			menuSet.AddSeparator ();
			menuSet.AddItem (EditCommands.DeleteKey);
			menuSet.AddItem (DebugCommands.ClearAllBreakpoints);
			
			var toolbarSet = new CommandEntrySet ();
			toolbarSet.AddItem (DebugCommands.EnableDisableBreakpoint);
			toolbarSet.AddItem (DebugCommands.ClearAllBreakpoints);
			toolbarSet.AddItem (DebugCommands.DisableAllBreakpoints);
			toolbarSet.AddItem (EditCommands.Delete);
			toolbarSet.AddSeparator ();
			toolbarSet.Add (propertiesCmd);
			toolbarSet.AddSeparator ();
			toolbarSet.Add (new CommandEntry (DebugCommands.NewFunctionBreakpoint){ DisplayType = CommandEntryDisplayType.IconAndText });
			toolbarSet.Add (new CommandEntry (DebugCommands.NewCatchpoint){ DisplayType = CommandEntryDisplayType.IconAndText });
			
			// The breakpoint list
			
			store = new TreeStore (typeof(string), typeof (bool), typeof(string), typeof(object), typeof(string), typeof(string), typeof(string), typeof(string));
			var modelAttr = new SemanticModelAttribute ("store__Icon", "store__Selected","store_FileName",
				"store_Breakpoint", "store_Condition", "store_TraceExp", "store_HitCount", "store_LastTrace");
			TypeDescriptor.AddAttributes (store, modelAttr);

			tree = new PadTreeView ();
			tree.Model = store;
			tree.RulesHint = true;
			tree.HeadersVisible = true;
			tree.DoPopupMenu = ShowPopup;
			tree.KeyPressEvent += OnKeyPressEvent;
			tree.Selection.Mode = SelectionMode.Multiple;
			
			treeState = new TreeViewState (tree, (int) Columns.Breakpoint);
							
			var col = new TreeViewColumn ();
			var crp = new CellRendererImage ();
			col.PackStart (crp, false);
			col.AddAttribute (crp, "stock_id", (int) Columns.Icon);
			tree.AppendColumn (col);

			var toggleRender = new Gtk.CellRendererToggle ();
			toggleRender.Toggled += new ToggledHandler (ItemToggled);
			col = new TreeViewColumn ();
			col.PackStart (toggleRender, false);
			col.AddAttribute (toggleRender, "active", (int) Columns.Selected);
			tree.AppendColumn (col);

			var FrameCol = new TreeViewColumn ();
			var crt = tree.TextRenderer;
			FrameCol.Title = GettextCatalog.GetString ("Name");
			FrameCol.PackStart (crt, true);
			FrameCol.AddAttribute (crt, "text", (int) Columns.FileName);
			FrameCol.Resizable = true;
			FrameCol.Alignment = 0.0f;
			tree.AppendColumn (FrameCol);

			col = tree.AppendColumn (GettextCatalog.GetString ("Condition"), crt, "text", (int) Columns.Condition);
			col.Resizable = true;
			
			col = tree.AppendColumn (GettextCatalog.GetString ("Trace Expression"), crt, "text", (int) Columns.TraceExp);
			col.Resizable = true;
			
			col = tree.AppendColumn (GettextCatalog.GetString ("Hit Count"), crt, "text", (int) Columns.HitCount);
			col.Resizable = true;
			
			col = tree.AppendColumn (GettextCatalog.GetString ("Last Trace"), crt, "text", (int) Columns.LastTrace);
			col.Resizable = true;
			
			sw = new Gtk.ScrolledWindow ();
			sw.ShadowType = ShadowType.None;
			sw.Add (tree);
			
			control = sw;
			
			control.ShowAll ();
			
			breakpoints = DebuggingService.Breakpoints;
			
			UpdateDisplay ();

			breakpoints.BreakpointAdded += OnBreakpointAdded;
			breakpoints.BreakpointRemoved += OnBreakpointRemoved;
			breakpoints.Changed += OnBreakpointChanged;
			breakpoints.BreakpointUpdated += OnBreakpointUpdated;
			
			DebuggingService.PausedEvent += OnDebuggerStatusCheck;
			DebuggingService.ResumedEvent += OnDebuggerStatusCheck;
			DebuggingService.StoppedEvent += OnDebuggerStatusCheck;
			
			tree.RowActivated += OnRowActivated;
			
			var toolbar = window.GetToolbar (DockPositionType.Top);
			toolbar.Add (toolbarSet, sw);
			toolbar.ShowAll ();
		}
		
		public override void Dispose ()
		{
			breakpoints.BreakpointAdded -= OnBreakpointAdded;
			breakpoints.BreakpointRemoved -= OnBreakpointRemoved;
			breakpoints.Changed -= OnBreakpointChanged;
			breakpoints.BreakpointUpdated -= OnBreakpointUpdated;
			
			DebuggingService.PausedEvent -= OnDebuggerStatusCheck;
			DebuggingService.ResumedEvent -= OnDebuggerStatusCheck;
			DebuggingService.StoppedEvent -= OnDebuggerStatusCheck;

			IdeApp.CommandService.UnregisterCommand (gotoCmd);
			base.Dispose ();
		}

		void ShowPopup (Gdk.EventButton evt)
		{
			tree.ShowContextMenu (evt, menuSet, tree);
		}
		
		[CommandHandler (LocalCommands.Properties)]
		protected void OnProperties ()
		{
			var selected = tree.Selection.GetSelectedRows ();

			if (selected.Length == 1 && store.GetIter (out TreeIter iter, selected[0])) {
				var bp = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);
				if (DebuggingService.ShowBreakpointProperties (ref bp))
					UpdateDisplay ();
			}
		}

		[CommandUpdateHandler (LocalCommands.Properties)]
		protected void OnUpdateProperties (CommandInfo info)
		{
			info.Enabled = !DebuggingService.IsDebugging && tree.Selection.CountSelectedRows () == 1 ;
		}

		string GetIconId (BreakEvent bp)
		{
			if (bp is Catchpoint)
				return bp.Enabled ? "md-catchpoint" : "md-catchpoint-disabled";

			return bp.Enabled ? "md-breakpoint" : "md-breakpoint-disabled";
		}

		
		[CommandHandler (DebugCommands.EnableDisableBreakpoint)]
		protected void OnEnableDisable ()
		{
			breakpoints.Changed -= OnBreakpointChanged;

			try {
				bool enable = false;

				// If any breakpoints are disabled, we'll enable them all. Otherwise, disable them all.
				foreach (var path in tree.Selection.GetSelectedRows ()) {
					if (!store.GetIter (out TreeIter iter, path))
						continue;

					var bp = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);
					if (!bp.Enabled) {
						enable = true;
						break;
					}
				}

				foreach (var path in tree.Selection.GetSelectedRows ()) {
					if (!store.GetIter (out TreeIter iter, path))
						continue;

					var bp = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);
					bp.Enabled = enable;

					store.SetValue (iter, (int) Columns.Icon, GetIconId(bp));
					store.SetValue (iter, (int) Columns.Selected, enable);
				}
			} finally {
				breakpoints.Changed += OnBreakpointChanged;
			}
		}
		
		[CommandHandler (LocalCommands.GoToFile)]
		protected void OnBpJumpTo ()
		{
			var selected = tree.Selection.GetSelectedRows ();

			if (selected.Length == 1 && store.GetIter (out TreeIter iter, selected[0])) {
				var be = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);

				if (be is Breakpoint bp && !string.IsNullOrEmpty (bp.FileName))
					IdeApp.Workbench.OpenDocument (bp.FileName, null, bp.Line, 1).Ignore ();
			}
		}

		bool DeleteSelectedBreakpoints ()
		{
			bool deleted = false;

			breakpoints.BreakpointRemoved -= OnBreakpointRemoved;

			try {
				// Note: since we'll be modifying the list of breakpoints, we need to sort
				// the paths in reverse order.
				var selected = tree.Selection.GetSelectedRows ();
				Array.Sort (selected, new TreePathComparer (true));

				foreach (var path in selected) {
					if (!store.GetIter (out TreeIter iter, path))
						continue;

					var bp = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);
					breakpoints.Remove (bp);
					deleted = true;
				}
			} finally {
				breakpoints.BreakpointRemoved += OnBreakpointRemoved;
			}

			return deleted;
		}

		[CommandUpdateHandler (EditCommands.SelectAll)]
		protected void UpdateSelectAll (CommandInfo cmd)
		{
			cmd.Enabled = store.GetIterFirst (out TreeIter iter);
		}

		[CommandHandler (EditCommands.SelectAll)]
		protected void OnSelectAll ()
		{
			tree.Selection.SelectAll ();
		}

		[CommandHandler (EditCommands.Delete)]
		[CommandHandler (EditCommands.DeleteKey)]
		protected void OnDeleted ()
		{
			if (DeleteSelectedBreakpoints ())
				UpdateDisplay ();
		}

		[CommandUpdateHandler (LocalCommands.GoToFile)]
		protected void UpdateBpCommand (CommandInfo cmd)
		{
			cmd.Enabled = tree.Selection.CountSelectedRows () == 1;
		}
		
		[CommandUpdateHandler (EditCommands.Delete)]
		[CommandUpdateHandler (EditCommands.DeleteKey)]
		protected void UpdateDeleteCommand (CommandInfo cmd)
		{
			var selectedCount = tree.Selection.CountSelectedRows ();
			cmd.Enabled = selectedCount > 0;
			cmd.Text = cmd.Description = GettextCatalog.GetPluralString ("Remove Breakpoint", "Remove Breakpoints", selectedCount);
		}

		[CommandUpdateHandler (DebugCommands.EnableDisableBreakpoint)]
		[CommandUpdateHandler (DebugCommands.DisableAllBreakpoints)]
		protected void UpdateEnableDisableBreakpointCommand (CommandInfo cmd)
		{
			var selectedCount = tree.Selection.CountSelectedRows ();
			cmd.Enabled = selectedCount > 0;
			bool enable = false;

			foreach (var path in tree.Selection.GetSelectedRows ()) {
				if (!store.GetIter (out TreeIter iter, path))
					continue;

				var bp = (BreakEvent)store.GetValue (iter, (int)Columns.Breakpoint);
				if (!bp.Enabled) {
					enable = true;
					break;
				}
			}

			if (cmd.Command.Id.Equals (CommandManager.ToCommandId (DebugCommands.DisableAllBreakpoints))) {
				if (enable)
					cmd.Text = GettextCatalog.GetString ("Enable All Breakpoints");
				else
					cmd.Text = GettextCatalog.GetString ("Disable All Breakpoints");
			} else {
				if (enable)
					cmd.Text = GettextCatalog.GetPluralString ("Enable Breakpoint", "Enable Breakpoints", selectedCount);
				else
					cmd.Text = GettextCatalog.GetPluralString ("Disable Breakpoint", "Disable Breakpoints", selectedCount);
			}
		}

		[GLib.ConnectBefore]
		void OnKeyPressEvent (object sender, KeyPressEventArgs args)
		{
			// Delete the currently selected breakpoint(s) with any delete key
			switch (args.Event.Key) {
			case Gdk.Key.Delete:
			case Gdk.Key.KP_Delete:
			case Gdk.Key.BackSpace:
				if (DeleteSelectedBreakpoints ()) {
					args.RetVal = true;
					UpdateDisplay ();
				}
				break;
			case Gdk.Key.space:
				if (tree.Selection.CountSelectedRows () > 0) {
					OnEnableDisable ();
					args.RetVal = true;
				}
				break;
			}
		}
		
		void ItemToggled (object o, ToggledArgs args)
		{
			breakpoints.Changed -= OnBreakpointChanged;

			try {
				TreeIter iter;

				if (store.GetIterFromString (out iter, args.Path)) {
					var bp = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);
					bp.Enabled = !bp.Enabled;

					store.SetValue (iter, (int) Columns.Icon, GetIconId(bp));
					store.SetValue (iter, (int) Columns.Selected, bp.Enabled);
				}
			} finally {
				breakpoints.Changed += OnBreakpointChanged;
			}
		}
		
		public void UpdateDisplay ()
		{
			if (tree.IsRealized)
				tree.ScrollToPoint (0, 0);

			treeState.Save ();
			
			store.Clear ();
			if (breakpoints != null) {	
				foreach (BreakEvent be in breakpoints) {
					if (be.NonUserBreakpoint)
						continue;

					string hitCount = be.HitCountMode != HitCountMode.None ? be.CurrentHitCount.ToString () : "";
					string traceExp = (be.HitAction & HitAction.PrintExpression) != HitAction.None ? be.TraceExpression : "";
					string traceVal = (be.HitAction & HitAction.PrintExpression) != HitAction.None ? be.LastTraceValue : "";
					string name, condition = null;

					if (be is FunctionBreakpoint fb) {
						if (fb.ParamTypes != null)
							name = fb.FunctionName + "(" + string.Join (", ", fb.ParamTypes) + ")";
						else
							name = fb.FunctionName;
					} else if (be is Breakpoint bp) {
						name = string.Format ("{0}:{1},{2}", bp.FileName, bp.Line, bp.Column);
						condition = bp.ConditionExpression;
					} else if (be is Catchpoint cp) {
						name = cp.ExceptionName;
					} else {
						name = "";
					}

					store.AppendValues (GetIconId (be), be.Enabled, name, be, condition, traceExp, hitCount, traceVal);
				}
			}

			treeState.Load ();
		}
		
		void OnBreakpointUpdated (object s, BreakpointEventArgs args)
		{
			Runtime.RunInMainThread (() => {
				if (!store.GetIterFirst (out TreeIter iter))
					return;

				do {
					var bp = (BreakEvent) store.GetValue (iter, (int) Columns.Breakpoint);
					if (bp == args.Breakpoint) {
						string hitCount = bp.HitCountMode != HitCountMode.None ? bp.CurrentHitCount.ToString () : "";
						string traceVal = (bp.HitAction & HitAction.PrintExpression) != HitAction.None ? bp.LastTraceValue : "";
						store.SetValue (iter, (int) Columns.HitCount, hitCount);
						store.SetValue (iter, (int) Columns.LastTrace, traceVal);
						break;
					}
				} while (store.IterNext (ref iter));
			}).Ignore ();
		}
		
		protected void OnBreakpointAdded (object o, EventArgs args)
		{
			Runtime.RunInMainThread ((System.Action) UpdateDisplay).Ignore ();
		}
		
		protected void OnBreakpointRemoved (object o, EventArgs args)
		{
			Runtime.RunInMainThread ((System.Action) UpdateDisplay).Ignore ();
		}
		
		protected void OnBreakpointChanged (object o, EventArgs args)
		{
			Runtime.RunInMainThread ((System.Action) UpdateDisplay).Ignore ();
		}
		
		void OnDebuggerStatusCheck (object s, EventArgs a)
		{
			if (control != null)
				control.Sensitive = !breakpoints.IsReadOnly;
		}

		void OnRowActivated (object o, Gtk.RowActivatedArgs args)
		{
			OnBpJumpTo ();
		}
		
		public override Control Control {
			get {
				return control;
			}
		}

		public string DefaultPlacement {
			get { return "Bottom"; }
		}

		protected void OnDeleteClicked (object o, EventArgs args)
		{
			OnDeleted ();
		}
	}
}