Browse code

regdb: gdb filter that synchronizes the current line of the editor to the gdb session

Dario Rodriguez authored on 23/01/2024 11:39:57
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,76 @@
1
+#!/usr/bin/tclsh
2
+
3
+if { [llength $argv]==0 || [string compare [lindex $argv 0] "--help"]==0 } {
4
+        puts "Syntax: regdb executable"
5
+        exit 0
6
+}
7
+
8
+set lastfile ""
9
+
10
+proc checkselection { curselection } {
11
+        global env lastfile
12
+        set socketsdir /tmp/.re_$env(USER)
13
+        set filenames [glob -tails -directory $socketsdir ) *]
14
+        # search for occurrences of <filename>:<line>[:<col>]
15
+        foreach f $filenames {
16
+                set index -1
17
+                set flen [string length $f]
18
+                while { [set index [string first $f $curselection [expr $index + 1]]]!=-1 } {
19
+                        if { [string compare [string index $curselection [expr $index+$flen]] ":"]==0
20
+                             && ( $index==0 || [string is alnum [string index $curselection [expr $index-1]]]==0 )
21
+                             && [string compare [string index $curselection [expr $index+$flen+1]] ""]!=0
22
+                             && [string is digit [string index $curselection [expr $index+$flen+1]]]==1} {
23
+                                set index [expr $index+$flen+1]
24
+                                set startindex $index
25
+                                while { [string compare [string index $curselection $index] ""]!=0
26
+                                        && ( [string is digit [string index $curselection $index]]==1
27
+                                        || [string compare [string index $curselection $index] ":"]==0 ) } {
28
+                                        incr index
29
+                                }
30
+                                set newpos [string range $curselection $startindex $index-1]
31
+                                remotecontrol $socketsdir $f "goto $newpos"
32
+                                set lastfile $f
33
+                        }
34
+                }
35
+        }
36
+        # if it is just <line><space><...>, set last used file to that line (useful for gdb)
37
+        set firstword [lindex [split $curselection " \t\r\t"] 0]
38
+        if { [string length $lastfile]>0 && [string length $firstword]>0 && [string is ascii $firstword] && [string is digit $firstword] } {
39
+                remotecontrol $socketsdir $lastfile "goto $curselection"
40
+        }
41
+}
42
+
43
+proc remotecontrol { socketsdir filename command } {
44
+        #puts "filename:$socketsdir/$filename command:\"$command\""
45
+        catch {
46
+                set f [open "|socat - UNIX-CONNECT:$socketsdir/$filename" r+]
47
+                puts -nonewline $f "$command\n"
48
+                close $f
49
+        }
50
+}
51
+
52
+
53
+set fifo [exec mktemp -u /tmp/regdbtmpXXX]
54
+exec mkfifo $fifo
55
+
56
+exec script -q -f -a -c "gdb [lindex $argv 0]" $fifo &
57
+
58
+set f [open $fifo "r"]
59
+set c ""
60
+set l ""
61
+while { [string length [set c [read $f 1]]]>0 } {
62
+        if { [string compare $c "\r"]==0 || [string compare $c "\n"]==0 } {
63
+                if { [string length $l]==0 } {
64
+                        # ignore empty lines
65
+                        continue
66
+                }
67
+                checkselection [set l]
68
+                set l ""
69
+                continue
70
+        }
71
+        set l "$l$c"
72
+}
73
+close $f
74
+file delete $fifo
75
+
76
+