print out jim error message stack trace in expected order(look at any C++ or Java...
[fw/openocd] / src / startup.tcl
1 #
2 # Defines basic Tcl procs that must be there for
3 # OpenOCD to work.
4 #
5 # Embedded into OpenOCD executable
6 #
7
8
9 # Help text list. A list of command + help text pairs.
10 #
11 # Commands can be more than one word and they are stored
12 # as "flash banks" "help text x x x"
13
14 set ocd_helptext {}
15
16 proc add_help_text {cmd cmd_help} {
17         global ocd_helptext
18         lappend ocd_helptext [list $cmd $cmd_help]
19 }
20
21 proc get_help_text {} {
22         global ocd_helptext
23         return $ocd_helptext
24 }
25
26 # Production command
27 # FIX!!! need to figure out how to feed back relevant output
28 # from e.g. "flash banks" command...
29 proc board_produce {filename serialnumber} {
30         openocd "reset init"
31         openocd "flash write_image erase $filename [flash] bin"]]
32         openocd "verify_image $filename [flash] bin"]]
33         echo "Successfully ran production procedure"
34 }
35
36 proc board_test {} {
37         echo "Production test not implemented"
38 }
39
40 # Show flash in human readable form
41 # This is an example of a human readable form of a low level fn
42 proc flash_banks_pretty {} { 
43         set i 0         
44         set result ""
45         foreach {a} [flash_banks] {
46                 if {$i > 0} {
47                         set result "$result\n"
48                 }
49                 set result [format "$result#%d: %s at 0x%08x, size 0x%08x, buswidth %d, chipwidth %d" $i [lindex $a 0] [lindex $a 1] [lindex $a 2] [lindex $a 3] [lindex $a 4]]
50                 set i [expr $i+1]       
51         }       
52         return $result
53 }
54
55 # We need to explicitly redirect this to the OpenOCD command
56 # as Tcl defines the exit proc
57 proc exit {} {
58         openocd_throw exit
59 }
60
61 # We have currently converted only "flash banks" to tcl.
62 proc flash args {
63         if {[string compare [lindex $args 0] banks]==0} {
64                 return [flash_banks_pretty]
65         }
66         openocd_throw "flash $args"
67 }
68
69 #Print help text for a command. Word wrap
70 #help text that is too wide inside column.
71 proc help {args} {
72         global ocd_helptext
73         set cmd $args
74         foreach a [lsort $ocd_helptext] {
75                 if {[string length $cmd]==0||[string first $cmd $a]!=-1||[string first $cmd [lindex $a 1]]!=-1} {
76                         set w 50
77                         set cmdname [lindex $a 0]
78                         set h [lindex $a 1]
79                         set n 0
80                         while 1 {
81                                 if {$n > [string length $h]} {break}
82                                 
83                                 set next_a [expr $n+$w]
84                                 if {[string length $h]>$n+$w} {
85                                         set xxxx [string range $h $n [expr $n+$w]]
86                                         for {set lastpos [expr [string length $xxxx]-1]} {$lastpos>=0&&[string compare [string range $xxxx $lastpos $lastpos] " "]!=0} {set lastpos [expr $lastpos-1]} {
87                                         }
88                                         #set next_a -1
89                                         if {$lastpos!=-1} {
90                                                 set next_a [expr $lastpos+$n+1]
91                                         }
92                                 }
93                                 
94                                 
95                                 puts [format "%-25s %s" $cmdname [string range $h $n [expr $next_a-1]] ]
96                                 set cmdname ""
97                                 set n [expr $next_a]
98                         }
99                 }
100         }
101 }
102
103 add_help_text help "Tcl implementation of help command"
104
105
106 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
107 proc unknown {args} {
108         if {[string length $args]>0} {
109                 set cmd ""
110                 # We need to add back quotes for arguments w/space
111                 # for args without space, we can add quotes anyway
112                 foreach {a} $args {
113                         set cmd "$cmd \"$a\""
114                 }
115                 openocd_throw $cmd
116         }
117         # openocd_throw outputs while running and also sets the
118         # primary return value to the output of the command
119         #
120         # The primary return value have been set by "openocd" above,
121         # so we need to clear it, lest we print out the output from
122         # the command twice.
123         return ""
124 }
125
126
127 proc target_script {target_num eventname scriptname} {
128         if {[string compare $eventname reset]==0} {
129                 set eventname post_reset
130         }
131
132         # This is the script we invoke
133         proc "target_[set eventname]_[set target_num]" {} "script $scriptname" 
134         
135 }
136
137 #add_help_text target_script "xxx"
138 add_help_text target_script "<target#> <event=reset/pre_reset/post_halt/pre_resume/gdb_program_config> <script_file>"
139