]>
Commit | Line | Data |
---|---|---|
1 | http://sourceware.org/ml/gdb-patches/2010-01/msg00558.html | |
2 | Subject: Re: [patch] print a more useful error message for "gdb core" | |
3 | ||
4 | [ Fixed up since the mail. ] | |
5 | ||
6 | On Thu, 21 Jan 2010 18:17:15 +0100, Doug Evans wrote: | |
7 | > Not an exhaustive list, but if we go down the path of converting "gdb | |
8 | > corefile" to "gdb -c corefile", then we also need to think about "file | |
9 | > corefile" being converted to "core corefile" [or "target core | |
10 | > corefile", "core" is apparently deprecated in favor of "target core"] | |
11 | > and "target exec corefile" -> "target core corefile". Presumably | |
12 | > "file corefile" (and "target exec corefile") would discard the | |
13 | > currently selected executable. But maybe not. Will that be confusing | |
14 | > for users? I don't know. | |
15 | ||
16 | While thinking about it overriding some GDB _commands_ was not my intention. | |
17 | ||
18 | There is a general assumption if I have a shell COMMAND and some FILE I can do | |
19 | $ COMMAND FILE | |
20 | and COMMAND will appropriately load the FILE. | |
21 | ||
22 | FSF GDB currently needs to specify also the executable file for core files | |
23 | which already inhibits this intuitive expectation. OTOH with the build-id | |
24 | locating patch which could allow such intuitive start notneeding the | |
25 | executable file. Still it currently did not work due to the required "-c": | |
26 | $ COMMAND -c COREFILE | |
27 | ||
28 | Entering "file", "core-file" or "attach" commands is already explicit enough | |
29 | so that it IMO should do what the command name says without any | |
30 | autodetections. The second command line argument | |
31 | (captured_main->pid_or_core_arg) is also autodetected (for PID or CORE) but | |
32 | neither "attach" accepts a core file nor "core-file" accepts a PID. | |
33 | ||
34 | ||
35 | The patch makes sense only with the build-id patchset so this is not submit | |
36 | for FSF GDB inclusion yet. I am fine with your patch (+/- Hui Zhu's pending | |
37 | bfd_check_format_matches) as the patch below is its natural extension. | |
38 | ||
39 | ||
40 | Sorry for the delay, | |
41 | Jan | |
42 | ||
43 | ||
44 | 2010-01-25 Jan Kratochvil <jan.kratochvil@redhat.com> | |
45 | ||
46 | * exceptions.h (enum errors <IS_CORE_ERROR>): New. | |
47 | * exec.c: Include exceptions.h. | |
48 | (exec_file_attach <bfd_core>): Call throw_error (IS_CORE_ERROR, ...). | |
49 | * main.c (exec_or_core_file_attach): New. | |
50 | (captured_main <optind < argc>): Set also corearg. | |
51 | (captured_main <strcmp (execarg, symarg) == 0>): New variable func. | |
52 | Call exec_or_core_file_attach if COREARG matches EXECARG. Call | |
53 | symbol_file_add_main only if CORE_BFD remained NULL. | |
54 | ||
55 | Http://sourceware.org/ml/gdb-patches/2010-01/msg00517.html | |
56 | 2010-01-20 Doug Evans <dje@google.com> | |
57 | ||
58 | * exec.c (exec_file_attach): Print a more useful error message if the | |
59 | user did "gdb core". | |
60 | ||
61 | Index: gdb-7.9.50.20150531/gdb/exec.c | |
62 | =================================================================== | |
63 | --- gdb-7.9.50.20150531.orig/gdb/exec.c 2015-05-31 03:48:29.000000000 +0200 | |
64 | +++ gdb-7.9.50.20150531/gdb/exec.c 2015-05-31 20:07:35.092878685 +0200 | |
65 | @@ -35,6 +35,7 @@ | |
66 | #include "progspace.h" | |
67 | #include "gdb_bfd.h" | |
68 | #include "gcore.h" | |
69 | +#include "exceptions.h" | |
70 | ||
71 | #include <fcntl.h> | |
72 | #include "readline/readline.h" | |
73 | @@ -298,12 +299,27 @@ exec_file_attach (const char *filename, | |
74 | ||
75 | if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching)) | |
76 | { | |
77 | + int is_core; | |
78 | + | |
79 | + /* If the user accidentally did "gdb core", print a useful | |
80 | + error message. Check it only after bfd_object has been checked as | |
81 | + a valid executable may get recognized for example also as | |
82 | + "trad-core". */ | |
83 | + is_core = bfd_check_format (exec_bfd, bfd_core); | |
84 | + | |
85 | /* Make sure to close exec_bfd, or else "run" might try to use | |
86 | it. */ | |
87 | exec_close (); | |
88 | - error (_("\"%s\": not in executable format: %s"), | |
89 | - scratch_pathname, | |
90 | - gdb_bfd_errmsg (bfd_get_error (), matching)); | |
91 | + | |
92 | + if (is_core != 0) | |
93 | + throw_error (IS_CORE_ERROR, | |
94 | + _("\"%s\" is a core file.\n" | |
95 | + "Please specify an executable to debug."), | |
96 | + scratch_pathname); | |
97 | + else | |
98 | + error (_("\"%s\": not in executable format: %s"), | |
99 | + scratch_pathname, | |
100 | + gdb_bfd_errmsg (bfd_get_error (), matching)); | |
101 | } | |
102 | ||
103 | if (build_section_table (exec_bfd, §ions, §ions_end)) | |
104 | Index: gdb-7.9.50.20150531/gdb/main.c | |
105 | =================================================================== | |
106 | --- gdb-7.9.50.20150531.orig/gdb/main.c 2015-05-31 20:07:34.183872824 +0200 | |
107 | +++ gdb-7.9.50.20150531/gdb/main.c 2015-05-31 20:10:05.095845935 +0200 | |
108 | @@ -435,6 +435,37 @@ typedef struct cmdarg { | |
109 | /* Define type VEC (cmdarg_s). */ | |
110 | DEF_VEC_O (cmdarg_s); | |
111 | ||
112 | +/* Call exec_file_attach. If it detected FILENAME is a core file call | |
113 | + core_file_command. Print the original exec_file_attach error only if | |
114 | + core_file_command failed to find a matching executable. */ | |
115 | + | |
116 | +static void | |
117 | +exec_or_core_file_attach (const char *filename, int from_tty) | |
118 | +{ | |
119 | + volatile struct gdb_exception e; | |
120 | + | |
121 | + gdb_assert (exec_bfd == NULL); | |
122 | + | |
123 | + TRY | |
124 | + { | |
125 | + exec_file_attach (filename, from_tty); | |
126 | + } | |
127 | + CATCH (e, RETURN_MASK_ALL) | |
128 | + { | |
129 | + if (e.error == IS_CORE_ERROR) | |
130 | + { | |
131 | + core_file_command ((char *) filename, from_tty); | |
132 | + | |
133 | + /* Iff the core file found its executable suppress the error message | |
134 | + from exec_file_attach. */ | |
135 | + if (exec_bfd != NULL) | |
136 | + return; | |
137 | + } | |
138 | + throw_exception (e); | |
139 | + } | |
140 | + END_CATCH | |
141 | +} | |
142 | + | |
143 | static int | |
144 | captured_main (void *data) | |
145 | { | |
146 | @@ -923,6 +954,8 @@ captured_main (void *data) | |
147 | { | |
148 | symarg = argv[optind]; | |
149 | execarg = argv[optind]; | |
150 | + if (optind + 1 == argc && corearg == NULL) | |
151 | + corearg = argv[optind]; | |
152 | optind++; | |
153 | } | |
154 | ||
155 | @@ -1080,11 +1113,25 @@ captured_main (void *data) | |
156 | && symarg != NULL | |
157 | && strcmp (execarg, symarg) == 0) | |
158 | { | |
159 | + catch_command_errors_const_ftype *func; | |
160 | + | |
161 | + /* Call exec_or_core_file_attach only if the file was specified as | |
162 | + a command line argument (and not an a command line option). */ | |
163 | + if (corearg != NULL && strcmp (corearg, execarg) == 0) | |
164 | + { | |
165 | + func = exec_or_core_file_attach; | |
166 | + corearg = NULL; | |
167 | + } | |
168 | + else | |
169 | + func = exec_file_attach; | |
170 | + | |
171 | /* The exec file and the symbol-file are the same. If we can't | |
172 | open it, better only print one error message. | |
173 | - catch_command_errors returns non-zero on success! */ | |
174 | - if (catch_command_errors_const (exec_file_attach, execarg, | |
175 | - !batch_flag)) | |
176 | + catch_command_errors returns non-zero on success! | |
177 | + Do not load EXECARG as a symbol file if it has been already processed | |
178 | + as a core file. */ | |
179 | + if (catch_command_errors_const (func, execarg, !batch_flag) | |
180 | + && core_bfd == NULL) | |
181 | catch_command_errors_const (symbol_file_add_main, symarg, | |
182 | !batch_flag); | |
183 | } | |
184 | Index: gdb-7.9.50.20150531/gdb/common/common-exceptions.h | |
185 | =================================================================== | |
186 | --- gdb-7.9.50.20150531.orig/gdb/common/common-exceptions.h 2015-05-31 03:48:29.000000000 +0200 | |
187 | +++ gdb-7.9.50.20150531/gdb/common/common-exceptions.h 2015-05-31 20:07:35.093878692 +0200 | |
188 | @@ -105,6 +105,9 @@ enum errors { | |
189 | "_ERROR" is appended to the name. */ | |
190 | MAX_COMPLETIONS_REACHED_ERROR, | |
191 | ||
192 | + /* Attempt to load a core file as executable. */ | |
193 | + IS_CORE_ERROR, | |
194 | + | |
195 | /* Add more errors here. */ | |
196 | NR_ERRORS | |
197 | }; |