rellermeyer / course_os

CS 439 course OS
BSD 3-Clause "New" or "Revised" License
38 stars 26 forks source link

Emacs Automatic GDB Command #77

Closed fayalalebrun closed 3 years ago

fayalalebrun commented 3 years ago

It is time that we add some config specific for Emacs, the best text editor ever made.

Whenever you use the gdb command in Emacs, it asks you for the exact command that you wish to use. This sets the correct value for our project using a dir-local.el file.

As an idea for the future, we could also define an Elisp function in this file to set up the complete debug environment, first running make debug as an async shell command and then calling gdb with the correct command.

ValentijnvdBeek commented 3 years ago

Can confirm it works, here are some changes to set compile command to run the test suite and to add a command (+ keybind) to auto-setup the debugging suite

((nil . ((eval . (let ((root (projectile-project-root)))
                   (let ((command (concat "arm-none-eabi-gdb -i=mi -ex \"target remote localhost:1234\" -ex \"symbol-file " root "src/kernel/build/kernel.sym\"")))
                     (setq-local gud-gud-gdb-command-name command)
                     (setq-local gud-gdb-command-name command)
                     (set (make-local-variable 'compile-command)
                          (concat "cd " (concat root "src") " && make test"))
                     (use-local-map (copy-keymap (current-local-map)))
                     (local-set-key [f5] 'compile)
                     (local-set-key [f6] 'co/gdb)

                     (defun co/gdb ()
                       (interactive)
                       (async-shell-command (concat "cd "
                                                    (concat (projectile-project-root) "src")
                                                    " && "
                                                    "make debug") nil 0)
                       (gdb gud-gdb-command-name))))))))

It also overrides two keybinds locally which isn't ideal, but it works for my setup

fayalalebrun commented 3 years ago

@ValentijnvdBeek Those extra functions are very useful, though for me it doesn't work as-is. The problem is that (current-local-map) returns nil, which causes on error when you call copy-keymap on it, and it says here, that you should never copy a keymap, it's better to create a new one and then set its parent. So here is the same elisp code with that fix applied, which doesn't cause errors for me:

((nil . ((eval . (let ((root (projectile-project-root)))
  (let ((command (concat "arm-none-eabi-gdb -i=mi -ex \"target remote localhost:1234\" -ex \"symbol-file " root "src/kernel/build/kernel.sym\"")))
    (setq-local gud-gud-gdb-command-name command)
    (setq-local gud-gdb-command-name command)
    (set (make-local-variable 'compile-command)
         (concat "cd " (concat root "src") " && make test"))
    (let ((map (make-sparse-keymap))) 

      (set-keymap-parent map (current-local-map))
      (use-local-map map)
      (local-set-key [f5] 'compile)
      (local-set-key [f6] 'co/gdb)

      (defun co/gdb ()
        (interactive)
        (async-shell-command (concat "cd "
                                     (concat (projectile-project-root) "src")
                                     " && "
                                     "make debug") nil 0)
        (gdb gud-gdb-command-name)))))))))