Merge changes from emacs-23
[bpt/emacs.git] / lisp / progmodes / gdb-mi.el
1 ;;; gdb-mi.el --- User Interface for running GDB
2
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Nick Roberts <nickrob@gnu.org>
6 ;; Maintainer: FSF
7 ;; Keywords: unix, tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Credits:
27
28 ;; This file was written by by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It is currently being developed
30 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
31 ;; of Code 2009 Project "Emacs GDB/MI migration".
32
33 ;;; Commentary:
34
35 ;; This mode acts as a graphical user interface to GDB. You can interact with
36 ;; GDB through the GUD buffer in the usual way, but there are also further
37 ;; buffers which control the execution and describe the state of your program.
38 ;; It separates the input/output of your program from that of GDB and displays
39 ;; expressions and their current values in their own buffers. It also uses
40 ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
41 ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
42 ;; manual).
43
44 ;; M-x gdb will start the debugger.
45
46 ;; This file uses GDB/MI as the primary interface to GDB. It is still under
47 ;; development and is part of a process to migrate Emacs from annotations (as
48 ;; used in gdb-ui.el) to GDB/MI. It runs gdb with GDB/MI (-interp=mi) and
49 ;; access CLI using "-interpreter-exec console cli-command". This code works
50 ;; without gdb-ui.el and uses MI tokens instead of queues. Eventually MI
51 ;; should be asynchronous.
52
53 ;; This mode will PARTLY WORK WITH RECENT GDB RELEASES (status in modeline
54 ;; doesn't update properly when execution commands are issued from GUD buffer)
55 ;; and WORKS BEST when GDB runs asynchronously: maint set linux-async on.
56 ;;
57 ;; You need development version of GDB 7.0 for the thread buffer to work.
58
59 ;; This file replaces gdb-ui.el and is for development with GDB. Use the
60 ;; release branch of Emacs 22 for the latest version of gdb-ui.el.
61
62 ;; Windows Platforms:
63
64 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
65 ;; explicitly in your program if you want timely display of I/O in Emacs.
66 ;; Alternatively you can make the output stream unbuffered, for example, by
67 ;; using a macro:
68
69 ;; #ifdef UNBUFFERED
70 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
71 ;; #endif
72
73 ;; and compiling with -DUNBUFFERED while debugging.
74
75 ;; If you are using Cygwin GDB and find that the source is not being displayed
76 ;; in Emacs when you step through it, possible solutions are to:
77
78 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
79 ;; (Since 22.1 Emacs builds under Cygwin.)
80 ;; 2) Use MinGW GDB instead.
81 ;; 3) Use cygwin-mount.el
82
83 ;;; Mac OSX:
84
85 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
86 ;; some changes to the version that they include as part of Mac OSX.
87 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
88 ;; as earlier versions don not compile on Mac OSX.
89
90 ;;; Known Bugs:
91
92 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
93 ;; line information, e.g., a routine in libc (just a TODO item).
94
95 ;; TODO:
96 ;; 2) Watch windows to work with threads.
97 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
98 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
99
100 ;;; Code:
101
102 (require 'gud)
103 (require 'json)
104 (require 'bindat)
105 (eval-when-compile (require 'cl))
106
107 (defvar tool-bar-map)
108 (defvar speedbar-initial-expansion-list-name)
109 (defvar speedbar-frame)
110
111 (defvar gdb-memory-address "main")
112 (defvar gdb-memory-last-address nil
113 "Last successfully accessed memory address.")
114 (defvar gdb-memory-next-page nil
115 "Address of next memory page for program memory buffer.")
116 (defvar gdb-memory-prev-page nil
117 "Address of previous memory page for program memory buffer.")
118
119 (defvar gdb-thread-number nil
120 "Main current thread.
121
122 Invalidation triggers use this variable to query GDB for
123 information on the specified thread by wrapping GDB/MI commands
124 in `gdb-current-context-command'.
125
126 This variable may be updated implicitly by GDB via `gdb-stopped'
127 or explicitly by `gdb-select-thread'.
128
129 Only `gdb-setq-thread-number' should be used to change this
130 value.")
131
132 (defvar gdb-frame-number nil
133 "Selected frame level for main current thread.
134
135 Updated according to the following rules:
136
137 When a thread is selected or current thread stops, set to \"0\".
138
139 When current thread goes running (and possibly exits eventually),
140 set to nil.
141
142 May be manually changed by user with `gdb-select-frame'.")
143
144 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
145
146 ;; Used to show overlay arrow in source buffer. All set in
147 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
148 ;; these but rely on buffer-local thread information instead.
149 (defvar gdb-selected-frame nil
150 "Name of selected function for main current thread.")
151 (defvar gdb-selected-file nil
152 "Name of selected file for main current thread.")
153 (defvar gdb-selected-line nil
154 "Number of selected line for main current thread.")
155
156 (defvar gdb-threads-list nil
157 "Associative list of threads provided by \"-thread-info\" MI command.
158
159 Keys are thread numbers (in strings) and values are structures as
160 returned from -thread-info by `gdb-json-partial-output'. Updated in
161 `gdb-thread-list-handler-custom'.")
162
163 (defvar gdb-running-threads-count nil
164 "Number of currently running threads.
165
166 If nil, no information is available.
167
168 Updated in `gdb-thread-list-handler-custom'.")
169
170 (defvar gdb-stopped-threads-count nil
171 "Number of currently stopped threads.
172
173 See also `gdb-running-threads-count'.")
174
175 (defvar gdb-breakpoints-list nil
176 "Associative list of breakpoints provided by \"-break-list\" MI command.
177
178 Keys are breakpoint numbers (in string) and values are structures
179 as returned from \"-break-list\" by `gdb-json-partial-output'
180 \(\"body\" field is used). Updated in
181 `gdb-breakpoints-list-handler-custom'.")
182
183 (defvar gdb-current-language nil)
184 (defvar gdb-var-list nil
185 "List of variables in watch window.
186 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
187 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
188 address for root variables.")
189 (defvar gdb-main-file nil "Source file from which program execution begins.")
190
191 ;; Overlay arrow markers
192 (defvar gdb-stack-position nil)
193 (defvar gdb-thread-position nil)
194 (defvar gdb-disassembly-position nil)
195
196 (defvar gdb-location-alist nil
197 "Alist of breakpoint numbers and full filenames. Only used for files that
198 Emacs can't find.")
199 (defvar gdb-active-process nil
200 "GUD tooltips display variable values when t, and macro definitions otherwise.")
201 (defvar gdb-error "Non-nil when GDB is reporting an error.")
202 (defvar gdb-macro-info nil
203 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
204 (defvar gdb-register-names nil "List of register names.")
205 (defvar gdb-changed-registers nil
206 "List of changed register numbers (strings).")
207 (defvar gdb-buffer-fringe-width nil)
208 (defvar gdb-last-command nil)
209 (defvar gdb-prompt-name nil)
210 (defvar gdb-token-number 0)
211 (defvar gdb-handler-alist '())
212 (defvar gdb-handler-number nil)
213 (defvar gdb-source-file-list nil
214 "List of source files for the current executable.")
215 (defvar gdb-first-done-or-error t)
216 (defvar gdb-source-window nil)
217 (defvar gdb-inferior-status nil)
218 (defvar gdb-continuation nil)
219 (defvar gdb-version nil)
220 (defvar gdb-filter-output nil
221 "Message to be shown in GUD console.
222
223 This variable is updated in `gdb-done-or-error' and returned by
224 `gud-gdbmi-marker-filter'.")
225
226 (defvar gdb-non-stop nil
227 "Indicates whether current GDB session is using non-stop mode.
228
229 It is initialized to `gdb-non-stop-setting' at the beginning of
230 every GDB session.")
231
232 (defvar gdb-buffer-type nil
233 "One of the symbols bound in `gdb-buffer-rules'.")
234 (make-variable-buffer-local 'gdb-buffer-type)
235
236 (defvar gdb-output-sink 'nil
237 "The disposition of the output of the current gdb command.
238 Possible values are these symbols:
239
240 `user' -- gdb output should be copied to the GUD buffer
241 for the user to see.
242
243 `emacs' -- output should be collected in the partial-output-buffer
244 for subsequent processing by a command. This is the
245 disposition of output generated by commands that
246 gdb mode sends to gdb on its own behalf.")
247
248 ;; Pending triggers prevent congestion: Emacs won't send two similar
249 ;; consecutive requests.
250
251 (defvar gdb-pending-triggers '()
252 "A list of trigger functions which have not yet been handled.
253
254 Elements are either function names or pairs (buffer . function)")
255
256 (defmacro gdb-add-pending (item)
257 `(push ,item gdb-pending-triggers))
258 (defmacro gdb-pending-p (item)
259 `(member ,item gdb-pending-triggers))
260 (defmacro gdb-delete-pending (item)
261 `(setq gdb-pending-triggers
262 (delete ,item gdb-pending-triggers)))
263
264 (defmacro gdb-wait-for-pending (&rest body)
265 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
266
267 This function checks `gdb-pending-triggers' value every
268 `gdb-wait-for-pending' seconds."
269 (run-with-timer
270 0.5 nil
271 `(lambda ()
272 (if (not gdb-pending-triggers)
273 (progn ,@body)
274 (gdb-wait-for-pending ,@body)))))
275
276 ;; Publish-subscribe
277
278 (defmacro gdb-add-subscriber (publisher subscriber)
279 "Register new PUBLISHER's SUBSCRIBER.
280
281 SUBSCRIBER must be a pair, where cdr is a function of one
282 argument (see `gdb-emit-signal')."
283 `(add-to-list ',publisher ,subscriber t))
284
285 (defmacro gdb-delete-subscriber (publisher subscriber)
286 "Unregister SUBSCRIBER from PUBLISHER."
287 `(setq ,publisher (delete ,subscriber
288 ,publisher)))
289
290 (defun gdb-get-subscribers (publisher)
291 publisher)
292
293 (defun gdb-emit-signal (publisher &optional signal)
294 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
295 (dolist (subscriber (gdb-get-subscribers publisher))
296 (funcall (cdr subscriber) signal)))
297
298 (defvar gdb-buf-publisher '()
299 "Used to invalidate GDB buffers by emitting a signal in
300 `gdb-update'.
301
302 Must be a list of pairs with cars being buffers and cdr's being
303 valid signal handlers.")
304
305 (defgroup gdb nil
306 "GDB graphical interface"
307 :group 'tools
308 :link '(info-link "(emacs)GDB Graphical Interface")
309 :version "23.2")
310
311 (defgroup gdb-non-stop nil
312 "GDB non-stop debugging settings"
313 :group 'gdb
314 :version "23.2")
315
316 (defgroup gdb-buffers nil
317 "GDB buffers"
318 :group 'gdb
319 :version "23.2")
320
321 (defcustom gdb-debug-log-max 128
322 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
323 :group 'gdb
324 :type '(choice (integer :tag "Number of elements")
325 (const :tag "Unlimited" nil))
326 :version "22.1")
327
328 (defcustom gdb-non-stop-setting t
329 "When in non-stop mode, stopped threads can be examined while
330 other threads continue to execute.
331
332 GDB session needs to be restarted for this setting to take
333 effect."
334 :type 'boolean
335 :group 'gdb-non-stop
336 :version "23.2")
337
338 ;; TODO Some commands can't be called with --all (give a notice about
339 ;; it in setting doc)
340 (defcustom gdb-gud-control-all-threads t
341 "When enabled, GUD execution commands affect all threads when
342 in non-stop mode. Otherwise, only current thread is affected."
343 :type 'boolean
344 :group 'gdb-non-stop
345 :version "23.2")
346
347 (defcustom gdb-switch-reasons t
348 "List of stop reasons which cause Emacs to switch to the thread
349 which caused the stop. When t, switch to stopped thread no matter
350 what the reason was. When nil, never switch to stopped thread
351 automatically.
352
353 This setting is used in non-stop mode only. In all-stop mode,
354 Emacs always switches to the thread which caused the stop."
355 ;; exited, exited-normally and exited-signalled are not
356 ;; thread-specific stop reasons and therefore are not included in
357 ;; this list
358 :type '(choice
359 (const :tag "All reasons" t)
360 (set :tag "Selection of reasons..."
361 (const :tag "A breakpoint was reached." "breakpoint-hit")
362 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
363 (const :tag "A read watchpoint was triggered." "read-watchpoint-trigger")
364 (const :tag "An access watchpoint was triggered." "access-watchpoint-trigger")
365 (const :tag "Function finished execution." "function-finished")
366 (const :tag "Location reached." "location-reached")
367 (const :tag "Watchpoint has gone out of scope" "watchpoint-scope")
368 (const :tag "End of stepping range reached." "end-stepping-range")
369 (const :tag "Signal received (like interruption)." "signal-received"))
370 (const :tag "None" nil))
371 :group 'gdb-non-stop
372 :version "23.2"
373 :link '(info-link "(gdb)GDB/MI Async Records"))
374
375 (defcustom gdb-stopped-hooks nil
376 "This variable holds a list of functions to be called whenever
377 GDB stops.
378
379 Each function takes one argument, a parsed MI response, which
380 contains fields of corresponding MI *stopped async record:
381
382 ((stopped-threads . \"all\")
383 (thread-id . \"1\")
384 (frame (line . \"38\")
385 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
386 (file . \"server.c\")
387 (args ((value . \"0x804b038\")
388 (name . \"arg\")))
389 (func . \"hello\")
390 (addr . \"0x0804869e\"))
391 (reason . \"end-stepping-range\"))
392
393 Note that \"reason\" is only present in non-stop debugging mode.
394
395 `bindat-get-field' may be used to access the fields of response.
396
397 Each function is called after the new current thread was selected
398 and GDB buffers were updated in `gdb-stopped'."
399 :type '(repeat function)
400 :group 'gdb
401 :version "23.2"
402 :link '(info-link "(gdb)GDB/MI Async Records"))
403
404 (defcustom gdb-switch-when-another-stopped t
405 "When nil, Emacs won't switch to stopped thread if some other
406 stopped thread is already selected."
407 :type 'boolean
408 :group 'gdb-non-stop
409 :version "23.2")
410
411 (defcustom gdb-stack-buffer-locations t
412 "Show file information or library names in stack buffers."
413 :type 'boolean
414 :group 'gdb-buffers
415 :version "23.2")
416
417 (defcustom gdb-stack-buffer-addresses nil
418 "Show frame addresses in stack buffers."
419 :type 'boolean
420 :group 'gdb-buffers
421 :version "23.2")
422
423 (defcustom gdb-thread-buffer-verbose-names t
424 "Show long thread names in threads buffer."
425 :type 'boolean
426 :group 'gdb-buffers
427 :version "23.2")
428
429 (defcustom gdb-thread-buffer-arguments t
430 "Show function arguments in threads buffer."
431 :type 'boolean
432 :group 'gdb-buffers
433 :version "23.2")
434
435 (defcustom gdb-thread-buffer-locations t
436 "Show file information or library names in threads buffer."
437 :type 'boolean
438 :group 'gdb-buffers
439 :version "23.2")
440
441 (defcustom gdb-thread-buffer-addresses nil
442 "Show addresses for thread frames in threads buffer."
443 :type 'boolean
444 :group 'gdb-buffers
445 :version "23.2")
446
447 (defcustom gdb-show-threads-by-default nil
448 "Show threads list buffer instead of breakpoints list by
449 default."
450 :type 'boolean
451 :group 'gdb-buffers
452 :version "23.2")
453
454 (defvar gdb-debug-log nil
455 "List of commands sent to and replies received from GDB.
456 Most recent commands are listed first. This list stores only the last
457 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
458
459 ;;;###autoload
460 (defcustom gdb-enable-debug nil
461 "Non-nil means record the process input and output in `gdb-debug-log'."
462 :type 'boolean
463 :group 'gdb
464 :version "22.1")
465
466 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
467 "Shell command for generating a list of defined macros in a source file.
468 This list is used to display the #define directive associated
469 with an identifier as a tooltip. It works in a debug session with
470 GDB, when `gud-tooltip-mode' is t.
471
472 Set `gdb-cpp-define-alist-flags' for any include paths or
473 predefined macros."
474 :type 'string
475 :group 'gdb
476 :version "22.1")
477
478 (defcustom gdb-cpp-define-alist-flags ""
479 "Preprocessor flags for `gdb-cpp-define-alist-program'."
480 :type 'string
481 :group 'gdb
482 :version "22.1")
483
484 (defcustom gdb-create-source-file-list t
485 "Non-nil means create a list of files from which the executable was built.
486 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
487 line for a long time when starting, possibly because your executable was
488 built from a large number of files. This allows quicker initialization
489 but means that these files are not automatically enabled for debugging,
490 e.g., you won't be able to click in the fringe to set a breakpoint until
491 execution has already stopped there."
492 :type 'boolean
493 :group 'gdb
494 :version "23.1")
495
496 (defcustom gdb-show-main nil
497 "Non-nil means display source file containing the main routine at startup.
498 Also display the main routine in the disassembly buffer if present."
499 :type 'boolean
500 :group 'gdb
501 :version "22.1")
502
503 (defun gdb-force-mode-line-update (status)
504 (let ((buffer gud-comint-buffer))
505 (if (and buffer (buffer-name buffer))
506 (with-current-buffer buffer
507 (setq mode-line-process
508 (format ":%s [%s]"
509 (process-status (get-buffer-process buffer)) status))
510 ;; Force mode line redisplay soon.
511 (force-mode-line-update)))))
512
513 (defun gdb-enable-debug (arg)
514 "Toggle logging of transaction between Emacs and Gdb.
515 The log is stored in `gdb-debug-log' as an alist with elements
516 whose cons is send, send-item or recv and whose cdr is the string
517 being transferred. This list may grow up to a size of
518 `gdb-debug-log-max' after which the oldest element (at the end of
519 the list) is deleted every time a new one is added (at the front)."
520 (interactive "P")
521 (setq gdb-enable-debug
522 (if (null arg)
523 (not gdb-enable-debug)
524 (> (prefix-numeric-value arg) 0)))
525 (message (format "Logging of transaction %sabled"
526 (if gdb-enable-debug "en" "dis"))))
527
528 ;; These two are used for menu and toolbar
529 (defun gdb-control-all-threads ()
530 "Switch to non-stop/A mode."
531 (interactive)
532 (setq gdb-gud-control-all-threads t)
533 ;; Actually forcing the tool-bar to update.
534 (force-mode-line-update)
535 (message "Now in non-stop/A mode."))
536
537 (defun gdb-control-current-thread ()
538 "Switch to non-stop/T mode."
539 (interactive)
540 (setq gdb-gud-control-all-threads nil)
541 ;; Actually forcing the tool-bar to update.
542 (force-mode-line-update)
543 (message "Now in non-stop/T mode."))
544
545 (defun gdb-find-watch-expression ()
546 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
547 (varnum (car var)) expr array)
548 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
549 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
550 (component-list (split-string (match-string 2 varnum) "\\." t)))
551 (setq expr (nth 1 var1))
552 (setq varnumlet (car var1))
553 (dolist (component component-list)
554 (setq var2 (assoc varnumlet gdb-var-list))
555 (setq expr (concat expr
556 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
557 (concat "[" component "]")
558 (concat "." component))))
559 (setq varnumlet (concat varnumlet "." component)))
560 expr)))
561
562 ;; noall is used for commands which don't take --all, but only
563 ;; --thread.
564 (defun gdb-gud-context-command (command &optional noall)
565 "When `gdb-non-stop' is t, add --thread option to COMMAND if
566 `gdb-gud-control-all-threads' is nil and --all option otherwise.
567 If NOALL is t, always add --thread option no matter what
568 `gdb-gud-control-all-threads' value is.
569
570 When `gdb-non-stop' is nil, return COMMAND unchanged."
571 (if gdb-non-stop
572 (if (and gdb-gud-control-all-threads
573 (not noall)
574 (string-equal gdb-version "7.0+"))
575 (concat command " --all ")
576 (gdb-current-context-command command))
577 command))
578
579 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
580 "`gud-call' wrapper which adds --thread/--all options between
581 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
582
583 NOARG must be t when this macro is used outside `gud-def'"
584 `(gud-call
585 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
586 ,(when (not noarg) 'arg)))
587
588 ;;;###autoload
589 (defun gdb (command-line)
590 "Run gdb on program FILE in buffer *gud-FILE*.
591 The directory containing FILE becomes the initial working directory
592 and source-file directory for your debugger.
593
594 If `gdb-many-windows' is nil (the default value) then gdb just
595 pops up the GUD buffer unless `gdb-show-main' is t. In this case
596 it starts with two windows: one displaying the GUD buffer and the
597 other with the source file with the main routine of the inferior.
598
599 If `gdb-many-windows' is t, regardless of the value of
600 `gdb-show-main', the layout below will appear. Keybindings are
601 shown in some of the buffers.
602
603 Watch expressions appear in the speedbar/slowbar.
604
605 The following commands help control operation :
606
607 `gdb-many-windows' - Toggle the number of windows gdb uses.
608 `gdb-restore-windows' - To restore the window layout.
609
610 See Info node `(emacs)GDB Graphical Interface' for a more
611 detailed description of this mode.
612
613
614 +----------------------------------------------------------------------+
615 | GDB Toolbar |
616 +-----------------------------------+----------------------------------+
617 | GUD buffer (I/O of GDB) | Locals buffer |
618 | | |
619 | | |
620 | | |
621 +-----------------------------------+----------------------------------+
622 | Source buffer | I/O buffer (of debugged program) |
623 | | (comint-mode) |
624 | | |
625 | | |
626 | | |
627 | | |
628 | | |
629 | | |
630 +-----------------------------------+----------------------------------+
631 | Stack buffer | Breakpoints buffer |
632 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
633 | | RET gdb-goto-breakpoint |
634 | | D gdb-delete-breakpoint |
635 +-----------------------------------+----------------------------------+"
636 ;;
637 (interactive (list (gud-query-cmdline 'gdb)))
638
639 (when (and gud-comint-buffer
640 (buffer-name gud-comint-buffer)
641 (get-buffer-process gud-comint-buffer)
642 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
643 (gdb-restore-windows)
644 (error
645 "Multiple debugging requires restarting in text command mode"))
646 ;;
647 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
648 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
649 (setq comint-input-sender 'gdb-send)
650
651 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
652 "Set temporary breakpoint at current line.")
653 (gud-def gud-jump
654 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
655 "\C-j" "Set execution address to current line.")
656
657 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
658 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
659 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
660 (gud-def gud-pstar "print* %e" nil
661 "Evaluate C dereferenced pointer expression at point.")
662
663 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
664 "\C-s"
665 "Step one source line with display.")
666 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
667 "\C-i"
668 "Step one instruction with display.")
669 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
670 "\C-n"
671 "Step one line (skip functions).")
672 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
673 nil
674 "Step one instruction (skip functions).")
675 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
676 "\C-r"
677 "Continue with display.")
678 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
679 "\C-f"
680 "Finish executing current function.")
681 (gud-def gud-run "-exec-run"
682 nil
683 "Run the program.")
684
685 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
686 (gud-call "break %f:%l" arg)
687 (save-excursion
688 (beginning-of-line)
689 (forward-char 2)
690 (gud-call "break *%a" arg)))
691 "\C-b" "Set breakpoint at current line or address.")
692
693 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
694 (gud-call "clear %f:%l" arg)
695 (save-excursion
696 (beginning-of-line)
697 (forward-char 2)
698 (gud-call "clear *%a" arg)))
699 "\C-d" "Remove breakpoint at current line or address.")
700
701 ;; -exec-until doesn't support --all yet
702 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
703 (gud-call "-exec-until %f:%l" arg)
704 (save-excursion
705 (beginning-of-line)
706 (forward-char 2)
707 (gud-call "-exec-until *%a" arg)))
708 "\C-u" "Continue to current line or address.")
709 ;; TODO Why arg here?
710 (gud-def
711 gud-go (gud-call (if gdb-active-process
712 (gdb-gud-context-command "-exec-continue")
713 "-exec-run") arg)
714 nil "Start or continue execution.")
715
716 ;; For debugging Emacs only.
717 (gud-def gud-pp
718 (gud-call
719 (concat
720 "pp1 " (if (eq (buffer-local-value
721 'major-mode (window-buffer)) 'speedbar-mode)
722 (gdb-find-watch-expression) "%e")) arg)
723 nil "Print the Emacs s-expression.")
724
725 (define-key gud-minor-mode-map [left-margin mouse-1]
726 'gdb-mouse-set-clear-breakpoint)
727 (define-key gud-minor-mode-map [left-fringe mouse-1]
728 'gdb-mouse-set-clear-breakpoint)
729 (define-key gud-minor-mode-map [left-margin C-mouse-1]
730 'gdb-mouse-toggle-breakpoint-margin)
731 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
732 'gdb-mouse-toggle-breakpoint-fringe)
733
734 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
735 'gdb-mouse-until)
736 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
737 'gdb-mouse-until)
738 (define-key gud-minor-mode-map [left-margin mouse-3]
739 'gdb-mouse-until)
740 (define-key gud-minor-mode-map [left-fringe mouse-3]
741 'gdb-mouse-until)
742
743 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
744 'gdb-mouse-jump)
745 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
746 'gdb-mouse-jump)
747 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
748 'gdb-mouse-jump)
749 (define-key gud-minor-mode-map [left-margin C-mouse-3]
750 'gdb-mouse-jump)
751
752 (local-set-key "\C-i" 'gud-gdb-complete-command)
753 (setq gdb-first-prompt t)
754 (setq gud-running nil)
755
756 (gdb-update)
757
758 (run-hooks 'gdb-mode-hook))
759
760 (defun gdb-init-1 ()
761 ;; (re-)initialise
762 (setq gdb-selected-frame nil
763 gdb-frame-number nil
764 gdb-thread-number nil
765 gdb-var-list nil
766 gdb-pending-triggers nil
767 gdb-output-sink 'user
768 gdb-location-alist nil
769 gdb-source-file-list nil
770 gdb-last-command nil
771 gdb-token-number 0
772 gdb-handler-alist '()
773 gdb-handler-number nil
774 gdb-prompt-name nil
775 gdb-first-done-or-error t
776 gdb-buffer-fringe-width (car (window-fringes))
777 gdb-debug-log nil
778 gdb-source-window nil
779 gdb-inferior-status nil
780 gdb-continuation nil
781 gdb-buf-publisher '()
782 gdb-threads-list '()
783 gdb-breakpoints-list '()
784 gdb-register-names '()
785 gdb-non-stop gdb-non-stop-setting)
786 ;;
787 (setq gdb-buffer-type 'gdbmi)
788 ;;
789 (gdb-force-mode-line-update
790 (propertize "initializing..." 'face font-lock-variable-name-face))
791
792 (gdb-get-buffer-create 'gdb-inferior-io)
793 (gdb-clear-inferior-io)
794 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
795 (gdb-input
796 ;; Needs GDB 6.4 onwards
797 (list (concat "-inferior-tty-set "
798 (or
799 ;; The process can run on a remote host.
800 (process-get (get-process "gdb-inferior") 'remote-tty)
801 (process-tty-name (get-process "gdb-inferior"))))
802 'ignore))
803 (if (eq window-system 'w32)
804 (gdb-input (list "-gdb-set new-console off" 'ignore)))
805 (gdb-input (list "-gdb-set height 0" 'ignore))
806
807 (when gdb-non-stop
808 (gdb-input (list "-gdb-set non-stop 1" 'gdb-non-stop-handler)))
809
810 ;; find source file and compilation directory here
811 (gdb-input
812 ; Needs GDB 6.2 onwards.
813 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
814 (if gdb-create-source-file-list
815 (gdb-input
816 ; Needs GDB 6.0 onwards.
817 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
818 (gdb-input
819 (list "-gdb-show prompt" 'gdb-get-prompt)))
820
821 (defun gdb-non-stop-handler ()
822 (goto-char (point-min))
823 (if (re-search-forward "No symbol" nil t)
824 (progn
825 (message "This version of GDB doesn't support non-stop mode. Turning it off.")
826 (setq gdb-non-stop nil)
827 (setq gdb-version "pre-7.0"))
828 (setq gdb-version "7.0+")
829 (gdb-input (list "-gdb-set target-async 1" 'ignore))
830 (gdb-input (list "-enable-pretty-printing" 'ignore))))
831
832 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
833
834 (defun gdb-create-define-alist ()
835 "Create an alist of #define directives for GUD tooltips."
836 (let* ((file (buffer-file-name))
837 (output
838 (with-output-to-string
839 (with-current-buffer standard-output
840 (and file
841 (file-exists-p file)
842 ;; call-process doesn't work with remote file names.
843 (not (file-remote-p default-directory))
844 (call-process shell-file-name file
845 (list t nil) nil "-c"
846 (concat gdb-cpp-define-alist-program " "
847 gdb-cpp-define-alist-flags))))))
848 (define-list (split-string output "\n" t))
849 (name))
850 (setq gdb-define-alist nil)
851 (dolist (define define-list)
852 (setq name (nth 1 (split-string define "[( ]")))
853 (push (cons name define) gdb-define-alist))))
854
855 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
856 (defvar tooltip-use-echo-area)
857
858 (defun gdb-tooltip-print (expr)
859 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
860 (goto-char (point-min))
861 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
862 (tooltip-show
863 (concat expr " = " (read (match-string 1)))
864 (or gud-tooltip-echo-area tooltip-use-echo-area
865 (not (display-graphic-p)))))))
866
867 ;; If expr is a macro for a function don't print because of possible dangerous
868 ;; side-effects. Also printing a function within a tooltip generates an
869 ;; unexpected starting annotation (phase error).
870 (defun gdb-tooltip-print-1 (expr)
871 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
872 (goto-char (point-min))
873 (if (search-forward "expands to: " nil t)
874 (unless (looking-at "\\S-+.*(.*).*")
875 (gdb-input
876 (list (concat "-data-evaluate-expression " expr)
877 `(lambda () (gdb-tooltip-print ,expr))))))))
878
879 (defun gdb-init-buffer ()
880 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
881 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
882 (when gud-tooltip-mode
883 (make-local-variable 'gdb-define-alist)
884 (gdb-create-define-alist)
885 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
886
887 (defmacro gdb-if-arrow (arrow-position &rest body)
888 `(if ,arrow-position
889 (let ((buffer (marker-buffer ,arrow-position)) (line))
890 (if (equal buffer (window-buffer (posn-window end)))
891 (with-current-buffer buffer
892 (when (or (equal start end)
893 (equal (posn-point start)
894 (marker-position ,arrow-position)))
895 ,@body))))))
896
897 (defun gdb-mouse-until (event)
898 "Continue running until a source line past the current line.
899 The destination source line can be selected either by clicking
900 with mouse-3 on the fringe/margin or dragging the arrow
901 with mouse-1 (default bindings)."
902 (interactive "e")
903 (let ((start (event-start event))
904 (end (event-end event)))
905 (gdb-if-arrow gud-overlay-arrow-position
906 (setq line (line-number-at-pos (posn-point end)))
907 (gud-call (concat "until " (number-to-string line))))
908 (gdb-if-arrow gdb-disassembly-position
909 (save-excursion
910 (goto-char (point-min))
911 (forward-line (1- (line-number-at-pos (posn-point end))))
912 (forward-char 2)
913 (gud-call (concat "until *%a"))))))
914
915 (defun gdb-mouse-jump (event)
916 "Set execution address/line.
917 The destination source line can be selected either by clicking with C-mouse-3
918 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
919 Unlike `gdb-mouse-until' the destination address can be before the current
920 line, and no execution takes place."
921 (interactive "e")
922 (let ((start (event-start event))
923 (end (event-end event)))
924 (gdb-if-arrow gud-overlay-arrow-position
925 (setq line (line-number-at-pos (posn-point end)))
926 (progn
927 (gud-call (concat "tbreak " (number-to-string line)))
928 (gud-call (concat "jump " (number-to-string line)))))
929 (gdb-if-arrow gdb-disassembly-position
930 (save-excursion
931 (goto-char (point-min))
932 (forward-line (1- (line-number-at-pos (posn-point end))))
933 (forward-char 2)
934 (progn
935 (gud-call (concat "tbreak *%a"))
936 (gud-call (concat "jump *%a")))))))
937
938 (defcustom gdb-show-changed-values t
939 "If non-nil change the face of out of scope variables and changed values.
940 Out of scope variables are suppressed with `shadow' face.
941 Changed values are highlighted with the face `font-lock-warning-face'."
942 :type 'boolean
943 :group 'gdb
944 :version "22.1")
945
946 (defcustom gdb-max-children 40
947 "Maximum number of children before expansion requires confirmation."
948 :type 'integer
949 :group 'gdb
950 :version "22.1")
951
952 (defcustom gdb-delete-out-of-scope t
953 "If non-nil delete watch expressions automatically when they go out of scope."
954 :type 'boolean
955 :group 'gdb
956 :version "22.2")
957
958 (defcustom gdb-speedbar-auto-raise nil
959 "If non-nil raise speedbar every time display of watch expressions is\
960 updated."
961 :type 'boolean
962 :group 'gdb
963 :version "22.1")
964
965 (defcustom gdb-use-colon-colon-notation nil
966 "If non-nil use FUN::VAR format to display variables in the speedbar."
967 :type 'boolean
968 :group 'gdb
969 :version "22.1")
970
971 (defun gdb-speedbar-auto-raise (arg)
972 "Toggle automatic raising of the speedbar for watch expressions.
973 With prefix argument ARG, automatically raise speedbar if ARG is
974 positive, otherwise don't automatically raise it."
975 (interactive "P")
976 (setq gdb-speedbar-auto-raise
977 (if (null arg)
978 (not gdb-speedbar-auto-raise)
979 (> (prefix-numeric-value arg) 0)))
980 (message (format "Auto raising %sabled"
981 (if gdb-speedbar-auto-raise "en" "dis"))))
982
983 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
984 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
985
986 (declare-function tooltip-identifier-from-point "tooltip" (point))
987
988 (defun gud-watch (&optional arg event)
989 "Watch expression at point.
990 With arg, enter name of variable to be watched in the minibuffer."
991 (interactive (list current-prefix-arg last-input-event))
992 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
993 (if (eq minor-mode 'gdbmi)
994 (progn
995 (if event (posn-set-point (event-end event)))
996 (require 'tooltip)
997 (save-selected-window
998 (let ((expr
999 (if arg
1000 (completing-read "Name of variable: "
1001 'gud-gdb-complete-command)
1002 (if (and transient-mark-mode mark-active)
1003 (buffer-substring (region-beginning) (region-end))
1004 (concat (if (eq major-mode 'gdb-registers-mode) "$")
1005 (tooltip-identifier-from-point (point)))))))
1006 (set-text-properties 0 (length expr) nil expr)
1007 (gdb-input
1008 (list (concat"-var-create - * " expr "")
1009 `(lambda () (gdb-var-create-handler ,expr)))))))
1010 (message "gud-watch is a no-op in this mode."))))
1011
1012 (defun gdb-var-create-handler (expr)
1013 (let* ((result (gdb-json-partial-output)))
1014 (if (not (bindat-get-field result 'msg))
1015 (let ((var
1016 (list (bindat-get-field result 'name)
1017 (if (and (string-equal gdb-current-language "c")
1018 gdb-use-colon-colon-notation gdb-selected-frame)
1019 (setq expr (concat gdb-selected-frame "::" expr))
1020 expr)
1021 (bindat-get-field result 'numchild)
1022 (bindat-get-field result 'type)
1023 (bindat-get-field result 'value)
1024 nil
1025 (bindat-get-field result 'has_more)
1026 gdb-frame-address)))
1027 (push var gdb-var-list)
1028 (speedbar 1)
1029 (unless (string-equal
1030 speedbar-initial-expansion-list-name "GUD")
1031 (speedbar-change-initial-expansion-list "GUD")))
1032 (message-box "No symbol \"%s\" in current context." expr))))
1033
1034 (defun gdb-speedbar-update ()
1035 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1036 (not (gdb-pending-p 'gdb-speedbar-timer)))
1037 ;; Dummy command to update speedbar even when idle.
1038 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
1039 ;; Keep gdb-pending-triggers non-nil till end.
1040 (gdb-add-pending 'gdb-speedbar-timer)))
1041
1042 (defun gdb-speedbar-timer-fn ()
1043 (if gdb-speedbar-auto-raise
1044 (raise-frame speedbar-frame))
1045 (gdb-delete-pending 'gdb-speedbar-timer)
1046 (speedbar-timer-fn))
1047
1048 (defun gdb-var-evaluate-expression-handler (varnum changed)
1049 (goto-char (point-min))
1050 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1051 (let ((var (assoc varnum gdb-var-list)))
1052 (when var
1053 (if changed (setcar (nthcdr 5 var) 'changed))
1054 (setcar (nthcdr 4 var) (read (match-string 1)))))
1055 (gdb-speedbar-update))
1056
1057 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1058 (defun gdb-var-list-children (varnum)
1059 (gdb-input
1060 (list (concat "-var-update " varnum) 'ignore))
1061 (gdb-input
1062 (list (concat "-var-list-children --all-values "
1063 varnum)
1064 `(lambda () (gdb-var-list-children-handler ,varnum)))))
1065
1066 (defun gdb-var-list-children-handler (varnum)
1067 (let* ((var-list nil)
1068 (output (bindat-get-field (gdb-json-partial-output "child")))
1069 (children (bindat-get-field output 'children)))
1070 (catch 'child-already-watched
1071 (dolist (var gdb-var-list)
1072 (if (string-equal varnum (car var))
1073 (progn
1074 ;; With dynamic varobjs numchild may have increased.
1075 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1076 (push var var-list)
1077 (dolist (child children)
1078 (let ((varchild (list (bindat-get-field child 'name)
1079 (bindat-get-field child 'exp)
1080 (bindat-get-field child 'numchild)
1081 (bindat-get-field child 'type)
1082 (bindat-get-field child 'value)
1083 nil
1084 (bindat-get-field child 'has_more))))
1085 (if (assoc (car varchild) gdb-var-list)
1086 (throw 'child-already-watched nil))
1087 (push varchild var-list))))
1088 (push var var-list)))
1089 (setq gdb-var-list (nreverse var-list))))
1090 (gdb-speedbar-update))
1091
1092 (defun gdb-var-set-format (format)
1093 "Set the output format for a variable displayed in the speedbar."
1094 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1095 (varnum (car var)))
1096 (gdb-input
1097 (list (concat "-var-set-format " varnum " " format) 'ignore))
1098 (gdb-var-update)))
1099
1100 (defun gdb-var-delete-1 (var varnum)
1101 (gdb-input
1102 (list (concat "-var-delete " varnum) 'ignore))
1103 (setq gdb-var-list (delq var gdb-var-list))
1104 (dolist (varchild gdb-var-list)
1105 (if (string-match (concat (car var) "\\.") (car varchild))
1106 (setq gdb-var-list (delq varchild gdb-var-list)))))
1107
1108 (defun gdb-var-delete ()
1109 "Delete watch expression at point from the speedbar."
1110 (interactive)
1111 (let ((text (speedbar-line-text)))
1112 (string-match "\\(\\S-+\\)" text)
1113 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1114 (varnum (car var)))
1115 (if (string-match "\\." (car var))
1116 (message-box "Can only delete a root expression")
1117 (gdb-var-delete-1 var varnum)))))
1118
1119 (defun gdb-var-delete-children (varnum)
1120 "Delete children of variable object at point from the speedbar."
1121 (gdb-input
1122 (list (concat "-var-delete -c " varnum) 'ignore)))
1123
1124 (defun gdb-edit-value (text token indent)
1125 "Assign a value to a variable displayed in the speedbar."
1126 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1127 (varnum (car var)) (value))
1128 (setq value (read-string "New value: "))
1129 (gdb-input
1130 (list (concat "-var-assign " varnum " " value)
1131 `(lambda () (gdb-edit-value-handler ,value))))))
1132
1133 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1134
1135 (defun gdb-edit-value-handler (value)
1136 (goto-char (point-min))
1137 (if (re-search-forward gdb-error-regexp nil t)
1138 (message-box "Invalid number or expression (%s)" value)))
1139
1140 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1141 (defun gdb-var-update ()
1142 (if (not (gdb-pending-p 'gdb-var-update))
1143 (gdb-input
1144 (list "-var-update --all-values *" 'gdb-var-update-handler)))
1145 (gdb-add-pending 'gdb-var-update))
1146
1147 (defun gdb-var-update-handler ()
1148 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1149 (dolist (var gdb-var-list)
1150 (setcar (nthcdr 5 var) nil))
1151 (let ((temp-var-list gdb-var-list))
1152 (dolist (change changelist)
1153 (let* ((varnum (bindat-get-field change 'name))
1154 (var (assoc varnum gdb-var-list))
1155 (new-num (bindat-get-field change 'new_num_children)))
1156 (when var
1157 (let ((scope (bindat-get-field change 'in_scope))
1158 (has-more (bindat-get-field change 'has_more)))
1159 (cond ((string-equal scope "false")
1160 (if gdb-delete-out-of-scope
1161 (gdb-var-delete-1 var varnum)
1162 (setcar (nthcdr 5 var) 'out-of-scope)))
1163 ((string-equal scope "true")
1164 (setcar (nthcdr 6 var) has-more)
1165 (when (and (or (not has-more)
1166 (string-equal has-more "0"))
1167 (not new-num)
1168 (string-equal (nth 2 var) "0"))
1169 (setcar (nthcdr 4 var)
1170 (bindat-get-field change 'value))
1171 (setcar (nthcdr 5 var) 'changed)))
1172 ((string-equal scope "invalid")
1173 (gdb-var-delete-1 var varnum)))))
1174 (let ((var-list nil) var1
1175 (children (bindat-get-field change 'new_children)))
1176 (if new-num
1177 (progn
1178 (setq var1 (pop temp-var-list))
1179 (while var1
1180 (if (string-equal varnum (car var1))
1181 (let ((new (string-to-number new-num))
1182 (previous (string-to-number (nth 2 var1))))
1183 (setcar (nthcdr 2 var1) new-num)
1184 (push var1 var-list)
1185 (cond ((> new previous)
1186 ;; Add new children to list.
1187 (dotimes (dummy previous)
1188 (push (pop temp-var-list) var-list))
1189 (dolist (child children)
1190 (let ((varchild
1191 (list (bindat-get-field child 'name)
1192 (bindat-get-field child 'exp)
1193 (bindat-get-field child 'numchild)
1194 (bindat-get-field child 'type)
1195 (bindat-get-field child 'value)
1196 'changed
1197 (bindat-get-field child 'has_more))))
1198 (push varchild var-list))))
1199 ;; Remove deleted children from list.
1200 ((< new previous)
1201 (dotimes (dummy new)
1202 (push (pop temp-var-list) var-list))
1203 (dotimes (dummy (- previous new))
1204 (pop temp-var-list)))))
1205 (push var1 var-list))
1206 (setq var1 (pop temp-var-list)))
1207 (setq gdb-var-list (nreverse var-list)))))))))
1208 (setq gdb-pending-triggers
1209 (delq 'gdb-var-update gdb-pending-triggers))
1210 (gdb-speedbar-update))
1211
1212 (defun gdb-speedbar-expand-node (text token indent)
1213 "Expand the node the user clicked on.
1214 TEXT is the text of the button we clicked on, a + or - item.
1215 TOKEN is data related to this node.
1216 INDENT is the current indentation depth."
1217 (cond ((string-match "+" text) ;expand this node
1218 (let* ((var (assoc token gdb-var-list))
1219 (expr (nth 1 var)) (children (nth 2 var)))
1220 (if (or (<= (string-to-number children) gdb-max-children)
1221 (y-or-n-p
1222 (format "%s has %s children. Continue? " expr children)))
1223 (gdb-var-list-children token))))
1224 ((string-match "-" text) ;contract this node
1225 (dolist (var gdb-var-list)
1226 (if (string-match (concat token "\\.") (car var))
1227 (setq gdb-var-list (delq var gdb-var-list))))
1228 (gdb-var-delete-children token)
1229 (speedbar-change-expand-button-char ?+)
1230 (speedbar-delete-subblock indent))
1231 (t (error "Ooops... not sure what to do")))
1232 (speedbar-center-buffer-smartly))
1233
1234 (defun gdb-get-target-string ()
1235 (with-current-buffer gud-comint-buffer
1236 gud-target-name))
1237 \f
1238
1239 ;;
1240 ;; gdb buffers.
1241 ;;
1242 ;; Each buffer has a TYPE -- a symbol that identifies the function
1243 ;; of that particular buffer.
1244 ;;
1245 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1246 ;; is constructed specially.
1247 ;;
1248 ;; Others are constructed by gdb-get-buffer-create and
1249 ;; named according to the rules set forth in the gdb-buffer-rules
1250
1251 (defvar gdb-buffer-rules '())
1252
1253 (defun gdb-rules-name-maker (rules-entry)
1254 (cadr rules-entry))
1255 (defun gdb-rules-buffer-mode (rules-entry)
1256 (nth 2 rules-entry))
1257 (defun gdb-rules-update-trigger (rules-entry)
1258 (nth 3 rules-entry))
1259
1260 (defun gdb-update-buffer-name ()
1261 "Rename current buffer according to name-maker associated with
1262 it in `gdb-buffer-rules'."
1263 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1264 gdb-buffer-rules))))
1265 (when f (rename-buffer (funcall f)))))
1266
1267 (defun gdb-current-buffer-rules ()
1268 "Get `gdb-buffer-rules' entry for current buffer type."
1269 (assoc gdb-buffer-type gdb-buffer-rules))
1270
1271 (defun gdb-current-buffer-thread ()
1272 "Get thread object of current buffer from `gdb-threads-list'.
1273
1274 When current buffer is not bound to any thread, return main
1275 thread."
1276 (cdr (assoc gdb-thread-number gdb-threads-list)))
1277
1278 (defun gdb-current-buffer-frame ()
1279 "Get current stack frame object for thread of current buffer."
1280 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1281
1282 (defun gdb-buffer-type (buffer)
1283 "Get value of `gdb-buffer-type' for BUFFER."
1284 (with-current-buffer buffer
1285 gdb-buffer-type))
1286
1287 (defun gdb-buffer-shows-main-thread-p ()
1288 "Return t if current GDB buffer shows main selected thread and
1289 is not bound to it."
1290 (current-buffer)
1291 (not (local-variable-p 'gdb-thread-number)))
1292
1293 (defun gdb-get-buffer (buffer-type &optional thread)
1294 "Get a specific GDB buffer.
1295
1296 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1297 and `gdb-thread-number' (if provided) must be equal to THREAD."
1298 (catch 'found
1299 (dolist (buffer (buffer-list) nil)
1300 (with-current-buffer buffer
1301 (when (and (eq gdb-buffer-type buffer-type)
1302 (or (not thread)
1303 (equal gdb-thread-number thread)))
1304 (throw 'found buffer))))))
1305
1306 (defun gdb-get-buffer-create (buffer-type &optional thread)
1307 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1308 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1309
1310 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1311 buffer-local variable of the new buffer.
1312
1313 Buffer mode and name are selected according to buffer type.
1314
1315 If buffer has trigger associated with it in `gdb-buffer-rules',
1316 this trigger is subscribed to `gdb-buf-publisher' and called with
1317 'update argument."
1318 (or (gdb-get-buffer buffer-type thread)
1319 (let ((rules (assoc buffer-type gdb-buffer-rules))
1320 (new (generate-new-buffer "limbo")))
1321 (with-current-buffer new
1322 (let ((mode (gdb-rules-buffer-mode rules))
1323 (trigger (gdb-rules-update-trigger rules)))
1324 (when mode (funcall mode))
1325 (setq gdb-buffer-type buffer-type)
1326 (when thread
1327 (set (make-local-variable 'gdb-thread-number) thread))
1328 (set (make-local-variable 'gud-minor-mode)
1329 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1330 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1331 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1332 (when trigger
1333 (gdb-add-subscriber gdb-buf-publisher
1334 (cons (current-buffer)
1335 (gdb-bind-function-to-buffer trigger (current-buffer))))
1336 (funcall trigger 'start))
1337 (current-buffer))))))
1338
1339 (defun gdb-bind-function-to-buffer (expr buffer)
1340 "Return a function which will evaluate EXPR in BUFFER."
1341 `(lambda (&rest args)
1342 (with-current-buffer ,buffer
1343 (apply ',expr args))))
1344
1345 ;; Used to define all gdb-frame-*-buffer functions except
1346 ;; `gdb-frame-io-buffer'
1347 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1348 "Define a function NAME which shows gdb BUFFER in a separate frame.
1349
1350 DOC is an optional documentation string."
1351 `(defun ,name (&optional thread)
1352 ,(when doc doc)
1353 (interactive)
1354 (let ((special-display-regexps (append special-display-regexps '(".*")))
1355 (special-display-frame-alist gdb-frame-parameters))
1356 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1357
1358 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1359 "Define a function NAME which shows gdb BUFFER.
1360
1361 DOC is an optional documentation string."
1362 `(defun ,name (&optional thread)
1363 ,(when doc doc)
1364 (interactive)
1365 (gdb-display-buffer
1366 (gdb-get-buffer-create ,buffer thread) t)))
1367
1368 ;; Used to display windows with thread-bound buffers
1369 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1370 split-horizontal)
1371 `(defun ,name (&optional thread)
1372 ,(when doc doc)
1373 (message thread)
1374 (gdb-preempt-existing-or-display-buffer
1375 (gdb-get-buffer-create ,buffer thread)
1376 ,split-horizontal)))
1377
1378 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1379 ;; at least one and possible more functions. The functions have these
1380 ;; roles in defining a buffer type:
1381 ;;
1382 ;; NAME - Return a name for this buffer type.
1383 ;;
1384 ;; The remaining function(s) are optional:
1385 ;;
1386 ;; MODE - called in a new buffer with no arguments, should establish
1387 ;; the proper mode for the buffer.
1388 ;;
1389
1390 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1391 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1392 (if binding
1393 (setcdr binding rules)
1394 (push (cons buffer-type rules)
1395 gdb-buffer-rules))))
1396
1397 (defun gdb-parent-mode ()
1398 "Generic mode to derive all other GDB buffer modes from."
1399 (kill-all-local-variables)
1400 (setq buffer-read-only t)
1401 (buffer-disable-undo)
1402 ;; Delete buffer from gdb-buf-publisher when it's killed
1403 ;; (if it has an associated update trigger)
1404 (add-hook
1405 'kill-buffer-hook
1406 (function
1407 (lambda ()
1408 (let ((trigger (gdb-rules-update-trigger
1409 (gdb-current-buffer-rules))))
1410 (when trigger
1411 (gdb-delete-subscriber
1412 gdb-buf-publisher
1413 ;; This should match gdb-add-subscriber done in
1414 ;; gdb-get-buffer-create
1415 (cons (current-buffer)
1416 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1417 nil t))
1418
1419 ;; Partial-output buffer : This accumulates output from a command executed on
1420 ;; behalf of emacs (rather than the user).
1421 ;;
1422 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1423 'gdb-partial-output-name)
1424
1425 (defun gdb-partial-output-name ()
1426 (concat " *partial-output-"
1427 (gdb-get-target-string)
1428 "*"))
1429
1430 \f
1431 (gdb-set-buffer-rules 'gdb-inferior-io
1432 'gdb-inferior-io-name
1433 'gdb-inferior-io-mode)
1434
1435 (defun gdb-inferior-io-name ()
1436 (concat "*input/output of "
1437 (gdb-get-target-string)
1438 "*"))
1439
1440 (defun gdb-display-io-buffer ()
1441 "Display IO of debugged program in a separate window."
1442 (interactive)
1443 (gdb-display-buffer
1444 (gdb-get-buffer-create 'gdb-inferior-io) t))
1445
1446 (defconst gdb-frame-parameters
1447 '((height . 14) (width . 80)
1448 (unsplittable . t)
1449 (tool-bar-lines . nil)
1450 (menu-bar-lines . nil)
1451 (minibuffer . nil)))
1452
1453 (defun gdb-frame-io-buffer ()
1454 "Display IO of debugged program in a new frame."
1455 (interactive)
1456 (let ((special-display-regexps (append special-display-regexps '(".*")))
1457 (special-display-frame-alist gdb-frame-parameters))
1458 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1459
1460 (defvar gdb-inferior-io-mode-map
1461 (let ((map (make-sparse-keymap)))
1462 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1463 (define-key map "\C-c\C-z" 'gdb-io-stop)
1464 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1465 (define-key map "\C-c\C-d" 'gdb-io-eof)
1466 (define-key map "\C-d" 'gdb-io-eof)
1467 map))
1468
1469 ;; We want to use comint because it has various nifty and familiar features.
1470 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1471 "Major mode for gdb inferior-io.
1472
1473 The following commands are available:
1474 \\{gdb-inferior-io-mode-map}"
1475
1476 :syntax-table nil :abbrev-table nil
1477
1478 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1479
1480 (defun gdb-inferior-filter (proc string)
1481 (unless (string-equal string "")
1482 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1483 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1484 (comint-output-filter proc string)))
1485
1486 (defun gdb-io-interrupt ()
1487 "Interrupt the program being debugged."
1488 (interactive)
1489 (interrupt-process
1490 (get-buffer-process gud-comint-buffer) comint-ptyp))
1491
1492 (defun gdb-io-quit ()
1493 "Send quit signal to the program being debugged."
1494 (interactive)
1495 (quit-process
1496 (get-buffer-process gud-comint-buffer) comint-ptyp))
1497
1498 (defun gdb-io-stop ()
1499 "Stop the program being debugged."
1500 (interactive)
1501 (stop-process
1502 (get-buffer-process gud-comint-buffer) comint-ptyp))
1503
1504 (defun gdb-io-eof ()
1505 "Send end-of-file to the program being debugged."
1506 (interactive)
1507 (process-send-eof
1508 (get-buffer-process gud-comint-buffer)))
1509
1510 (defun gdb-clear-inferior-io ()
1511 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1512 (erase-buffer)))
1513 \f
1514
1515 (defconst breakpoint-xpm-data
1516 "/* XPM */
1517 static char *magick[] = {
1518 /* columns rows colors chars-per-pixel */
1519 \"10 10 2 1\",
1520 \" c red\",
1521 \"+ c None\",
1522 /* pixels */
1523 \"+++ +++\",
1524 \"++ ++\",
1525 \"+ +\",
1526 \" \",
1527 \" \",
1528 \" \",
1529 \" \",
1530 \"+ +\",
1531 \"++ ++\",
1532 \"+++ +++\",
1533 };"
1534 "XPM data used for breakpoint icon.")
1535
1536 (defconst breakpoint-enabled-pbm-data
1537 "P1
1538 10 10\",
1539 0 0 0 0 1 1 1 1 0 0 0 0
1540 0 0 0 1 1 1 1 1 1 0 0 0
1541 0 0 1 1 1 1 1 1 1 1 0 0
1542 0 1 1 1 1 1 1 1 1 1 1 0
1543 0 1 1 1 1 1 1 1 1 1 1 0
1544 0 1 1 1 1 1 1 1 1 1 1 0
1545 0 1 1 1 1 1 1 1 1 1 1 0
1546 0 0 1 1 1 1 1 1 1 1 0 0
1547 0 0 0 1 1 1 1 1 1 0 0 0
1548 0 0 0 0 1 1 1 1 0 0 0 0"
1549 "PBM data used for enabled breakpoint icon.")
1550
1551 (defconst breakpoint-disabled-pbm-data
1552 "P1
1553 10 10\",
1554 0 0 1 0 1 0 1 0 0 0
1555 0 1 0 1 0 1 0 1 0 0
1556 1 0 1 0 1 0 1 0 1 0
1557 0 1 0 1 0 1 0 1 0 1
1558 1 0 1 0 1 0 1 0 1 0
1559 0 1 0 1 0 1 0 1 0 1
1560 1 0 1 0 1 0 1 0 1 0
1561 0 1 0 1 0 1 0 1 0 1
1562 0 0 1 0 1 0 1 0 1 0
1563 0 0 0 1 0 1 0 1 0 0"
1564 "PBM data used for disabled breakpoint icon.")
1565
1566 (defvar breakpoint-enabled-icon nil
1567 "Icon for enabled breakpoint in display margin.")
1568
1569 (defvar breakpoint-disabled-icon nil
1570 "Icon for disabled breakpoint in display margin.")
1571
1572 (declare-function define-fringe-bitmap "fringe.c"
1573 (bitmap bits &optional height width align))
1574
1575 (and (display-images-p)
1576 ;; Bitmap for breakpoint in fringe
1577 (define-fringe-bitmap 'breakpoint
1578 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1579 ;; Bitmap for gud-overlay-arrow in fringe
1580 (define-fringe-bitmap 'hollow-right-triangle
1581 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1582
1583 (defface breakpoint-enabled
1584 '((t
1585 :foreground "red1"
1586 :weight bold))
1587 "Face for enabled breakpoint icon in fringe."
1588 :group 'gdb)
1589
1590 (defface breakpoint-disabled
1591 '((((class color) (min-colors 88)) :foreground "grey70")
1592 ;; Ensure that on low-color displays that we end up something visible.
1593 (((class color) (min-colors 8) (background light))
1594 :foreground "black")
1595 (((class color) (min-colors 8) (background dark))
1596 :foreground "white")
1597 (((type tty) (class mono))
1598 :inverse-video t)
1599 (t :background "gray"))
1600 "Face for disabled breakpoint icon in fringe."
1601 :group 'gdb)
1602
1603 \f
1604 (defun gdb-send (proc string)
1605 "A comint send filter for gdb."
1606 (with-current-buffer gud-comint-buffer
1607 (let ((inhibit-read-only t))
1608 (remove-text-properties (point-min) (point-max) '(face))))
1609 ;; mimic <RET> key to repeat previous command in GDB
1610 (if (not (string= "" string))
1611 (setq gdb-last-command string)
1612 (if gdb-last-command (setq string gdb-last-command)))
1613 (if gdb-enable-debug
1614 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1615 (if (string-match "^-" string)
1616 ;; MI command
1617 (progn
1618 (setq gdb-first-done-or-error t)
1619 (process-send-string proc (concat string "\n")))
1620 ;; CLI command
1621 (if (string-match "\\\\$" string)
1622 (setq gdb-continuation (concat gdb-continuation string "\n"))
1623 (setq gdb-first-done-or-error t)
1624 (process-send-string proc (concat "-interpreter-exec console \""
1625 gdb-continuation string "\"\n"))
1626 (setq gdb-continuation nil))))
1627
1628 (defun gdb-input (item)
1629 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1630 (setq gdb-token-number (1+ gdb-token-number))
1631 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1632 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1633 (process-send-string (get-buffer-process gud-comint-buffer)
1634 (concat (car item) "\n")))
1635
1636 ;; NOFRAME is used for gud execution control commands
1637 (defun gdb-current-context-command (command)
1638 "Add --thread to gdb COMMAND when needed."
1639 (if (and gdb-thread-number
1640 (string-equal gdb-version "7.0+"))
1641 (concat command " --thread " gdb-thread-number)
1642 command))
1643
1644 (defun gdb-current-context-buffer-name (name)
1645 "Add thread information and asterisks to string NAME.
1646
1647 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1648 (concat "*" name
1649 (if (local-variable-p 'gdb-thread-number)
1650 (format " (bound to thread %s)" gdb-thread-number)
1651 "")
1652 "*"))
1653
1654 (defun gdb-current-context-mode-name (mode)
1655 "Add thread information to MODE which is to be used as
1656 `mode-name'."
1657 (concat mode
1658 (if gdb-thread-number
1659 (format " [thread %s]" gdb-thread-number)
1660 "")))
1661 \f
1662
1663 (defcustom gud-gdb-command-name "gdb -i=mi"
1664 "Default command to execute an executable under the GDB debugger."
1665 :type 'string
1666 :group 'gdb)
1667
1668 (defun gdb-resync()
1669 (setq gud-running nil)
1670 (setq gdb-output-sink 'user)
1671 (setq gdb-pending-triggers nil))
1672
1673 (defun gdb-update ()
1674 "Update buffers showing status of debug session."
1675 (when gdb-first-prompt
1676 (gdb-force-mode-line-update
1677 (propertize "initializing..." 'face font-lock-variable-name-face))
1678 (gdb-init-1)
1679 (setq gdb-first-prompt nil))
1680
1681 (gdb-get-main-selected-frame)
1682 ;; We may need to update gdb-threads-list so we can use
1683 (gdb-get-buffer-create 'gdb-threads-buffer)
1684 ;; gdb-break-list is maintained in breakpoints handler
1685 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1686
1687 (gdb-emit-signal gdb-buf-publisher 'update)
1688
1689 (gdb-get-changed-registers)
1690
1691 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1692 (dolist (var gdb-var-list)
1693 (setcar (nthcdr 5 var) nil))
1694 (gdb-var-update)))
1695
1696 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1697 ;; because we may need to update current gud-running value without
1698 ;; changing current thread (see gdb-running)
1699 (defun gdb-setq-thread-number (number)
1700 "Only this function must be used to change `gdb-thread-number'
1701 value to NUMBER, because `gud-running' and `gdb-frame-number'
1702 need to be updated appropriately when current thread changes."
1703 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1704 (unless (string-equal number "0") (setq gdb-thread-number number))
1705 (setq gdb-frame-number "0")
1706 (gdb-update-gud-running))
1707
1708 (defun gdb-update-gud-running ()
1709 "Set `gud-running' according to the state of current thread.
1710
1711 `gdb-frame-number' is set to 0 if current thread is now stopped.
1712
1713 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1714 cannot be reliably used to determine whether or not execution
1715 control buttons should be shown in menu or toolbar. Use
1716 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1717 instead.
1718
1719 For all-stop mode, thread information is unavailable while target
1720 is running."
1721 (let ((old-value gud-running))
1722 (setq gud-running
1723 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1724 "running"))
1725 ;; Set frame number to "0" when _current_ threads stops
1726 (when (and (gdb-current-buffer-thread)
1727 (not (eq gud-running old-value)))
1728 (setq gdb-frame-number "0"))))
1729
1730 (defun gdb-show-run-p ()
1731 "Return t if \"Run/continue\" should be shown on the toolbar."
1732 (or (not gdb-active-process)
1733 (and (or
1734 (not gdb-gud-control-all-threads)
1735 (not gdb-non-stop))
1736 (not gud-running))
1737 (and gdb-gud-control-all-threads
1738 (> gdb-stopped-threads-count 0))))
1739
1740 (defun gdb-show-stop-p ()
1741 "Return t if \"Stop\" should be shown on the toolbar."
1742 (or (and (or
1743 (not gdb-gud-control-all-threads)
1744 (not gdb-non-stop))
1745 gud-running)
1746 (and gdb-gud-control-all-threads
1747 (> gdb-running-threads-count 0))))
1748
1749 ;; GUD displays the selected GDB frame. This might might not be the current
1750 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1751 ;; visited breakpoint is, use that window.
1752 (defun gdb-display-source-buffer (buffer)
1753 (let* ((last-window (if gud-last-last-frame
1754 (get-buffer-window
1755 (gud-find-file (car gud-last-last-frame)))))
1756 (source-window (or last-window
1757 (if (and gdb-source-window
1758 (window-live-p gdb-source-window))
1759 gdb-source-window))))
1760 (when source-window
1761 (setq gdb-source-window source-window)
1762 (set-window-buffer source-window buffer))
1763 source-window))
1764
1765 (defun gdb-car< (a b)
1766 (< (car a) (car b)))
1767
1768 (defvar gdbmi-record-list
1769 '((gdb-gdb . "(gdb) \n")
1770 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1771 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1772 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1773 (gdb-console . "~\\(\".*?\"\\)\n")
1774 (gdb-internals . "&\\(\".*?\"\\)\n")
1775 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1776 (gdb-running . "\\*running,\\(.*?\n\\)")
1777 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1778 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1779 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1780 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1781 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1782
1783 (defun gud-gdbmi-marker-filter (string)
1784 "Filter GDB/MI output."
1785
1786 ;; Record transactions if logging is enabled.
1787 (when gdb-enable-debug
1788 (push (cons 'recv string) gdb-debug-log)
1789 (if (and gdb-debug-log-max
1790 (> (length gdb-debug-log) gdb-debug-log-max))
1791 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1792
1793 ;; Recall the left over gud-marker-acc from last time
1794 (setq gud-marker-acc (concat gud-marker-acc string))
1795
1796 ;; Start accumulating output for the GUD buffer
1797 (setq gdb-filter-output "")
1798 (let ((output-record) (output-record-list))
1799
1800 ;; Process all the complete markers in this chunk.
1801 (dolist (gdbmi-record gdbmi-record-list)
1802 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1803 (push (list (match-beginning 0)
1804 (car gdbmi-record)
1805 (match-string 1 gud-marker-acc)
1806 (match-string 2 gud-marker-acc)
1807 (match-end 0))
1808 output-record-list)
1809 (setq gud-marker-acc
1810 (concat (substring gud-marker-acc 0 (match-beginning 0))
1811 ;; Pad with spaces to preserve position.
1812 (make-string (length (match-string 0 gud-marker-acc)) 32)
1813 (substring gud-marker-acc (match-end 0))))))
1814
1815 (setq output-record-list (sort output-record-list 'gdb-car<))
1816
1817 (dolist (output-record output-record-list)
1818 (let ((record-type (cadr output-record))
1819 (arg1 (nth 2 output-record))
1820 (arg2 (nth 3 output-record)))
1821 (if (eq record-type 'gdb-error)
1822 (gdb-done-or-error arg2 arg1 'error)
1823 (if (eq record-type 'gdb-done)
1824 (gdb-done-or-error arg2 arg1 'done)
1825 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1826 ;; error message on internal stream. Don't print to GUD buffer.
1827 (unless (and (eq record-type 'gdb-internals)
1828 (string-equal (read arg1) "No registers.\n"))
1829 (funcall record-type arg1))))))
1830
1831 (setq gdb-output-sink 'user)
1832 ;; Remove padding.
1833 (string-match "^ *" gud-marker-acc)
1834 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1835
1836 gdb-filter-output))
1837
1838 (defun gdb-gdb (output-field))
1839
1840 (defun gdb-shell (output-field)
1841 (let ((gdb-output-sink gdb-output-sink))
1842 (setq gdb-filter-output
1843 (concat output-field gdb-filter-output))))
1844
1845 (defun gdb-ignored-notification (output-field))
1846
1847 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1848 (defun gdb-thread-created (output-field))
1849 (defun gdb-thread-exited (output-field)
1850 "Handle =thread-exited async record: unset `gdb-thread-number'
1851 if current thread exited and update threads list."
1852 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1853 (if (string= gdb-thread-number thread-id)
1854 (gdb-setq-thread-number nil))
1855 ;; When we continue current thread and it quickly exits,
1856 ;; gdb-pending-triggers left after gdb-running disallow us to
1857 ;; properly call -thread-info without --thread option. Thus we
1858 ;; need to use gdb-wait-for-pending.
1859 (gdb-wait-for-pending
1860 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1861
1862 (defun gdb-thread-selected (output-field)
1863 "Handler for =thread-selected MI output record.
1864
1865 Sets `gdb-thread-number' to new id."
1866 (let* ((result (gdb-json-string output-field))
1867 (thread-id (bindat-get-field result 'id)))
1868 (gdb-setq-thread-number thread-id)
1869 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1870 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1871 ;; as usually. Things happen to fast and second call (from
1872 ;; gdb-thread-selected handler) gets cut off by our beloved
1873 ;; gdb-pending-triggers.
1874 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1875 ;; body will get executed when `gdb-pending-triggers` is empty.
1876 (gdb-wait-for-pending
1877 (gdb-update))))
1878
1879 (defun gdb-running (output-field)
1880 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1881 ;; We reset gdb-frame-number to nil if current thread has gone
1882 ;; running. This can't be done in gdb-thread-list-handler-custom
1883 ;; because we need correct gdb-frame-number by the time
1884 ;; -thread-info command is sent.
1885 (when (or (string-equal thread-id "all")
1886 (string-equal thread-id gdb-thread-number))
1887 (setq gdb-frame-number nil)))
1888 (setq gdb-inferior-status "running")
1889 (gdb-force-mode-line-update
1890 (propertize gdb-inferior-status 'face font-lock-type-face))
1891 (when (not gdb-non-stop)
1892 (setq gud-running t))
1893 (setq gdb-active-process t)
1894 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1895
1896 (defun gdb-starting (output-field)
1897 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1898 (setq gdb-inferior-status "running")
1899 (gdb-force-mode-line-update
1900 (propertize gdb-inferior-status 'face font-lock-type-face))
1901 (setq gdb-active-process t)
1902 (setq gud-running t)
1903 ;; GDB doesn't seem to respond to -thread-info before first stop or
1904 ;; thread exit (even in non-stop mode), so this is useless.
1905 ;; Behaviour may change in the future.
1906 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1907
1908 ;; -break-insert -t didn't give a reason before gdb 6.9
1909
1910 (defun gdb-stopped (output-field)
1911 "Given the contents of *stopped MI async record, select new
1912 current thread and update GDB buffers."
1913 ;; Reason is available with target-async only
1914 (let* ((result (gdb-json-string output-field))
1915 (reason (bindat-get-field result 'reason))
1916 (thread-id (bindat-get-field result 'thread-id)))
1917
1918 ;; -data-list-register-names needs to be issued for any stopped
1919 ;; thread
1920 (when (not gdb-register-names)
1921 (gdb-input
1922 (list (concat "-data-list-register-names"
1923 (if (string-equal gdb-version "7.0+")
1924 (concat" --thread " thread-id)))
1925 'gdb-register-names-handler)))
1926
1927 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1928 ;;; because synchronous GDB doesn't give these fields with CLI.
1929 ;;; (when file
1930 ;;; (setq
1931 ;;; ;; Extract the frame position from the marker.
1932 ;;; gud-last-frame (cons file
1933 ;;; (string-to-number
1934 ;;; (match-string 6 gud-marker-acc)))))
1935
1936 (setq gdb-inferior-status (or reason "unknown"))
1937 (gdb-force-mode-line-update
1938 (propertize gdb-inferior-status 'face font-lock-warning-face))
1939 (if (string-equal reason "exited-normally")
1940 (setq gdb-active-process nil))
1941
1942 ;; Select new current thread.
1943
1944 ;; Don't switch if we have no reasons selected
1945 (when gdb-switch-reasons
1946 ;; Switch from another stopped thread only if we have
1947 ;; gdb-switch-when-another-stopped:
1948 (when (or gdb-switch-when-another-stopped
1949 (not (string= "stopped"
1950 (bindat-get-field (gdb-current-buffer-thread) 'state))))
1951 ;; Switch if current reason has been selected or we have no
1952 ;; reasons
1953 (if (or (eq gdb-switch-reasons t)
1954 (member reason gdb-switch-reasons))
1955 (when (not (string-equal gdb-thread-number thread-id))
1956 (message (concat "Switched to thread " thread-id))
1957 (gdb-setq-thread-number thread-id))
1958 (message (format "Thread %s stopped" thread-id)))))
1959
1960 ;; Print "(gdb)" to GUD console
1961 (when gdb-first-done-or-error
1962 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1963
1964 ;; In non-stop, we update information as soon as another thread gets
1965 ;; stopped
1966 (when (or gdb-first-done-or-error
1967 gdb-non-stop)
1968 ;; In all-stop this updates gud-running properly as well.
1969 (gdb-update)
1970 (setq gdb-first-done-or-error nil))
1971 (run-hook-with-args 'gdb-stopped-hooks result)))
1972
1973 ;; Remove the trimmings from log stream containing debugging messages
1974 ;; being produced by GDB's internals, use warning face and send to GUD
1975 ;; buffer.
1976 (defun gdb-internals (output-field)
1977 (setq gdb-filter-output
1978 (gdb-concat-output
1979 gdb-filter-output
1980 (let ((error-message
1981 (read output-field)))
1982 (put-text-property
1983 0 (length error-message)
1984 'face font-lock-warning-face
1985 error-message)
1986 error-message))))
1987
1988 ;; Remove the trimmings from the console stream and send to GUD buffer
1989 ;; (frontend MI commands should not print to this stream)
1990 (defun gdb-console (output-field)
1991 (setq gdb-filter-output
1992 (gdb-concat-output
1993 gdb-filter-output
1994 (read output-field))))
1995
1996 (defun gdb-done-or-error (output-field token-number type)
1997 (if (string-equal token-number "")
1998 ;; Output from command entered by user
1999 (progn
2000 (setq gdb-output-sink 'user)
2001 (setq token-number nil)
2002 ;; MI error - send to minibuffer
2003 (when (eq type 'error)
2004 ;; Skip "msg=" from `output-field'
2005 (message (read (substring output-field 4)))
2006 ;; Don't send to the console twice. (If it is a console error
2007 ;; it is also in the console stream.)
2008 (setq output-field nil)))
2009 ;; Output from command from frontend.
2010 (setq gdb-output-sink 'emacs))
2011
2012 (gdb-clear-partial-output)
2013 (when gdb-first-done-or-error
2014 (unless (or token-number gud-running)
2015 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2016 (gdb-update)
2017 (setq gdb-first-done-or-error nil))
2018
2019 (setq gdb-filter-output
2020 (gdb-concat-output gdb-filter-output output-field))
2021
2022 (if token-number
2023 (progn
2024 (with-current-buffer
2025 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2026 (funcall
2027 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2028 (setq gdb-handler-alist
2029 (assq-delete-all token-number gdb-handler-alist)))))
2030
2031 (defun gdb-concat-output (so-far new)
2032 (let ((sink gdb-output-sink))
2033 (cond
2034 ((eq sink 'user) (concat so-far new))
2035 ((eq sink 'emacs)
2036 (gdb-append-to-partial-output new)
2037 so-far))))
2038
2039 (defun gdb-append-to-partial-output (string)
2040 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2041 (goto-char (point-max))
2042 (insert string)))
2043
2044 (defun gdb-clear-partial-output ()
2045 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2046 (erase-buffer)))
2047
2048 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2049 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2050
2051 Field names are wrapped in double quotes and equal signs are
2052 replaced with semicolons.
2053
2054 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2055 partial output. This is used to get rid of useless keys in lists
2056 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2057 -break-info are examples of MI commands which issue such
2058 responses.
2059
2060 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2061 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2062 -break-info output when it contains breakpoint script field
2063 incompatible with GDB/MI output syntax."
2064 (save-excursion
2065 (goto-char (point-min))
2066 (when fix-key
2067 (save-excursion
2068 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2069 (replace-match "" nil nil nil 1))))
2070 (when fix-list
2071 (save-excursion
2072 ;; Find positions of braces which enclose broken list
2073 (while (re-search-forward (concat fix-list "={\"") nil t)
2074 (let ((p1 (goto-char (- (point) 2)))
2075 (p2 (progn (forward-sexp)
2076 (1- (point)))))
2077 ;; Replace braces with brackets
2078 (save-excursion
2079 (goto-char p1)
2080 (delete-char 1)
2081 (insert "[")
2082 (goto-char p2)
2083 (delete-char 1)
2084 (insert "]"))))))
2085 (goto-char (point-min))
2086 (insert "{")
2087 (while (re-search-forward
2088 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2089 (replace-match "\"\\1\":\\2" nil nil))
2090 (goto-char (point-max))
2091 (insert "}")))
2092
2093 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2094 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2095
2096 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2097 (gdb-jsonify-buffer fix-key fix-list)
2098 (save-excursion
2099 (goto-char (point-min))
2100 (let ((json-array-type 'list))
2101 (json-read))))
2102
2103 (defun gdb-json-string (string &optional fix-key fix-list)
2104 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2105
2106 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2107 (with-temp-buffer
2108 (insert string)
2109 (gdb-json-read-buffer fix-key fix-list)))
2110
2111 (defun gdb-json-partial-output (&optional fix-key fix-list)
2112 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2113
2114 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2115 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2116 (gdb-json-read-buffer fix-key fix-list)))
2117
2118 (defun gdb-line-posns (line)
2119 "Return a pair of LINE beginning and end positions."
2120 (let ((offset (1+ (- line (line-number-at-pos)))))
2121 (cons
2122 (line-beginning-position offset)
2123 (line-end-position offset))))
2124
2125 (defmacro gdb-mark-line (line variable)
2126 "Set VARIABLE marker to point at beginning of LINE.
2127
2128 If current window has no fringes, inverse colors on LINE.
2129
2130 Return position where LINE begins."
2131 `(save-excursion
2132 (let* ((posns (gdb-line-posns ,line))
2133 (start-posn (car posns))
2134 (end-posn (cdr posns)))
2135 (set-marker ,variable (copy-marker start-posn))
2136 (when (not (> (car (window-fringes)) 0))
2137 (put-text-property start-posn end-posn
2138 'font-lock-face '(:inverse-video t)))
2139 start-posn)))
2140
2141 (defun gdb-pad-string (string padding)
2142 (format (concat "%" (number-to-string padding) "s") string))
2143
2144 ;; gdb-table struct is a way to programmatically construct simple
2145 ;; tables. It help to reliably align columns of data in GDB buffers
2146 ;; and provides
2147 (defstruct
2148 gdb-table
2149 (column-sizes nil)
2150 (rows nil)
2151 (row-properties nil)
2152 (right-align nil))
2153
2154 (defun gdb-mapcar* (function &rest seqs)
2155 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2156 If there are several SEQS, FUNCTION is called with that many
2157 arugments, and mapping stops as sson as the shortest list runs
2158 out."
2159 (let ((shortest (apply #'min (mapcar #'length seqs))))
2160 (mapcar (lambda (i)
2161 (apply function
2162 (mapcar
2163 (lambda (seq)
2164 (nth i seq))
2165 seqs)))
2166 (number-sequence 0 (1- shortest)))))
2167
2168 (defun gdb-table-add-row (table row &optional properties)
2169 "Add ROW of string to TABLE and recalculate column sizes.
2170
2171 When non-nil, PROPERTIES will be added to the whole row when
2172 calling `gdb-table-string'."
2173 (let ((rows (gdb-table-rows table))
2174 (row-properties (gdb-table-row-properties table))
2175 (column-sizes (gdb-table-column-sizes table))
2176 (right-align (gdb-table-right-align table)))
2177 (when (not column-sizes)
2178 (setf (gdb-table-column-sizes table)
2179 (make-list (length row) 0)))
2180 (setf (gdb-table-rows table)
2181 (append rows (list row)))
2182 (setf (gdb-table-row-properties table)
2183 (append row-properties (list properties)))
2184 (setf (gdb-table-column-sizes table)
2185 (gdb-mapcar* (lambda (x s)
2186 (let ((new-x
2187 (max (abs x) (string-width (or s "")))))
2188 (if right-align new-x (- new-x))))
2189 (gdb-table-column-sizes table)
2190 row))
2191 ;; Avoid trailing whitespace at eol
2192 (if (not (gdb-table-right-align table))
2193 (setcar (last (gdb-table-column-sizes table)) 0))))
2194
2195 (defun gdb-table-string (table &optional sep)
2196 "Return TABLE as a string with columns separated with SEP."
2197 (let ((column-sizes (gdb-table-column-sizes table))
2198 (res ""))
2199 (mapconcat
2200 'identity
2201 (gdb-mapcar*
2202 (lambda (row properties)
2203 (apply 'propertize
2204 (mapconcat 'identity
2205 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2206 row column-sizes)
2207 sep)
2208 properties))
2209 (gdb-table-rows table)
2210 (gdb-table-row-properties table))
2211 "\n")))
2212
2213 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2214 (defun gdb-get-many-fields (struct &rest fields)
2215 "Return a list of FIELDS values from STRUCT."
2216 (let ((values))
2217 (dolist (field fields values)
2218 (setq values (append values (list (bindat-get-field struct field)))))))
2219
2220 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2221 handler-name
2222 &optional signal-list)
2223 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2224 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2225 buffer with `gdb-bind-function-to-buffer'.
2226
2227 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2228 defined trigger is called with an argument from SIGNAL-LIST. It's
2229 not recommended to define triggers with empty SIGNAL-LIST.
2230 Normally triggers should respond at least to 'update signal.
2231
2232 Normally the trigger defined by this command must be called from
2233 the buffer where HANDLER-NAME must work. This should be done so
2234 that buffer-local thread number may be used in GDB-COMMAND (by
2235 calling `gdb-current-context-command').
2236 `gdb-bind-function-to-buffer' is used to achieve this, see
2237 `gdb-get-buffer-create'.
2238
2239 Triggers defined by this command are meant to be used as a
2240 trigger argument when describing buffer types with
2241 `gdb-set-buffer-rules'."
2242 `(defun ,trigger-name (&optional signal)
2243 (when
2244 (or (not ,signal-list)
2245 (memq signal ,signal-list))
2246 (when (not (gdb-pending-p
2247 (cons (current-buffer) ',trigger-name)))
2248 (gdb-input
2249 (list ,gdb-command
2250 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
2251 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2252
2253 ;; Used by disassembly buffer only, the rest use
2254 ;; def-gdb-trigger-and-handler
2255 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2256 &optional nopreserve)
2257 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2258
2259 Handlers are normally called from the buffers they put output in.
2260
2261 Delete ((current-buffer) . TRIGGER-NAME) from
2262 `gdb-pending-triggers', erase current buffer and evaluate
2263 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2264
2265 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2266 `(defun ,handler-name ()
2267 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2268 (let* ((buffer-read-only nil)
2269 (window (get-buffer-window (current-buffer) 0))
2270 (start (window-start window))
2271 (p (window-point window)))
2272 (erase-buffer)
2273 (,custom-defun)
2274 (gdb-update-buffer-name)
2275 ,(when (not nopreserve)
2276 '(set-window-start window start)
2277 '(set-window-point window p)))))
2278
2279 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2280 handler-name custom-defun
2281 &optional signal-list)
2282 "Define trigger and handler.
2283
2284 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2285 `def-gdb-auto-update-trigger'.
2286
2287 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2288 `def-gdb-auto-update-handler'."
2289 `(progn
2290 (def-gdb-auto-update-trigger ,trigger-name
2291 ,gdb-command
2292 ,handler-name ,signal-list)
2293 (def-gdb-auto-update-handler ,handler-name
2294 ,trigger-name ,custom-defun)))
2295
2296 \f
2297
2298 ;; Breakpoint buffer : This displays the output of `-break-list'.
2299 (def-gdb-trigger-and-handler
2300 gdb-invalidate-breakpoints "-break-list"
2301 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2302 '(start update))
2303
2304 (gdb-set-buffer-rules
2305 'gdb-breakpoints-buffer
2306 'gdb-breakpoints-buffer-name
2307 'gdb-breakpoints-mode
2308 'gdb-invalidate-breakpoints)
2309
2310 (defun gdb-breakpoints-list-handler-custom ()
2311 (let ((breakpoints-list (bindat-get-field
2312 (gdb-json-partial-output "bkpt" "script")
2313 'BreakpointTable 'body))
2314 (table (make-gdb-table)))
2315 (setq gdb-breakpoints-list nil)
2316 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2317 (dolist (breakpoint breakpoints-list)
2318 (add-to-list 'gdb-breakpoints-list
2319 (cons (bindat-get-field breakpoint 'number)
2320 breakpoint))
2321 (let ((at (bindat-get-field breakpoint 'at))
2322 (pending (bindat-get-field breakpoint 'pending))
2323 (func (bindat-get-field breakpoint 'func))
2324 (type (bindat-get-field breakpoint 'type)))
2325 (gdb-table-add-row table
2326 (list
2327 (bindat-get-field breakpoint 'number)
2328 type
2329 (bindat-get-field breakpoint 'disp)
2330 (let ((flag (bindat-get-field breakpoint 'enabled)))
2331 (if (string-equal flag "y")
2332 (propertize "y" 'font-lock-face font-lock-warning-face)
2333 (propertize "n" 'font-lock-face font-lock-comment-face)))
2334 (bindat-get-field breakpoint 'addr)
2335 (bindat-get-field breakpoint 'times)
2336 (if (string-match ".*watchpoint" type)
2337 (bindat-get-field breakpoint 'what)
2338 (or pending at
2339 (concat "in "
2340 (propertize func 'font-lock-face font-lock-function-name-face)
2341 (gdb-frame-location breakpoint)))))
2342 ;; Add clickable properties only for breakpoints with file:line
2343 ;; information
2344 (append (list 'gdb-breakpoint breakpoint)
2345 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2346 mouse-face highlight))))))
2347 (insert (gdb-table-string table " "))
2348 (gdb-place-breakpoints)))
2349
2350 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2351 (defun gdb-place-breakpoints ()
2352 (let ((flag) (bptno))
2353 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2354 (dolist (buffer (buffer-list))
2355 (with-current-buffer buffer
2356 (if (and (eq gud-minor-mode 'gdbmi)
2357 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2358 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2359 (dolist (breakpoint gdb-breakpoints-list)
2360 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2361 ; an associative list
2362 (line (bindat-get-field breakpoint 'line)))
2363 (when line
2364 (let ((file (bindat-get-field breakpoint 'fullname))
2365 (flag (bindat-get-field breakpoint 'enabled))
2366 (bptno (bindat-get-field breakpoint 'number)))
2367 (unless (file-exists-p file)
2368 (setq file (cdr (assoc bptno gdb-location-alist))))
2369 (if (and file
2370 (not (string-equal file "File not found")))
2371 (with-current-buffer
2372 (find-file-noselect file 'nowarn)
2373 (gdb-init-buffer)
2374 ;; Only want one breakpoint icon at each location.
2375 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2376 (string-to-number line)))
2377 (gdb-input
2378 (list (concat "list " file ":1")
2379 'ignore))
2380 (gdb-input
2381 (list "-file-list-exec-source-file"
2382 `(lambda () (gdb-get-location
2383 ,bptno ,line ,flag)))))))))))
2384
2385 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2386
2387 (defun gdb-get-location (bptno line flag)
2388 "Find the directory containing the relevant source file.
2389 Put in buffer and place breakpoint icon."
2390 (goto-char (point-min))
2391 (catch 'file-not-found
2392 (if (re-search-forward gdb-source-file-regexp nil t)
2393 (delete (cons bptno "File not found") gdb-location-alist)
2394 (push (cons bptno (match-string 1)) gdb-location-alist)
2395 (gdb-resync)
2396 (unless (assoc bptno gdb-location-alist)
2397 (push (cons bptno "File not found") gdb-location-alist)
2398 (message-box "Cannot find source file for breakpoint location.
2399 Add directory to search path for source files using the GDB command, dir."))
2400 (throw 'file-not-found nil))
2401 (with-current-buffer (find-file-noselect (match-string 1))
2402 (gdb-init-buffer)
2403 ;; only want one breakpoint icon at each location
2404 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2405
2406 (add-hook 'find-file-hook 'gdb-find-file-hook)
2407
2408 (defun gdb-find-file-hook ()
2409 "Set up buffer for debugging if file is part of the source code
2410 of the current session."
2411 (if (and (buffer-name gud-comint-buffer)
2412 ;; in case gud or gdb-ui is just loaded
2413 gud-comint-buffer
2414 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2415 'gdbmi))
2416 (if (member buffer-file-name gdb-source-file-list)
2417 (with-current-buffer (find-buffer-visiting buffer-file-name)
2418 (gdb-init-buffer)))))
2419
2420 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2421 (declare-function gud-break "gdb-mi" t t) ; gud-def
2422 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2423
2424 (defun gdb-mouse-set-clear-breakpoint (event)
2425 "Set/clear breakpoint in left fringe/margin at mouse click.
2426 If not in a source or disassembly buffer just set point."
2427 (interactive "e")
2428 (mouse-minibuffer-check event)
2429 (let ((posn (event-end event)))
2430 (with-selected-window (posn-window posn)
2431 (if (or (buffer-file-name) (eq major-mode 'gdb-disassembly-mode))
2432 (if (numberp (posn-point posn))
2433 (save-excursion
2434 (goto-char (posn-point posn))
2435 (if (or (posn-object posn)
2436 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2437 'breakpoint))
2438 (gud-remove nil)
2439 (gud-break nil)))))
2440 (posn-set-point posn))))
2441
2442 (defun gdb-mouse-toggle-breakpoint-margin (event)
2443 "Enable/disable breakpoint in left margin with mouse click."
2444 (interactive "e")
2445 (mouse-minibuffer-check event)
2446 (let ((posn (event-end event)))
2447 (if (numberp (posn-point posn))
2448 (with-selected-window (posn-window posn)
2449 (save-excursion
2450 (goto-char (posn-point posn))
2451 (if (posn-object posn)
2452 (gud-basic-call
2453 (let ((bptno (get-text-property
2454 0 'gdb-bptno (car (posn-string posn)))))
2455 (concat
2456 (if (get-text-property
2457 0 'gdb-enabled (car (posn-string posn)))
2458 "-break-disable "
2459 "-break-enable ")
2460 bptno)))))))))
2461
2462 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2463 "Enable/disable breakpoint in left fringe with mouse click."
2464 (interactive "e")
2465 (mouse-minibuffer-check event)
2466 (let* ((posn (event-end event))
2467 (pos (posn-point posn))
2468 obj)
2469 (when (numberp pos)
2470 (with-selected-window (posn-window posn)
2471 (with-current-buffer (window-buffer (selected-window))
2472 (goto-char pos)
2473 (dolist (overlay (overlays-in pos pos))
2474 (when (overlay-get overlay 'put-break)
2475 (setq obj (overlay-get overlay 'before-string))))
2476 (when (stringp obj)
2477 (gud-basic-call
2478 (concat
2479 (if (get-text-property 0 'gdb-enabled obj)
2480 "-break-disable "
2481 "-break-enable ")
2482 (get-text-property 0 'gdb-bptno obj)))))))))
2483
2484 (defun gdb-breakpoints-buffer-name ()
2485 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2486
2487 (def-gdb-display-buffer
2488 gdb-display-breakpoints-buffer
2489 'gdb-breakpoints-buffer
2490 "Display status of user-settable breakpoints.")
2491
2492 (def-gdb-frame-for-buffer
2493 gdb-frame-breakpoints-buffer
2494 'gdb-breakpoints-buffer
2495 "Display status of user-settable breakpoints in a new frame.")
2496
2497 (defvar gdb-breakpoints-mode-map
2498 (let ((map (make-sparse-keymap))
2499 (menu (make-sparse-keymap "Breakpoints")))
2500 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2501 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2502 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2503 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2504 (suppress-keymap map)
2505 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2506 (define-key map " " 'gdb-toggle-breakpoint)
2507 (define-key map "D" 'gdb-delete-breakpoint)
2508 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2509 (define-key map "q" 'gdb-delete-frame-or-window)
2510 (define-key map "\r" 'gdb-goto-breakpoint)
2511 (define-key map "\t" '(lambda ()
2512 (interactive)
2513 (gdb-set-window-buffer
2514 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2515 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2516 (define-key map [follow-link] 'mouse-face)
2517 map))
2518
2519 (defun gdb-delete-frame-or-window ()
2520 "Delete frame if there is only one window. Otherwise delete the window."
2521 (interactive)
2522 (if (one-window-p) (delete-frame)
2523 (delete-window)))
2524
2525 ;;from make-mode-line-mouse-map
2526 (defun gdb-make-header-line-mouse-map (mouse function) "\
2527 Return a keymap with single entry for mouse key MOUSE on the header line.
2528 MOUSE is defined to run function FUNCTION with no args in the buffer
2529 corresponding to the mode line clicked."
2530 (let ((map (make-sparse-keymap)))
2531 (define-key map (vector 'header-line mouse) function)
2532 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2533 map))
2534
2535 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2536 `(propertize ,name
2537 'help-echo ,help-echo
2538 'mouse-face ',mouse-face
2539 'face ',face
2540 'local-map
2541 (gdb-make-header-line-mouse-map
2542 'mouse-1
2543 (lambda (event) (interactive "e")
2544 (save-selected-window
2545 (select-window (posn-window (event-start event)))
2546 (gdb-set-window-buffer
2547 (gdb-get-buffer-create ',buffer) t) )))))
2548
2549 \f
2550 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2551 ;;; Threads view
2552
2553 (defun gdb-threads-buffer-name ()
2554 (concat "*threads of " (gdb-get-target-string) "*"))
2555
2556 (def-gdb-display-buffer
2557 gdb-display-threads-buffer
2558 'gdb-threads-buffer
2559 "Display GDB threads.")
2560
2561 (def-gdb-frame-for-buffer
2562 gdb-frame-threads-buffer
2563 'gdb-threads-buffer
2564 "Display GDB threads in a new frame.")
2565
2566 (def-gdb-trigger-and-handler
2567 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2568 gdb-thread-list-handler gdb-thread-list-handler-custom
2569 '(start update update-threads))
2570
2571 (gdb-set-buffer-rules
2572 'gdb-threads-buffer
2573 'gdb-threads-buffer-name
2574 'gdb-threads-mode
2575 'gdb-invalidate-threads)
2576
2577 (defvar gdb-threads-font-lock-keywords
2578 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2579 (" \\(stopped\\)" (1 font-lock-warning-face))
2580 (" \\(running\\)" (1 font-lock-string-face))
2581 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2582 "Font lock keywords used in `gdb-threads-mode'.")
2583
2584 (defvar gdb-threads-mode-map
2585 (let ((map (make-sparse-keymap)))
2586 (define-key map "\r" 'gdb-select-thread)
2587 (define-key map "f" 'gdb-display-stack-for-thread)
2588 (define-key map "F" 'gdb-frame-stack-for-thread)
2589 (define-key map "l" 'gdb-display-locals-for-thread)
2590 (define-key map "L" 'gdb-frame-locals-for-thread)
2591 (define-key map "r" 'gdb-display-registers-for-thread)
2592 (define-key map "R" 'gdb-frame-registers-for-thread)
2593 (define-key map "d" 'gdb-display-disassembly-for-thread)
2594 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2595 (define-key map "i" 'gdb-interrupt-thread)
2596 (define-key map "c" 'gdb-continue-thread)
2597 (define-key map "s" 'gdb-step-thread)
2598 (define-key map "\t" '(lambda ()
2599 (interactive)
2600 (gdb-set-window-buffer
2601 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2602 (define-key map [mouse-2] 'gdb-select-thread)
2603 (define-key map [follow-link] 'mouse-face)
2604 map))
2605
2606 (defvar gdb-threads-header
2607 (list
2608 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
2609 "mouse-1: select" mode-line-highlight mode-line-inactive)
2610 " "
2611 (gdb-propertize-header "Threads" gdb-threads-buffer
2612 nil nil mode-line)))
2613
2614 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2615 "Major mode for GDB threads.
2616
2617 \\{gdb-threads-mode-map}"
2618 (setq gdb-thread-position (make-marker))
2619 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2620 (setq header-line-format gdb-threads-header)
2621 (set (make-local-variable 'font-lock-defaults)
2622 '(gdb-threads-font-lock-keywords))
2623 (run-mode-hooks 'gdb-threads-mode-hook)
2624 'gdb-invalidate-threads)
2625
2626 (defun gdb-thread-list-handler-custom ()
2627 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2628 (table (make-gdb-table))
2629 (marked-line nil))
2630 (setq gdb-threads-list nil)
2631 (setq gdb-running-threads-count 0)
2632 (setq gdb-stopped-threads-count 0)
2633 (set-marker gdb-thread-position nil)
2634
2635 (dolist (thread (reverse threads-list))
2636 (let ((running (string-equal (bindat-get-field thread 'state) "running")))
2637 (add-to-list 'gdb-threads-list
2638 (cons (bindat-get-field thread 'id)
2639 thread))
2640 (if running
2641 (incf gdb-running-threads-count)
2642 (incf gdb-stopped-threads-count))
2643
2644 (gdb-table-add-row table
2645 (list
2646 (bindat-get-field thread 'id)
2647 (concat
2648 (if gdb-thread-buffer-verbose-names
2649 (concat (bindat-get-field thread 'target-id) " ") "")
2650 (bindat-get-field thread 'state)
2651 ;; Include frame information for stopped threads
2652 (if (not running)
2653 (concat
2654 " in " (bindat-get-field thread 'frame 'func)
2655 (if gdb-thread-buffer-arguments
2656 (concat
2657 " ("
2658 (let ((args (bindat-get-field thread 'frame 'args)))
2659 (mapconcat
2660 (lambda (arg)
2661 (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value))))
2662 args ","))
2663 ")")
2664 "")
2665 (if gdb-thread-buffer-locations
2666 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2667 (if gdb-thread-buffer-addresses
2668 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2669 "")))
2670 (list
2671 'gdb-thread thread
2672 'mouse-face 'highlight
2673 'help-echo "mouse-2, RET: select thread")))
2674 (when (string-equal gdb-thread-number
2675 (bindat-get-field thread 'id))
2676 (setq marked-line (length gdb-threads-list))))
2677 (insert (gdb-table-string table " "))
2678 (when marked-line
2679 (gdb-mark-line marked-line gdb-thread-position)))
2680 ;; We update gud-running here because we need to make sure that
2681 ;; gdb-threads-list is up-to-date
2682 (gdb-update-gud-running)
2683 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2684
2685 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2686 "Define a NAME command which will act upon thread on the current line.
2687
2688 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2689 be the value of 'gdb-thread property of the current line. If
2690 'gdb-thread is nil, error is signaled."
2691 `(defun ,name (&optional event)
2692 ,(when doc doc)
2693 (interactive (list last-input-event))
2694 (if event (posn-set-point (event-end event)))
2695 (save-excursion
2696 (beginning-of-line)
2697 (let ((thread (get-text-property (point) 'gdb-thread)))
2698 (if thread
2699 ,custom-defun
2700 (error "Not recognized as thread line"))))))
2701
2702 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2703 "Define a NAME which will call BUFFER-COMMAND with id of thread
2704 on the current line."
2705 `(def-gdb-thread-buffer-command ,name
2706 (,buffer-command (bindat-get-field thread 'id))
2707 ,doc))
2708
2709 (def-gdb-thread-buffer-command gdb-select-thread
2710 (let ((new-id (bindat-get-field thread 'id)))
2711 (gdb-setq-thread-number new-id)
2712 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2713 (gdb-update))
2714 "Select the thread at current line of threads buffer.")
2715
2716 (def-gdb-thread-buffer-simple-command
2717 gdb-display-stack-for-thread
2718 gdb-preemptively-display-stack-buffer
2719 "Display stack buffer for the thread at current line.")
2720
2721 (def-gdb-thread-buffer-simple-command
2722 gdb-display-locals-for-thread
2723 gdb-preemptively-display-locals-buffer
2724 "Display locals buffer for the thread at current line.")
2725
2726 (def-gdb-thread-buffer-simple-command
2727 gdb-display-registers-for-thread
2728 gdb-preemptively-display-registers-buffer
2729 "Display registers buffer for the thread at current line.")
2730
2731 (def-gdb-thread-buffer-simple-command
2732 gdb-display-disassembly-for-thread
2733 gdb-preemptively-display-disassembly-buffer
2734 "Display disassembly buffer for the thread at current line.")
2735
2736 (def-gdb-thread-buffer-simple-command
2737 gdb-frame-stack-for-thread
2738 gdb-frame-stack-buffer
2739 "Display a new frame with stack buffer for the thread at
2740 current line.")
2741
2742 (def-gdb-thread-buffer-simple-command
2743 gdb-frame-locals-for-thread
2744 gdb-frame-locals-buffer
2745 "Display a new frame with locals buffer for the thread at
2746 current line.")
2747
2748 (def-gdb-thread-buffer-simple-command
2749 gdb-frame-registers-for-thread
2750 gdb-frame-registers-buffer
2751 "Display a new frame with registers buffer for the thread at
2752 current line.")
2753
2754 (def-gdb-thread-buffer-simple-command
2755 gdb-frame-disassembly-for-thread
2756 gdb-frame-disassembly-buffer
2757 "Display a new frame with disassembly buffer for the thread at
2758 current line.")
2759
2760 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2761 "Define a NAME which will execute GUD-COMMAND with
2762 `gdb-thread-number' locally bound to id of thread on the current
2763 line."
2764 `(def-gdb-thread-buffer-command ,name
2765 (if gdb-non-stop
2766 (let ((gdb-thread-number (bindat-get-field thread 'id))
2767 (gdb-gud-control-all-threads nil))
2768 (call-interactively #',gud-command))
2769 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2770 ,doc))
2771
2772 (def-gdb-thread-buffer-gud-command
2773 gdb-interrupt-thread
2774 gud-stop-subjob
2775 "Interrupt thread at current line.")
2776
2777 (def-gdb-thread-buffer-gud-command
2778 gdb-continue-thread
2779 gud-cont
2780 "Continue thread at current line.")
2781
2782 (def-gdb-thread-buffer-gud-command
2783 gdb-step-thread
2784 gud-step
2785 "Step thread at current line.")
2786
2787 \f
2788 ;;; Memory view
2789
2790 (defcustom gdb-memory-rows 8
2791 "Number of data rows in memory window."
2792 :type 'integer
2793 :group 'gud
2794 :version "23.2")
2795
2796 (defcustom gdb-memory-columns 4
2797 "Number of data columns in memory window."
2798 :type 'integer
2799 :group 'gud
2800 :version "23.2")
2801
2802 (defcustom gdb-memory-format "x"
2803 "Display format of data items in memory window."
2804 :type '(choice (const :tag "Hexadecimal" "x")
2805 (const :tag "Signed decimal" "d")
2806 (const :tag "Unsigned decimal" "u")
2807 (const :tag "Octal" "o")
2808 (const :tag "Binary" "t"))
2809 :group 'gud
2810 :version "22.1")
2811
2812 (defcustom gdb-memory-unit 4
2813 "Unit size of data items in memory window."
2814 :type '(choice (const :tag "Byte" 1)
2815 (const :tag "Halfword" 2)
2816 (const :tag "Word" 4)
2817 (const :tag "Giant word" 8))
2818 :group 'gud
2819 :version "23.2")
2820
2821 (def-gdb-trigger-and-handler
2822 gdb-invalidate-memory
2823 (format "-data-read-memory %s %s %d %d %d"
2824 gdb-memory-address
2825 gdb-memory-format
2826 gdb-memory-unit
2827 gdb-memory-rows
2828 gdb-memory-columns)
2829 gdb-read-memory-handler
2830 gdb-read-memory-custom
2831 '(start update))
2832
2833 (gdb-set-buffer-rules
2834 'gdb-memory-buffer
2835 'gdb-memory-buffer-name
2836 'gdb-memory-mode
2837 'gdb-invalidate-memory)
2838
2839 (defun gdb-memory-column-width (size format)
2840 "Return length of string with memory unit of SIZE in FORMAT.
2841
2842 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2843 in `gdb-memory-format'."
2844 (let ((format-base (cdr (assoc format
2845 '(("x" . 16)
2846 ("d" . 10) ("u" . 10)
2847 ("o" . 8)
2848 ("t" . 2))))))
2849 (if format-base
2850 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2851 (cond ((string-equal format "x")
2852 (+ 2 res)) ; hexadecimal numbers have 0x in front
2853 ((or (string-equal format "d")
2854 (string-equal format "o"))
2855 (1+ res))
2856 (t res)))
2857 (error "Unknown format"))))
2858
2859 (defun gdb-read-memory-custom ()
2860 (let* ((res (gdb-json-partial-output))
2861 (err-msg (bindat-get-field res 'msg)))
2862 (if (not err-msg)
2863 (let ((memory (bindat-get-field res 'memory)))
2864 (setq gdb-memory-address (bindat-get-field res 'addr))
2865 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2866 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2867 (setq gdb-memory-last-address gdb-memory-address)
2868 (dolist (row memory)
2869 (insert (concat (bindat-get-field row 'addr) ":"))
2870 (dolist (column (bindat-get-field row 'data))
2871 (insert (gdb-pad-string column
2872 (+ 2 (gdb-memory-column-width
2873 gdb-memory-unit
2874 gdb-memory-format)))))
2875 (newline)))
2876 ;; Show last page instead of empty buffer when out of bounds
2877 (progn
2878 (let ((gdb-memory-address gdb-memory-last-address))
2879 (gdb-invalidate-memory 'update)
2880 (error err-msg))))))
2881
2882 (defvar gdb-memory-mode-map
2883 (let ((map (make-sparse-keymap)))
2884 (suppress-keymap map t)
2885 (define-key map "q" 'kill-this-buffer)
2886 (define-key map "n" 'gdb-memory-show-next-page)
2887 (define-key map "p" 'gdb-memory-show-previous-page)
2888 (define-key map "a" 'gdb-memory-set-address)
2889 (define-key map "t" 'gdb-memory-format-binary)
2890 (define-key map "o" 'gdb-memory-format-octal)
2891 (define-key map "u" 'gdb-memory-format-unsigned)
2892 (define-key map "d" 'gdb-memory-format-signed)
2893 (define-key map "x" 'gdb-memory-format-hexadecimal)
2894 (define-key map "b" 'gdb-memory-unit-byte)
2895 (define-key map "h" 'gdb-memory-unit-halfword)
2896 (define-key map "w" 'gdb-memory-unit-word)
2897 (define-key map "g" 'gdb-memory-unit-giant)
2898 (define-key map "R" 'gdb-memory-set-rows)
2899 (define-key map "C" 'gdb-memory-set-columns)
2900 map))
2901
2902 (defun gdb-memory-set-address-event (event)
2903 "Handle a click on address field in memory buffer header."
2904 (interactive "e")
2905 (save-selected-window
2906 (select-window (posn-window (event-start event)))
2907 (gdb-memory-set-address)))
2908
2909 ;; Non-event version for use within keymap
2910 (defun gdb-memory-set-address ()
2911 "Set the start memory address."
2912 (interactive)
2913 (let ((arg (read-from-minibuffer "Memory address: ")))
2914 (setq gdb-memory-address arg))
2915 (gdb-invalidate-memory 'update))
2916
2917 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2918 "Define a function NAME which reads new VAR value from minibuffer."
2919 `(defun ,name (event)
2920 ,(when doc doc)
2921 (interactive "e")
2922 (save-selected-window
2923 (select-window (posn-window (event-start event)))
2924 (let* ((arg (read-from-minibuffer ,echo-string))
2925 (count (string-to-number arg)))
2926 (if (<= count 0)
2927 (error "Positive number only")
2928 (customize-set-variable ',variable count)
2929 (gdb-invalidate-memory 'update))))))
2930
2931 (def-gdb-set-positive-number
2932 gdb-memory-set-rows
2933 gdb-memory-rows
2934 "Rows: "
2935 "Set the number of data rows in memory window.")
2936
2937 (def-gdb-set-positive-number
2938 gdb-memory-set-columns
2939 gdb-memory-columns
2940 "Columns: "
2941 "Set the number of data columns in memory window.")
2942
2943 (defmacro def-gdb-memory-format (name format doc)
2944 "Define a function NAME to switch memory buffer to use FORMAT.
2945
2946 DOC is an optional documentation string."
2947 `(defun ,name () ,(when doc doc)
2948 (interactive)
2949 (customize-set-variable 'gdb-memory-format ,format)
2950 (gdb-invalidate-memory 'update)))
2951
2952 (def-gdb-memory-format
2953 gdb-memory-format-binary "t"
2954 "Set the display format to binary.")
2955
2956 (def-gdb-memory-format
2957 gdb-memory-format-octal "o"
2958 "Set the display format to octal.")
2959
2960 (def-gdb-memory-format
2961 gdb-memory-format-unsigned "u"
2962 "Set the display format to unsigned decimal.")
2963
2964 (def-gdb-memory-format
2965 gdb-memory-format-signed "d"
2966 "Set the display format to decimal.")
2967
2968 (def-gdb-memory-format
2969 gdb-memory-format-hexadecimal "x"
2970 "Set the display format to hexadecimal.")
2971
2972 (defvar gdb-memory-format-map
2973 (let ((map (make-sparse-keymap)))
2974 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2975 map)
2976 "Keymap to select format in the header line.")
2977
2978 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2979 "Menu of display formats in the header line.")
2980
2981 (define-key gdb-memory-format-menu [binary]
2982 '(menu-item "Binary" gdb-memory-format-binary
2983 :button (:radio . (equal gdb-memory-format "t"))))
2984 (define-key gdb-memory-format-menu [octal]
2985 '(menu-item "Octal" gdb-memory-format-octal
2986 :button (:radio . (equal gdb-memory-format "o"))))
2987 (define-key gdb-memory-format-menu [unsigned]
2988 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2989 :button (:radio . (equal gdb-memory-format "u"))))
2990 (define-key gdb-memory-format-menu [signed]
2991 '(menu-item "Signed Decimal" gdb-memory-format-signed
2992 :button (:radio . (equal gdb-memory-format "d"))))
2993 (define-key gdb-memory-format-menu [hexadecimal]
2994 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2995 :button (:radio . (equal gdb-memory-format "x"))))
2996
2997 (defun gdb-memory-format-menu (event)
2998 (interactive "@e")
2999 (x-popup-menu event gdb-memory-format-menu))
3000
3001 (defun gdb-memory-format-menu-1 (event)
3002 (interactive "e")
3003 (save-selected-window
3004 (select-window (posn-window (event-start event)))
3005 (let* ((selection (gdb-memory-format-menu event))
3006 (binding (and selection (lookup-key gdb-memory-format-menu
3007 (vector (car selection))))))
3008 (if binding (call-interactively binding)))))
3009
3010 (defmacro def-gdb-memory-unit (name unit-size doc)
3011 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3012
3013 DOC is an optional documentation string."
3014 `(defun ,name () ,(when doc doc)
3015 (interactive)
3016 (customize-set-variable 'gdb-memory-unit ,unit-size)
3017 (gdb-invalidate-memory 'update)))
3018
3019 (def-gdb-memory-unit gdb-memory-unit-giant 8
3020 "Set the unit size to giant words (eight bytes).")
3021
3022 (def-gdb-memory-unit gdb-memory-unit-word 4
3023 "Set the unit size to words (four bytes).")
3024
3025 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3026 "Set the unit size to halfwords (two bytes).")
3027
3028 (def-gdb-memory-unit gdb-memory-unit-byte 1
3029 "Set the unit size to bytes.")
3030
3031 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3032 "Define a function NAME which show new address in memory buffer.
3033
3034 The defined function switches Memory buffer to show address
3035 stored in ADDRESS-VAR variable.
3036
3037 DOC is an optional documentation string."
3038 `(defun ,name
3039 ,(when doc doc)
3040 (interactive)
3041 (let ((gdb-memory-address ,address-var))
3042 (gdb-invalidate-memory))))
3043
3044 (def-gdb-memory-show-page gdb-memory-show-previous-page
3045 gdb-memory-prev-page)
3046
3047 (def-gdb-memory-show-page gdb-memory-show-next-page
3048 gdb-memory-next-page)
3049
3050 (defvar gdb-memory-unit-map
3051 (let ((map (make-sparse-keymap)))
3052 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3053 map)
3054 "Keymap to select units in the header line.")
3055
3056 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
3057 "Menu of units in the header line.")
3058
3059 (define-key gdb-memory-unit-menu [giantwords]
3060 '(menu-item "Giant words" gdb-memory-unit-giant
3061 :button (:radio . (equal gdb-memory-unit 8))))
3062 (define-key gdb-memory-unit-menu [words]
3063 '(menu-item "Words" gdb-memory-unit-word
3064 :button (:radio . (equal gdb-memory-unit 4))))
3065 (define-key gdb-memory-unit-menu [halfwords]
3066 '(menu-item "Halfwords" gdb-memory-unit-halfword
3067 :button (:radio . (equal gdb-memory-unit 2))))
3068 (define-key gdb-memory-unit-menu [bytes]
3069 '(menu-item "Bytes" gdb-memory-unit-byte
3070 :button (:radio . (equal gdb-memory-unit 1))))
3071
3072 (defun gdb-memory-unit-menu (event)
3073 (interactive "@e")
3074 (x-popup-menu event gdb-memory-unit-menu))
3075
3076 (defun gdb-memory-unit-menu-1 (event)
3077 (interactive "e")
3078 (save-selected-window
3079 (select-window (posn-window (event-start event)))
3080 (let* ((selection (gdb-memory-unit-menu event))
3081 (binding (and selection (lookup-key gdb-memory-unit-menu
3082 (vector (car selection))))))
3083 (if binding (call-interactively binding)))))
3084
3085 (defvar gdb-memory-font-lock-keywords
3086 '(;; <__function.name+n>
3087 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
3088 )
3089 "Font lock keywords used in `gdb-memory-mode'.")
3090
3091 (defvar gdb-memory-header
3092 '(:eval
3093 (concat
3094 "Start address["
3095 (propertize "-"
3096 'face font-lock-warning-face
3097 'help-echo "mouse-1: decrement address"
3098 'mouse-face 'mode-line-highlight
3099 'local-map (gdb-make-header-line-mouse-map
3100 'mouse-1
3101 #'gdb-memory-show-previous-page))
3102 "|"
3103 (propertize "+"
3104 'face font-lock-warning-face
3105 'help-echo "mouse-1: increment address"
3106 'mouse-face 'mode-line-highlight
3107 'local-map (gdb-make-header-line-mouse-map
3108 'mouse-1
3109 #'gdb-memory-show-next-page))
3110 "]: "
3111 (propertize gdb-memory-address
3112 'face font-lock-warning-face
3113 'help-echo "mouse-1: set start address"
3114 'mouse-face 'mode-line-highlight
3115 'local-map (gdb-make-header-line-mouse-map
3116 'mouse-1
3117 #'gdb-memory-set-address-event))
3118 " Rows: "
3119 (propertize (number-to-string gdb-memory-rows)
3120 'face font-lock-warning-face
3121 'help-echo "mouse-1: set number of columns"
3122 'mouse-face 'mode-line-highlight
3123 'local-map (gdb-make-header-line-mouse-map
3124 'mouse-1
3125 #'gdb-memory-set-rows))
3126 " Columns: "
3127 (propertize (number-to-string gdb-memory-columns)
3128 'face font-lock-warning-face
3129 'help-echo "mouse-1: set number of columns"
3130 'mouse-face 'mode-line-highlight
3131 'local-map (gdb-make-header-line-mouse-map
3132 'mouse-1
3133 #'gdb-memory-set-columns))
3134 " Display Format: "
3135 (propertize gdb-memory-format
3136 'face font-lock-warning-face
3137 'help-echo "mouse-3: select display format"
3138 'mouse-face 'mode-line-highlight
3139 'local-map gdb-memory-format-map)
3140 " Unit Size: "
3141 (propertize (number-to-string gdb-memory-unit)
3142 'face font-lock-warning-face
3143 'help-echo "mouse-3: select unit size"
3144 'mouse-face 'mode-line-highlight
3145 'local-map gdb-memory-unit-map)))
3146 "Header line used in `gdb-memory-mode'.")
3147
3148 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3149 "Major mode for examining memory.
3150
3151 \\{gdb-memory-mode-map}"
3152 (setq header-line-format gdb-memory-header)
3153 (set (make-local-variable 'font-lock-defaults)
3154 '(gdb-memory-font-lock-keywords))
3155 (run-mode-hooks 'gdb-memory-mode-hook)
3156 'gdb-invalidate-memory)
3157
3158 (defun gdb-memory-buffer-name ()
3159 (concat "*memory of " (gdb-get-target-string) "*"))
3160
3161 (def-gdb-display-buffer
3162 gdb-display-memory-buffer
3163 'gdb-memory-buffer
3164 "Display memory contents.")
3165
3166 (defun gdb-frame-memory-buffer ()
3167 "Display memory contents in a new frame."
3168 (interactive)
3169 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3170 (special-display-frame-alist
3171 `((left-fringe . 0)
3172 (right-fringe . 0)
3173 (width . 83)
3174 ,@gdb-frame-parameters)))
3175 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3176
3177 \f
3178 ;;; Disassembly view
3179
3180 (defun gdb-disassembly-buffer-name ()
3181 (gdb-current-context-buffer-name
3182 (concat "disassembly of " (gdb-get-target-string))))
3183
3184 (def-gdb-display-buffer
3185 gdb-display-disassembly-buffer
3186 'gdb-disassembly-buffer
3187 "Display disassembly for current stack frame.")
3188
3189 (def-gdb-preempt-display-buffer
3190 gdb-preemptively-display-disassembly-buffer
3191 'gdb-disassembly-buffer)
3192
3193 (def-gdb-frame-for-buffer
3194 gdb-frame-disassembly-buffer
3195 'gdb-disassembly-buffer
3196 "Display disassembly in a new frame.")
3197
3198 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3199 (let* ((frame (gdb-current-buffer-frame))
3200 (file (bindat-get-field frame 'fullname))
3201 (line (bindat-get-field frame 'line)))
3202 (when file
3203 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3204 gdb-disassembly-handler
3205 ;; We update disassembly only after we have actual frame information
3206 ;; about all threads, so no there's `update' signal in this list
3207 '(start update-disassembly))
3208
3209 (def-gdb-auto-update-handler
3210 gdb-disassembly-handler
3211 gdb-invalidate-disassembly
3212 gdb-disassembly-handler-custom
3213 t)
3214
3215 (gdb-set-buffer-rules
3216 'gdb-disassembly-buffer
3217 'gdb-disassembly-buffer-name
3218 'gdb-disassembly-mode
3219 'gdb-invalidate-disassembly)
3220
3221 (defvar gdb-disassembly-font-lock-keywords
3222 '(;; <__function.name+n>
3223 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3224 (1 font-lock-function-name-face))
3225 ;; 0xNNNNNNNN <__function.name+n>: opcode
3226 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3227 (4 font-lock-keyword-face))
3228 ;; %register(at least i386)
3229 ("%\\sw+" . font-lock-variable-name-face)
3230 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3231 (1 font-lock-comment-face)
3232 (2 font-lock-function-name-face))
3233 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3234 "Font lock keywords used in `gdb-disassembly-mode'.")
3235
3236 (defvar gdb-disassembly-mode-map
3237 ;; TODO
3238 (let ((map (make-sparse-keymap)))
3239 (suppress-keymap map)
3240 (define-key map "q" 'kill-this-buffer)
3241 map))
3242
3243 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3244 "Major mode for GDB disassembly information.
3245
3246 \\{gdb-disassembly-mode-map}"
3247 ;; TODO Rename overlay variable for disassembly mode
3248 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3249 (setq fringes-outside-margins t)
3250 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3251 (set (make-local-variable 'font-lock-defaults)
3252 '(gdb-disassembly-font-lock-keywords))
3253 (run-mode-hooks 'gdb-disassembly-mode-hook)
3254 'gdb-invalidate-disassembly)
3255
3256 (defun gdb-disassembly-handler-custom ()
3257 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3258 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3259 (pos 1)
3260 (table (make-gdb-table))
3261 (marked-line nil))
3262 (dolist (instr instructions)
3263 (gdb-table-add-row table
3264 (list
3265 (bindat-get-field instr 'address)
3266 (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
3267 (bindat-get-field instr 'inst)))
3268 (when (string-equal (bindat-get-field instr 'address)
3269 address)
3270 (progn
3271 (setq marked-line (length (gdb-table-rows table)))
3272 (setq fringe-indicator-alist
3273 (if (string-equal gdb-frame-number "0")
3274 nil
3275 '((overlay-arrow . hollow-right-triangle)))))))
3276 (insert (gdb-table-string table " "))
3277 (gdb-disassembly-place-breakpoints)
3278 ;; Mark current position with overlay arrow and scroll window to
3279 ;; that point
3280 (when marked-line
3281 (let ((window (get-buffer-window (current-buffer) 0)))
3282 (set-window-point window (gdb-mark-line marked-line gdb-disassembly-position))))
3283 (setq mode-name
3284 (gdb-current-context-mode-name
3285 (concat "Disassembly: "
3286 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3287
3288 (defun gdb-disassembly-place-breakpoints ()
3289 (gdb-remove-breakpoint-icons (point-min) (point-max))
3290 (dolist (breakpoint gdb-breakpoints-list)
3291 (let* ((breakpoint (cdr breakpoint))
3292 (bptno (bindat-get-field breakpoint 'number))
3293 (flag (bindat-get-field breakpoint 'enabled))
3294 (address (bindat-get-field breakpoint 'addr)))
3295 (save-excursion
3296 (goto-char (point-min))
3297 (if (re-search-forward (concat "^" address) nil t)
3298 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3299
3300 \f
3301 (defvar gdb-breakpoints-header
3302 (list
3303 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3304 nil nil mode-line)
3305 " "
3306 (gdb-propertize-header "Threads" gdb-threads-buffer
3307 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3308
3309 ;;; Breakpoints view
3310 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3311 "Major mode for gdb breakpoints.
3312
3313 \\{gdb-breakpoints-mode-map}"
3314 (setq header-line-format gdb-breakpoints-header)
3315 (run-mode-hooks 'gdb-breakpoints-mode-hook)
3316 'gdb-invalidate-breakpoints)
3317
3318 (defun gdb-toggle-breakpoint ()
3319 "Enable/disable breakpoint at current line of breakpoints buffer."
3320 (interactive)
3321 (save-excursion
3322 (beginning-of-line)
3323 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3324 (if breakpoint
3325 (gud-basic-call
3326 (concat (if (string-equal "y" (bindat-get-field breakpoint 'enabled))
3327 "-break-disable "
3328 "-break-enable ")
3329 (bindat-get-field breakpoint 'number)))
3330 (error "Not recognized as break/watchpoint line")))))
3331
3332 (defun gdb-delete-breakpoint ()
3333 "Delete the breakpoint at current line of breakpoints buffer."
3334 (interactive)
3335 (save-excursion
3336 (beginning-of-line)
3337 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3338 (if breakpoint
3339 (gud-basic-call (concat "-break-delete " (bindat-get-field breakpoint 'number)))
3340 (error "Not recognized as break/watchpoint line")))))
3341
3342 (defun gdb-goto-breakpoint (&optional event)
3343 "Go to the location of breakpoint at current line of
3344 breakpoints buffer."
3345 (interactive (list last-input-event))
3346 (if event (posn-set-point (event-end event)))
3347 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3348 (let ((window (get-buffer-window gud-comint-buffer)))
3349 (if window (save-selected-window (select-window window))))
3350 (save-excursion
3351 (beginning-of-line)
3352 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3353 (if breakpoint
3354 (let ((bptno (bindat-get-field breakpoint 'number))
3355 (file (bindat-get-field breakpoint 'fullname))
3356 (line (bindat-get-field breakpoint 'line)))
3357 (save-selected-window
3358 (let* ((buffer (find-file-noselect
3359 (if (file-exists-p file) file
3360 (cdr (assoc bptno gdb-location-alist)))))
3361 (window (or (gdb-display-source-buffer buffer)
3362 (display-buffer buffer))))
3363 (setq gdb-source-window window)
3364 (with-current-buffer buffer
3365 (goto-char (point-min))
3366 (forward-line (1- (string-to-number line)))
3367 (set-window-point window (point))))))
3368 (error "Not recognized as break/watchpoint line")))))
3369
3370 \f
3371 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3372 ;;
3373 (def-gdb-trigger-and-handler
3374 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3375 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3376 '(start update))
3377
3378 (gdb-set-buffer-rules
3379 'gdb-stack-buffer
3380 'gdb-stack-buffer-name
3381 'gdb-frames-mode
3382 'gdb-invalidate-frames)
3383
3384 (defun gdb-frame-location (frame)
3385 "Return \" of file:line\" or \" of library\" for structure FRAME.
3386
3387 FRAME must have either \"file\" and \"line\" members or \"from\"
3388 member."
3389 (let ((file (bindat-get-field frame 'file))
3390 (line (bindat-get-field frame 'line))
3391 (from (bindat-get-field frame 'from)))
3392 (let ((res (or (and file line (concat file ":" line))
3393 from)))
3394 (if res (concat " of " res) ""))))
3395
3396 (defun gdb-stack-list-frames-custom ()
3397 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3398 (table (make-gdb-table)))
3399 (set-marker gdb-stack-position nil)
3400 (dolist (frame stack)
3401 (gdb-table-add-row table
3402 (list
3403 (bindat-get-field frame 'level)
3404 "in"
3405 (concat
3406 (bindat-get-field frame 'func)
3407 (if gdb-stack-buffer-locations
3408 (gdb-frame-location frame) "")
3409 (if gdb-stack-buffer-addresses
3410 (concat " at " (bindat-get-field frame 'addr)) "")))
3411 `(mouse-face highlight
3412 help-echo "mouse-2, RET: Select frame"
3413 gdb-frame ,frame)))
3414 (insert (gdb-table-string table " ")))
3415 (when (and gdb-frame-number
3416 (gdb-buffer-shows-main-thread-p))
3417 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3418 gdb-stack-position))
3419 (setq mode-name
3420 (gdb-current-context-mode-name "Frames")))
3421
3422 (defun gdb-stack-buffer-name ()
3423 (gdb-current-context-buffer-name
3424 (concat "stack frames of " (gdb-get-target-string))))
3425
3426 (def-gdb-display-buffer
3427 gdb-display-stack-buffer
3428 'gdb-stack-buffer
3429 "Display backtrace of current stack.")
3430
3431 (def-gdb-preempt-display-buffer
3432 gdb-preemptively-display-stack-buffer
3433 'gdb-stack-buffer nil t)
3434
3435 (def-gdb-frame-for-buffer
3436 gdb-frame-stack-buffer
3437 'gdb-stack-buffer
3438 "Display backtrace of current stack in a new frame.")
3439
3440 (defvar gdb-frames-mode-map
3441 (let ((map (make-sparse-keymap)))
3442 (suppress-keymap map)
3443 (define-key map "q" 'kill-this-buffer)
3444 (define-key map "\r" 'gdb-select-frame)
3445 (define-key map [mouse-2] 'gdb-select-frame)
3446 (define-key map [follow-link] 'mouse-face)
3447 map))
3448
3449 (defvar gdb-frames-font-lock-keywords
3450 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3451 "Font lock keywords used in `gdb-frames-mode'.")
3452
3453 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3454 "Major mode for gdb call stack.
3455
3456 \\{gdb-frames-mode-map}"
3457 (setq gdb-stack-position (make-marker))
3458 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3459 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3460 (set (make-local-variable 'font-lock-defaults)
3461 '(gdb-frames-font-lock-keywords))
3462 (run-mode-hooks 'gdb-frames-mode-hook)
3463 'gdb-invalidate-frames)
3464
3465 (defun gdb-select-frame (&optional event)
3466 "Select the frame and display the relevant source."
3467 (interactive (list last-input-event))
3468 (if event (posn-set-point (event-end event)))
3469 (let ((frame (get-text-property (point) 'gdb-frame)))
3470 (if frame
3471 (if (gdb-buffer-shows-main-thread-p)
3472 (let ((new-level (bindat-get-field frame 'level)))
3473 (setq gdb-frame-number new-level)
3474 (gdb-input (list (concat "-stack-select-frame " new-level) 'ignore))
3475 (gdb-update))
3476 (error "Could not select frame for non-current thread"))
3477 (error "Not recognized as frame line"))))
3478
3479 \f
3480 ;; Locals buffer.
3481 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3482 (def-gdb-trigger-and-handler
3483 gdb-invalidate-locals
3484 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
3485 gdb-locals-handler gdb-locals-handler-custom
3486 '(start update))
3487
3488 (gdb-set-buffer-rules
3489 'gdb-locals-buffer
3490 'gdb-locals-buffer-name
3491 'gdb-locals-mode
3492 'gdb-invalidate-locals)
3493
3494 (defvar gdb-locals-watch-map
3495 (let ((map (make-sparse-keymap)))
3496 (suppress-keymap map)
3497 (define-key map "\r" 'gud-watch)
3498 (define-key map [mouse-2] 'gud-watch)
3499 map)
3500 "Keymap to create watch expression of a complex data type local variable.")
3501
3502 (defvar gdb-edit-locals-map-1
3503 (let ((map (make-sparse-keymap)))
3504 (suppress-keymap map)
3505 (define-key map "\r" 'gdb-edit-locals-value)
3506 (define-key map [mouse-2] 'gdb-edit-locals-value)
3507 map)
3508 "Keymap to edit value of a simple data type local variable.")
3509
3510 (defun gdb-edit-locals-value (&optional event)
3511 "Assign a value to a variable displayed in the locals buffer."
3512 (interactive (list last-input-event))
3513 (save-excursion
3514 (if event (posn-set-point (event-end event)))
3515 (beginning-of-line)
3516 (let* ((var (bindat-get-field
3517 (get-text-property (point) 'gdb-local-variable) 'name))
3518 (value (read-string (format "New value (%s): " var))))
3519 (gud-basic-call
3520 (concat "-gdb-set variable " var " = " value)))))
3521
3522 ;; Dont display values of arrays or structures.
3523 ;; These can be expanded using gud-watch.
3524 (defun gdb-locals-handler-custom ()
3525 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3526 (table (make-gdb-table)))
3527 (dolist (local locals-list)
3528 (let ((name (bindat-get-field local 'name))
3529 (value (bindat-get-field local 'value))
3530 (type (bindat-get-field local 'type)))
3531 (if (or (not value)
3532 (string-match "\\0x" value))
3533 (add-text-properties 0 (length name)
3534 `(mouse-face highlight
3535 help-echo "mouse-2: create watch expression"
3536 local-map ,gdb-locals-watch-map)
3537 name)
3538 (add-text-properties 0 (length value)
3539 `(mouse-face highlight
3540 help-echo "mouse-2: edit value"
3541 local-map ,gdb-edit-locals-map-1)
3542 value))
3543 (gdb-table-add-row
3544 table
3545 (list
3546 (propertize type 'font-lock-face font-lock-type-face)
3547 (propertize name 'font-lock-face font-lock-variable-name-face)
3548 value)
3549 `(gdb-local-variable ,local))))
3550 (insert (gdb-table-string table " "))
3551 (setq mode-name
3552 (gdb-current-context-mode-name
3553 (concat "Locals: " (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3554
3555 (defvar gdb-locals-header
3556 (list
3557 (gdb-propertize-header "Locals" gdb-locals-buffer
3558 nil nil mode-line)
3559 " "
3560 (gdb-propertize-header "Registers" gdb-registers-buffer
3561 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3562
3563 (defvar gdb-locals-mode-map
3564 (let ((map (make-sparse-keymap)))
3565 (suppress-keymap map)
3566 (define-key map "q" 'kill-this-buffer)
3567 (define-key map "\t" '(lambda ()
3568 (interactive)
3569 (gdb-set-window-buffer
3570 (gdb-get-buffer-create
3571 'gdb-registers-buffer
3572 gdb-thread-number) t)))
3573 map))
3574
3575 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3576 "Major mode for gdb locals.
3577
3578 \\{gdb-locals-mode-map}"
3579 (setq header-line-format gdb-locals-header)
3580 (run-mode-hooks 'gdb-locals-mode-hook)
3581 'gdb-invalidate-locals)
3582
3583 (defun gdb-locals-buffer-name ()
3584 (gdb-current-context-buffer-name
3585 (concat "locals of " (gdb-get-target-string))))
3586
3587 (def-gdb-display-buffer
3588 gdb-display-locals-buffer
3589 'gdb-locals-buffer
3590 "Display local variables of current stack and their values.")
3591
3592 (def-gdb-preempt-display-buffer
3593 gdb-preemptively-display-locals-buffer
3594 'gdb-locals-buffer nil t)
3595
3596 (def-gdb-frame-for-buffer
3597 gdb-frame-locals-buffer
3598 'gdb-locals-buffer
3599 "Display local variables of current stack and their values in a new frame.")
3600
3601 \f
3602 ;; Registers buffer.
3603
3604 (def-gdb-trigger-and-handler
3605 gdb-invalidate-registers
3606 (concat (gdb-current-context-command "-data-list-register-values") " x")
3607 gdb-registers-handler
3608 gdb-registers-handler-custom
3609 '(start update))
3610
3611 (gdb-set-buffer-rules
3612 'gdb-registers-buffer
3613 'gdb-registers-buffer-name
3614 'gdb-registers-mode
3615 'gdb-invalidate-registers)
3616
3617 (defun gdb-registers-handler-custom ()
3618 (when gdb-register-names
3619 (let ((register-values (bindat-get-field (gdb-json-partial-output) 'register-values))
3620 (table (make-gdb-table)))
3621 (dolist (register register-values)
3622 (let* ((register-number (bindat-get-field register 'number))
3623 (value (bindat-get-field register 'value))
3624 (register-name (nth (string-to-number register-number)
3625 gdb-register-names)))
3626 (gdb-table-add-row
3627 table
3628 (list
3629 (propertize register-name 'font-lock-face font-lock-variable-name-face)
3630 (if (member register-number gdb-changed-registers)
3631 (propertize value 'font-lock-face font-lock-warning-face)
3632 value))
3633 `(mouse-face highlight
3634 help-echo "mouse-2: edit value"
3635 gdb-register-name ,register-name))))
3636 (insert (gdb-table-string table " ")))
3637 (setq mode-name
3638 (gdb-current-context-mode-name "Registers"))))
3639
3640 (defun gdb-edit-register-value (&optional event)
3641 "Assign a value to a register displayed in the registers buffer."
3642 (interactive (list last-input-event))
3643 (save-excursion
3644 (if event (posn-set-point (event-end event)))
3645 (beginning-of-line)
3646 (let* ((var (bindat-get-field
3647 (get-text-property (point) 'gdb-register-name)))
3648 (value (read-string (format "New value (%s): " var))))
3649 (gud-basic-call
3650 (concat "-gdb-set variable $" var " = " value)))))
3651
3652 (defvar gdb-registers-mode-map
3653 (let ((map (make-sparse-keymap)))
3654 (suppress-keymap map)
3655 (define-key map "\r" 'gdb-edit-register-value)
3656 (define-key map [mouse-2] 'gdb-edit-register-value)
3657 (define-key map "q" 'kill-this-buffer)
3658 (define-key map "\t" '(lambda ()
3659 (interactive)
3660 (gdb-set-window-buffer
3661 (gdb-get-buffer-create
3662 'gdb-locals-buffer
3663 gdb-thread-number) t)))
3664 map))
3665
3666 (defvar gdb-registers-header
3667 (list
3668 (gdb-propertize-header "Locals" gdb-locals-buffer
3669 "mouse-1: select" mode-line-highlight mode-line-inactive)
3670 " "
3671 (gdb-propertize-header "Registers" gdb-registers-buffer
3672 nil nil mode-line)))
3673
3674 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3675 "Major mode for gdb registers.
3676
3677 \\{gdb-registers-mode-map}"
3678 (setq header-line-format gdb-registers-header)
3679 (run-mode-hooks 'gdb-registers-mode-hook)
3680 'gdb-invalidate-registers)
3681
3682 (defun gdb-registers-buffer-name ()
3683 (gdb-current-context-buffer-name
3684 (concat "registers of " (gdb-get-target-string))))
3685
3686 (def-gdb-display-buffer
3687 gdb-display-registers-buffer
3688 'gdb-registers-buffer
3689 "Display integer register contents.")
3690
3691 (def-gdb-preempt-display-buffer
3692 gdb-preemptively-display-registers-buffer
3693 'gdb-registers-buffer nil t)
3694
3695 (def-gdb-frame-for-buffer
3696 gdb-frame-registers-buffer
3697 'gdb-registers-buffer
3698 "Display integer register contents in a new frame.")
3699
3700 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3701 (defun gdb-get-changed-registers ()
3702 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3703 (not (gdb-pending-p 'gdb-get-changed-registers)))
3704 (progn
3705 (gdb-input
3706 (list
3707 "-data-list-changed-registers"
3708 'gdb-changed-registers-handler))
3709 (gdb-add-pending 'gdb-get-changed-registers))))
3710
3711 (defun gdb-changed-registers-handler ()
3712 (gdb-delete-pending 'gdb-get-changed-registers)
3713 (setq gdb-changed-registers nil)
3714 (dolist (register-number (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3715 (push register-number gdb-changed-registers)))
3716
3717 (defun gdb-register-names-handler ()
3718 ;; Don't use gdb-pending-triggers because this handler is called
3719 ;; only once (in gdb-init-1)
3720 (setq gdb-register-names nil)
3721 (dolist (register-name (bindat-get-field (gdb-json-partial-output) 'register-names))
3722 (push register-name gdb-register-names))
3723 (setq gdb-register-names (reverse gdb-register-names)))
3724 \f
3725
3726 (defun gdb-get-source-file-list ()
3727 "Create list of source files for current GDB session.
3728 If buffers already exist for any of these files, gud-minor-mode
3729 is set in them."
3730 (goto-char (point-min))
3731 (while (re-search-forward gdb-source-file-regexp nil t)
3732 (push (match-string 1) gdb-source-file-list))
3733 (dolist (buffer (buffer-list))
3734 (with-current-buffer buffer
3735 (when (member buffer-file-name gdb-source-file-list)
3736 (gdb-init-buffer))))
3737 (gdb-force-mode-line-update
3738 (propertize "ready" 'face font-lock-variable-name-face)))
3739
3740 (defun gdb-get-main-selected-frame ()
3741 "Trigger for `gdb-frame-handler' which uses main current
3742 thread. Called from `gdb-update'."
3743 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3744 (progn
3745 (gdb-input
3746 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3747 (gdb-add-pending 'gdb-get-main-selected-frame))))
3748
3749 (defun gdb-frame-handler ()
3750 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3751 overlay arrow in source buffer."
3752 (gdb-delete-pending 'gdb-get-main-selected-frame)
3753 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3754 (when frame
3755 (setq gdb-selected-frame (bindat-get-field frame 'func))
3756 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3757 (setq gdb-frame-number (bindat-get-field frame 'level))
3758 (setq gdb-frame-address (bindat-get-field frame 'addr))
3759 (let ((line (bindat-get-field frame 'line)))
3760 (setq gdb-selected-line (and line (string-to-number line)))
3761 (when (and gdb-selected-file gdb-selected-line)
3762 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3763 (gud-display-frame)))
3764 (if gud-overlay-arrow-position
3765 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3766 (position (marker-position gud-overlay-arrow-position)))
3767 (when buffer
3768 (with-current-buffer buffer
3769 (setq fringe-indicator-alist
3770 (if (string-equal gdb-frame-number "0")
3771 nil
3772 '((overlay-arrow . hollow-right-triangle))))
3773 (setq gud-overlay-arrow-position (make-marker))
3774 (set-marker gud-overlay-arrow-position position))))))))
3775
3776 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3777
3778 (defun gdb-get-prompt ()
3779 "Find prompt for GDB session."
3780 (goto-char (point-min))
3781 (setq gdb-prompt-name nil)
3782 (re-search-forward gdb-prompt-name-regexp nil t)
3783 (setq gdb-prompt-name (match-string 1))
3784 ;; Insert first prompt.
3785 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3786
3787 ;;;; Window management
3788 (defun gdb-display-buffer (buf dedicated &optional frame)
3789 "Show buffer BUF.
3790
3791 If BUF is already displayed in some window, show it, deiconifying
3792 the frame if necessary. Otherwise, find least recently used
3793 window and show BUF there, if the window is not used for GDB
3794 already, in which case that window is splitted first."
3795 (let ((answer (get-buffer-window buf (or frame 0))))
3796 (if answer
3797 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3798 (let ((window (get-lru-window)))
3799 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3800 'gdbmi)
3801 (let* ((largest (get-largest-window))
3802 (cur-size (window-height largest)))
3803 (setq answer (split-window largest))
3804 (set-window-buffer answer buf)
3805 (set-window-dedicated-p answer dedicated)
3806 answer)
3807 (set-window-buffer window buf)
3808 window)))))
3809
3810 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3811 "Find window displaying a buffer with the same
3812 `gdb-buffer-type' as BUF and show BUF there. If no such window
3813 exists, just call `gdb-display-buffer' for BUF. If the window
3814 found is already dedicated, split window according to
3815 SPLIT-HORIZONTAL and show BUF in the new window."
3816 (if buf
3817 (when (not (get-buffer-window buf))
3818 (let* ((buf-type (gdb-buffer-type buf))
3819 (existing-window
3820 (get-window-with-predicate
3821 #'(lambda (w)
3822 (and (eq buf-type
3823 (gdb-buffer-type (window-buffer w)))
3824 (not (window-dedicated-p w)))))))
3825 (if existing-window
3826 (set-window-buffer existing-window buf)
3827 (let ((dedicated-window
3828 (get-window-with-predicate
3829 #'(lambda (w)
3830 (eq buf-type
3831 (gdb-buffer-type (window-buffer w)))))))
3832 (if dedicated-window
3833 (set-window-buffer
3834 (split-window dedicated-window nil split-horizontal) buf)
3835 (gdb-display-buffer buf t))))))
3836 (error "Null buffer")))
3837 \f
3838 ;;; Shared keymap initialization:
3839
3840 (let ((menu (make-sparse-keymap "GDB-Windows")))
3841 (define-key gud-menu-map [displays]
3842 `(menu-item "GDB-Windows" ,menu
3843 :visible (eq gud-minor-mode 'gdbmi)))
3844 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3845 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3846 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3847 (define-key menu [disassembly]
3848 '("Disassembly" . gdb-display-disassembly-buffer))
3849 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3850 (define-key menu [inferior]
3851 '("IO" . gdb-display-io-buffer))
3852 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3853 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3854 (define-key menu [breakpoints]
3855 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3856
3857 (let ((menu (make-sparse-keymap "GDB-Frames")))
3858 (define-key gud-menu-map [frames]
3859 `(menu-item "GDB-Frames" ,menu
3860 :visible (eq gud-minor-mode 'gdbmi)))
3861 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3862 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3863 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3864 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3865 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3866 (define-key menu [inferior]
3867 '("IO" . gdb-frame-io-buffer))
3868 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3869 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3870 (define-key menu [breakpoints]
3871 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3872
3873 (let ((menu (make-sparse-keymap "GDB-MI")))
3874 (define-key menu [gdb-customize]
3875 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3876 :help "Customize Gdb Graphical Mode options."))
3877 (define-key menu [gdb-many-windows]
3878 '(menu-item "Display Other Windows" gdb-many-windows
3879 :help "Toggle display of locals, stack and breakpoint information"
3880 :button (:toggle . gdb-many-windows)))
3881 (define-key menu [gdb-restore-windows]
3882 '(menu-item "Restore Window Layout" gdb-restore-windows
3883 :help "Restore standard layout for debug session."))
3884 (define-key menu [sep1]
3885 '(menu-item "--"))
3886 (define-key menu [all-threads]
3887 '(menu-item "GUD controls all threads"
3888 (lambda ()
3889 (interactive)
3890 (setq gdb-gud-control-all-threads t))
3891 :help "GUD start/stop commands apply to all threads"
3892 :button (:radio . gdb-gud-control-all-threads)))
3893 (define-key menu [current-thread]
3894 '(menu-item "GUD controls current thread"
3895 (lambda ()
3896 (interactive)
3897 (setq gdb-gud-control-all-threads nil))
3898 :help "GUD start/stop commands apply to current thread only"
3899 :button (:radio . (not gdb-gud-control-all-threads))))
3900 (define-key menu [sep2]
3901 '(menu-item "--"))
3902 (define-key menu [gdb-customize-reasons]
3903 '(menu-item "Customize switching..."
3904 (lambda ()
3905 (interactive)
3906 (customize-option 'gdb-switch-reasons))))
3907 (define-key menu [gdb-switch-when-another-stopped]
3908 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped gdb-switch-when-another-stopped
3909 "Automatically switch to stopped thread"
3910 "GDB thread switching %s"
3911 "Switch to stopped thread"))
3912 (define-key gud-menu-map [mi]
3913 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3914
3915 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3916 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3917 ;; tool-bar-local-item-from-menu here because it appends new buttons
3918 ;; to toolbar from right to left while we want our A/T throttle to
3919 ;; show up right before Run button.
3920 (define-key-after gud-tool-bar-map [all-threads]
3921 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3922 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3923 :visible (and (eq gud-minor-mode 'gdbmi)
3924 gdb-non-stop
3925 (not gdb-gud-control-all-threads)))
3926 'run)
3927
3928 (define-key-after gud-tool-bar-map [current-thread]
3929 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3930 :image (find-image '((:type xpm :file "gud/all.xpm")))
3931 :visible (and (eq gud-minor-mode 'gdbmi)
3932 gdb-non-stop
3933 gdb-gud-control-all-threads))
3934 'all-threads)
3935
3936 (defun gdb-frame-gdb-buffer ()
3937 "Display GUD buffer in a new frame."
3938 (interactive)
3939 (let ((special-display-regexps (append special-display-regexps '(".*")))
3940 (special-display-frame-alist
3941 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3942 gdb-frame-parameters)))
3943 (same-window-regexps nil))
3944 (display-buffer gud-comint-buffer)))
3945
3946 (defun gdb-display-gdb-buffer ()
3947 "Display GUD buffer."
3948 (interactive)
3949 (let ((same-window-regexps nil))
3950 (select-window (display-buffer gud-comint-buffer nil 0))))
3951
3952 (defun gdb-set-window-buffer (name &optional ignore-dedicated)
3953 "Set buffer of selected window to NAME and dedicate window.
3954
3955 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
3956 window is dedicated."
3957 (when ignore-dedicated
3958 (set-window-dedicated-p (selected-window) nil))
3959 (set-window-buffer (selected-window) (get-buffer name))
3960 (set-window-dedicated-p (selected-window) t))
3961
3962 (defun gdb-setup-windows ()
3963 "Layout the window pattern for `gdb-many-windows'."
3964 (gdb-display-locals-buffer)
3965 (gdb-display-stack-buffer)
3966 (delete-other-windows)
3967 (gdb-display-breakpoints-buffer)
3968 (delete-other-windows)
3969 ; Don't dedicate.
3970 (pop-to-buffer gud-comint-buffer)
3971 (split-window nil ( / ( * (window-height) 3) 4))
3972 (split-window nil ( / (window-height) 3))
3973 (split-window-horizontally)
3974 (other-window 1)
3975 (gdb-set-window-buffer (gdb-locals-buffer-name))
3976 (other-window 1)
3977 (switch-to-buffer
3978 (if gud-last-last-frame
3979 (gud-find-file (car gud-last-last-frame))
3980 (if gdb-main-file
3981 (gud-find-file gdb-main-file)
3982 ;; Put buffer list in window if we
3983 ;; can't find a source file.
3984 (list-buffers-noselect))))
3985 (setq gdb-source-window (selected-window))
3986 (split-window-horizontally)
3987 (other-window 1)
3988 (gdb-set-window-buffer
3989 (gdb-get-buffer-create 'gdb-inferior-io))
3990 (other-window 1)
3991 (gdb-set-window-buffer (gdb-stack-buffer-name))
3992 (split-window-horizontally)
3993 (other-window 1)
3994 (gdb-set-window-buffer (if gdb-show-threads-by-default
3995 (gdb-threads-buffer-name)
3996 (gdb-breakpoints-buffer-name)))
3997 (other-window 1))
3998
3999 (defcustom gdb-many-windows nil
4000 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4001 In this case it starts with two windows: one displaying the GUD
4002 buffer and the other with the source file with the main routine
4003 of the debugged program. Non-nil means display the layout shown for
4004 `gdb'."
4005 :type 'boolean
4006 :group 'gdb
4007 :version "22.1")
4008
4009 (defun gdb-many-windows (arg)
4010 "Toggle the number of windows in the basic arrangement.
4011 With arg, display additional buffers iff arg is positive."
4012 (interactive "P")
4013 (setq gdb-many-windows
4014 (if (null arg)
4015 (not gdb-many-windows)
4016 (> (prefix-numeric-value arg) 0)))
4017 (message (format "Display of other windows %sabled"
4018 (if gdb-many-windows "en" "dis")))
4019 (if (and gud-comint-buffer
4020 (buffer-name gud-comint-buffer))
4021 (condition-case nil
4022 (gdb-restore-windows)
4023 (error nil))))
4024
4025 (defun gdb-restore-windows ()
4026 "Restore the basic arrangement of windows used by gdb.
4027 This arrangement depends on the value of `gdb-many-windows'."
4028 (interactive)
4029 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
4030 (delete-other-windows)
4031 (if gdb-many-windows
4032 (gdb-setup-windows)
4033 (when (or gud-last-last-frame gdb-show-main)
4034 (split-window)
4035 (other-window 1)
4036 (switch-to-buffer
4037 (if gud-last-last-frame
4038 (gud-find-file (car gud-last-last-frame))
4039 (gud-find-file gdb-main-file)))
4040 (setq gdb-source-window (selected-window))
4041 (other-window 1))))
4042
4043 (defun gdb-reset ()
4044 "Exit a debugging session cleanly.
4045 Kills the gdb buffers, and resets variables and the source buffers."
4046 (dolist (buffer (buffer-list))
4047 (unless (eq buffer gud-comint-buffer)
4048 (with-current-buffer buffer
4049 (if (eq gud-minor-mode 'gdbmi)
4050 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4051 (kill-buffer nil)
4052 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4053 (setq gud-minor-mode nil)
4054 (kill-local-variable 'tool-bar-map)
4055 (kill-local-variable 'gdb-define-alist))))))
4056 (setq gdb-disassembly-position nil)
4057 (setq overlay-arrow-variable-list
4058 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4059 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4060 (setq gdb-stack-position nil)
4061 (setq overlay-arrow-variable-list
4062 (delq 'gdb-stack-position overlay-arrow-variable-list))
4063 (setq gdb-thread-position nil)
4064 (setq overlay-arrow-variable-list
4065 (delq 'gdb-thread-position overlay-arrow-variable-list))
4066 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4067 (setq gud-running nil)
4068 (setq gdb-active-process nil)
4069 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4070
4071 (defun gdb-get-source-file ()
4072 "Find the source file where the program starts and display it with related
4073 buffers, if required."
4074 (goto-char (point-min))
4075 (if (re-search-forward gdb-source-file-regexp nil t)
4076 (setq gdb-main-file (match-string 1)))
4077 (if gdb-many-windows
4078 (gdb-setup-windows)
4079 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4080 (if gdb-show-main
4081 (let ((pop-up-windows t))
4082 (display-buffer (gud-find-file gdb-main-file))))))
4083
4084 ;;from put-image
4085 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4086 "Put string PUTSTRING in front of POS in the current buffer.
4087 PUTSTRING is displayed by putting an overlay into the current buffer with a
4088 `before-string' string that has a `display' property whose value is
4089 PUTSTRING."
4090 (let ((string (make-string 1 ?x))
4091 (buffer (current-buffer)))
4092 (setq putstring (copy-sequence putstring))
4093 (let ((overlay (make-overlay pos pos buffer))
4094 (prop (or dprop
4095 (list (list 'margin 'left-margin) putstring))))
4096 (put-text-property 0 1 'display prop string)
4097 (if sprops
4098 (add-text-properties 0 1 sprops string))
4099 (overlay-put overlay 'put-break t)
4100 (overlay-put overlay 'before-string string))))
4101
4102 ;;from remove-images
4103 (defun gdb-remove-strings (start end &optional buffer)
4104 "Remove strings between START and END in BUFFER.
4105 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4106 BUFFER nil or omitted means use the current buffer."
4107 (unless buffer
4108 (setq buffer (current-buffer)))
4109 (dolist (overlay (overlays-in start end))
4110 (when (overlay-get overlay 'put-break)
4111 (delete-overlay overlay))))
4112
4113 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4114 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4115 (start (- (car posns) 1))
4116 (end (+ (cdr posns) 1))
4117 (putstring (if enabled "B" "b"))
4118 (source-window (get-buffer-window (current-buffer) 0)))
4119 (add-text-properties
4120 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4121 putstring)
4122 (if enabled
4123 (add-text-properties
4124 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4125 (add-text-properties
4126 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4127 (gdb-remove-breakpoint-icons start end)
4128 (if (display-images-p)
4129 (if (>= (or left-fringe-width
4130 (if source-window (car (window-fringes source-window)))
4131 gdb-buffer-fringe-width) 8)
4132 (gdb-put-string
4133 nil (1+ start)
4134 `(left-fringe breakpoint
4135 ,(if enabled
4136 'breakpoint-enabled
4137 'breakpoint-disabled))
4138 'gdb-bptno bptno
4139 'gdb-enabled enabled)
4140 (when (< left-margin-width 2)
4141 (save-current-buffer
4142 (setq left-margin-width 2)
4143 (if source-window
4144 (set-window-margins
4145 source-window
4146 left-margin-width right-margin-width))))
4147 (put-image
4148 (if enabled
4149 (or breakpoint-enabled-icon
4150 (setq breakpoint-enabled-icon
4151 (find-image `((:type xpm :data
4152 ,breakpoint-xpm-data
4153 :ascent 100 :pointer hand)
4154 (:type pbm :data
4155 ,breakpoint-enabled-pbm-data
4156 :ascent 100 :pointer hand)))))
4157 (or breakpoint-disabled-icon
4158 (setq breakpoint-disabled-icon
4159 (find-image `((:type xpm :data
4160 ,breakpoint-xpm-data
4161 :conversion disabled
4162 :ascent 100 :pointer hand)
4163 (:type pbm :data
4164 ,breakpoint-disabled-pbm-data
4165 :ascent 100 :pointer hand))))))
4166 (+ start 1)
4167 putstring
4168 'left-margin))
4169 (when (< left-margin-width 2)
4170 (save-current-buffer
4171 (setq left-margin-width 2)
4172 (let ((window (get-buffer-window (current-buffer) 0)))
4173 (if window
4174 (set-window-margins
4175 window left-margin-width right-margin-width)))))
4176 (gdb-put-string
4177 (propertize putstring
4178 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
4179 (1+ start)))))
4180
4181 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4182 (gdb-remove-strings start end)
4183 (if (display-images-p)
4184 (remove-images start end))
4185 (when remove-margin
4186 (setq left-margin-width 0)
4187 (let ((window (get-buffer-window (current-buffer) 0)))
4188 (if window
4189 (set-window-margins
4190 window left-margin-width right-margin-width)))))
4191
4192 (provide 'gdb-mi)
4193
4194 ;; arch-tag: 1b41ea2b-f364-4cec-8f35-e02e4fe01912
4195 ;;; gdb-mi.el ends here