Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Target-vector operations for controlling Mac applications, for GDB. |
4e052eda | 2 | Copyright 1995, 2001 Free Software Foundation, Inc. |
c906108c SS |
3 | Written by Stan Shebs. Contributed by Cygnus Support. |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without eve nthe implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | /* Note that because all the available Mac compilers are ANSI or very | |
23 | close, and this is a native-only file, the code may be purely ANSI. */ | |
24 | ||
25 | #include "defs.h" | |
26 | #include "frame.h" /* required by inferior.h */ | |
27 | #include "inferior.h" | |
28 | #include "target.h" | |
03f2053f | 29 | #include "gdb_wait.h" |
c906108c SS |
30 | #include "gdbcore.h" |
31 | #include "command.h" | |
32 | #include <signal.h> | |
33 | #include <sys/types.h> | |
34 | #include <fcntl.h> | |
35 | #include "buildsym.h" | |
36 | #include "gdb_string.h" | |
37 | #include "gdbthread.h" | |
38 | #include "gdbcmd.h" | |
4e052eda | 39 | #include "regcache.h" |
c906108c SS |
40 | |
41 | #include <Processes.h> | |
42 | ||
43 | /* We call the functions "child_..." rather than "mac_..." so no one | |
44 | is tempted to try to link this with other native-only code. */ | |
45 | ||
46 | /* Forward declaration */ | |
47 | ||
48 | extern struct target_ops child_ops; | |
49 | ||
a14ed312 | 50 | static void child_stop (void); |
c906108c SS |
51 | |
52 | static void | |
53 | child_fetch_inferior_registers (int r) | |
54 | { | |
55 | if (r < 0) | |
56 | { | |
57 | for (r = 0; r < NUM_REGS; r++) | |
58 | child_fetch_inferior_registers (r); | |
59 | } | |
60 | else | |
61 | { | |
62 | supply_register (r, 0); | |
63 | } | |
64 | } | |
65 | ||
66 | static void | |
67 | child_store_inferior_registers (int r) | |
68 | { | |
69 | if (r < 0) | |
70 | { | |
71 | for (r = 0; r < NUM_REGS; r++) | |
72 | child_store_inferior_registers (r); | |
73 | } | |
74 | else | |
75 | { | |
76 | read_register_gen (r, 0); | |
77 | } | |
78 | } | |
79 | ||
80 | static int | |
81 | child_wait (int pid, struct target_waitstatus *ourstatus) | |
82 | { | |
83 | } | |
84 | ||
85 | /* Attach to process PID, then initialize for debugging it. */ | |
86 | ||
87 | static void | |
fba45db2 | 88 | child_attach (char *args, int from_tty) |
c906108c SS |
89 | { |
90 | ProcessSerialNumber psn; | |
91 | ProcessInfoRec inforec; | |
92 | Str31 name; | |
93 | FSSpecPtr fsspec; | |
94 | OSType code; | |
95 | int pid; | |
96 | char *exec_file; | |
97 | ||
98 | if (!args) | |
99 | error_no_arg ("process-id to attach"); | |
100 | ||
101 | pid = atoi (args); | |
102 | ||
103 | psn.highLongOfPSN = 0; | |
104 | psn.lowLongOfPSN = pid; | |
105 | ||
c5aa993b | 106 | inforec.processInfoLength = sizeof (ProcessInfoRec); |
c906108c SS |
107 | inforec.processName = name; |
108 | inforec.processAppSpec = fsspec; | |
109 | ||
110 | if (GetProcessInformation (&psn, &inforec) == noErr) | |
111 | { | |
112 | if (from_tty) | |
113 | { | |
114 | exec_file = (char *) get_exec_file (0); | |
115 | ||
116 | if (exec_file) | |
117 | printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, | |
118 | target_pid_to_str (pid)); | |
119 | else | |
120 | printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid)); | |
121 | ||
122 | gdb_flush (gdb_stdout); | |
123 | } | |
124 | /* Do we need to do anything special? */ | |
125 | attach_flag = 1; | |
126 | inferior_pid = pid; | |
127 | push_target (&child_ops); | |
128 | } | |
129 | } | |
130 | ||
131 | static void | |
fba45db2 | 132 | child_detach (char *args, int from_tty) |
c906108c SS |
133 | { |
134 | char *exec_file; | |
135 | ||
136 | if (from_tty) | |
137 | { | |
138 | exec_file = get_exec_file (0); | |
139 | if (exec_file == 0) | |
140 | exec_file = ""; | |
141 | printf_unfiltered ("Detaching from program: %s %s\n", exec_file, | |
142 | target_pid_to_str (inferior_pid)); | |
143 | gdb_flush (gdb_stdout); | |
144 | } | |
145 | inferior_pid = 0; | |
146 | unpush_target (&child_ops); | |
147 | } | |
148 | ||
149 | /* Print status information about what we're accessing. */ | |
150 | ||
151 | static void | |
fba45db2 | 152 | child_files_info (struct target_ops *ignore) |
c906108c SS |
153 | { |
154 | printf_unfiltered ("\tUsing the running image of %s %s.\n", | |
155 | attach_flag ? "attached" : "child", target_pid_to_str (inferior_pid)); | |
156 | } | |
157 | ||
158 | /* ARGSUSED */ | |
159 | static void | |
fba45db2 | 160 | child_open (char *arg, int from_tty) |
c906108c SS |
161 | { |
162 | error ("Use the \"run\" command to start a Mac application."); | |
163 | } | |
164 | ||
165 | /* Start an inferior Mac program and sets inferior_pid to its pid. | |
166 | EXEC_FILE is the file to run. | |
167 | ALLARGS is a string containing the arguments to the program. | |
168 | ENV is the environment vector to pass. Errors reported with error(). */ | |
169 | ||
170 | static void | |
fba45db2 | 171 | child_create_inferior (char *exec_file, char *allargs, char **env) |
c906108c SS |
172 | { |
173 | LaunchParamBlockRec launchparms; | |
174 | FSSpec fsspec; | |
175 | OSErr launch_err; | |
176 | ||
177 | if (!exec_file) | |
178 | { | |
179 | error ("No executable specified, use `target exec'.\n"); | |
180 | } | |
181 | ||
182 | launchparms.launchBlockID = extendedBlock; | |
183 | launchparms.launchEPBLength = extendedBlockLen; | |
184 | launchparms.launchFileFlags = 0; | |
185 | launchparms.launchControlFlags = launchContinue | launchNoFileFlags; | |
186 | fsspec.vRefNum = 0; | |
187 | fsspec.parID = 0; | |
c5aa993b JM |
188 | strcpy (fsspec.name + 1, exec_file); |
189 | fsspec.name[0] = strlen (exec_file); | |
c906108c SS |
190 | launchparms.launchAppSpec = &fsspec; |
191 | launchparms.launchAppParameters = nil; | |
192 | ||
193 | launch_err = LaunchApplication (&launchparms); | |
194 | ||
c5aa993b | 195 | if (launch_err == 999 /*memFullErr */ ) |
c906108c SS |
196 | { |
197 | error ("Not enough memory to launch %s\n", exec_file); | |
198 | } | |
199 | else if (launch_err != noErr) | |
200 | { | |
201 | error ("Error launching %s, code %d\n", exec_file, launch_err); | |
202 | } | |
203 | ||
204 | inferior_pid = launchparms.launchProcessSN.lowLongOfPSN; | |
205 | /* FIXME be sure that high long of PSN is 0 */ | |
206 | ||
207 | push_target (&child_ops); | |
208 | init_wait_for_inferior (); | |
209 | clear_proceed_status (); | |
210 | ||
2acceee2 | 211 | /* proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0); */ |
c906108c SS |
212 | } |
213 | ||
214 | static void | |
fba45db2 | 215 | child_mourn_inferior (void) |
c906108c SS |
216 | { |
217 | unpush_target (&child_ops); | |
218 | generic_mourn_inferior (); | |
219 | } | |
220 | ||
221 | static void | |
fba45db2 | 222 | child_stop (void) |
c906108c SS |
223 | { |
224 | } | |
225 | ||
226 | int | |
227 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, | |
c5aa993b | 228 | int write, struct target_ops *target) |
c906108c SS |
229 | { |
230 | int i; | |
231 | ||
232 | for (i = 0; i < len; ++i) | |
233 | { | |
234 | if (write) | |
235 | { | |
236 | ((char *) memaddr)[i] = myaddr[i]; | |
237 | } | |
238 | else | |
239 | { | |
240 | myaddr[i] = ((char *) memaddr)[i]; | |
241 | } | |
242 | } | |
243 | return len; | |
244 | } | |
245 | ||
246 | void | |
247 | child_kill_inferior (void) | |
248 | { | |
249 | } | |
250 | ||
251 | void | |
252 | child_resume (int pid, int step, enum target_signal signal) | |
253 | { | |
254 | } | |
255 | ||
256 | static void | |
fba45db2 | 257 | child_prepare_to_store (void) |
c906108c SS |
258 | { |
259 | /* Do nothing, since we can store individual regs */ | |
260 | } | |
261 | ||
262 | static int | |
fba45db2 | 263 | child_can_run (void) |
c906108c SS |
264 | { |
265 | return 1; | |
266 | } | |
267 | ||
268 | static void | |
fba45db2 | 269 | child_close (void) |
c906108c SS |
270 | { |
271 | } | |
272 | ||
273 | static void | |
fba45db2 | 274 | info_proc (char *args, int from_tty) |
c906108c SS |
275 | { |
276 | ProcessSerialNumber psn; | |
277 | ProcessInfoRec inforec; | |
278 | Str31 name; | |
279 | FSSpecPtr fsspec; | |
280 | OSType code; | |
281 | ||
282 | /* Eventually use args, but not right now. */ | |
283 | ||
284 | psn.highLongOfPSN = 0; | |
285 | psn.lowLongOfPSN = kNoProcess; | |
286 | ||
c5aa993b | 287 | inforec.processInfoLength = sizeof (ProcessInfoRec); |
c906108c SS |
288 | inforec.processName = name; |
289 | inforec.processAppSpec = fsspec; | |
290 | ||
291 | printf_filtered ("Process Name Sgnt Type PSN Loc Size FreeMem Time\n"); | |
292 | ||
293 | while (GetNextProcess (&psn) == noErr) | |
294 | { | |
295 | if (GetProcessInformation (&psn, &inforec) == noErr) | |
296 | { | |
297 | name[name[0] + 1] = '\0'; | |
298 | printf_filtered ("%-32.32s", name + 1); | |
299 | code = inforec.processSignature; | |
300 | printf_filtered (" %c%c%c%c", | |
301 | (code >> 24) & 0xff, | |
302 | (code >> 16) & 0xff, | |
c5aa993b JM |
303 | (code >> 8) & 0xff, |
304 | (code >> 0) & 0xff); | |
c906108c SS |
305 | code = inforec.processType; |
306 | printf_filtered (" %c%c%c%c", | |
307 | (code >> 24) & 0xff, | |
308 | (code >> 16) & 0xff, | |
c5aa993b JM |
309 | (code >> 8) & 0xff, |
310 | (code >> 0) & 0xff); | |
c906108c SS |
311 | if (psn.highLongOfPSN == 0) |
312 | printf_filtered (" %9d", psn.lowLongOfPSN); | |
313 | else | |
314 | printf_filtered (" %9d,%9d\n", | |
315 | psn.highLongOfPSN, psn.lowLongOfPSN); | |
316 | printf_filtered (" 0x%x", inforec.processLocation); | |
317 | printf_filtered (" %9d", inforec.processSize); | |
318 | printf_filtered (" %9d", inforec.processFreeMem); | |
319 | printf_filtered (" %9d", inforec.processActiveTime); | |
320 | printf_filtered ("\n"); | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
c5aa993b | 325 | struct target_ops child_ops; |
c906108c | 326 | |
c5aa993b JM |
327 | static void |
328 | init_child_ops (void) | |
c906108c | 329 | { |
c5aa993b JM |
330 | child_ops.to_shortname = "mac"; |
331 | child_ops.to_longname = "MacOS application"; | |
332 | child_ops.to_doc = "MacOS application (started by the \"run\" command)."; | |
333 | child_ops.to_open = child_open; | |
334 | child_ops.to_close = child_close; | |
335 | child_ops.to_attach = child_attach; | |
c906108c | 336 | child_ops.to_post_attach = NULL; |
c5aa993b JM |
337 | child_ops.to_require_attach = NULL; /* to_require_attach */ |
338 | child_ops.to_detach = child_detach; | |
339 | child_ops.to_require_detach = NULL; /* to_require_detach */ | |
340 | child_ops.to_resume = child_resume; | |
341 | child_ops.to_wait = child_wait; | |
342 | child_ops.to_post_wait = NULL; /* to_post_wait */ | |
343 | child_ops.to_fetch_registers = child_fetch_inferior_registers; | |
344 | child_ops.to_store_registers = child_store_inferior_registers; | |
345 | child_ops.to_prepare_to_store = child_prepare_to_store; | |
346 | child_ops.to_xfer_memory = child_xfer_memory; | |
347 | child_ops.to_files_info = child_files_info; | |
348 | child_ops.to_insert_breakpoint = memory_insert_breakpoint; | |
349 | child_ops.to_remove_breakpoint = memory_remove_breakpoint; | |
350 | child_ops.to_terminal_init = 0; | |
351 | child_ops.to_terminal_inferior = 0; | |
352 | child_ops.to_terminal_ours_for_output = 0; | |
353 | child_ops.to_terminal_ours = 0; | |
354 | child_ops.to_terminal_info = 0; | |
355 | child_ops.to_kill = child_kill_inferior; | |
356 | child_ops.to_load = 0; | |
357 | child_ops.to_lookup_symbol = 0; | |
c906108c | 358 | child_ops.to_create_inferior = child_create_inferior; |
c5aa993b JM |
359 | child_ops.to_post_startup_inferior = NULL; /* to_post_startup_inferior */ |
360 | child_ops.to_acknowledge_created_inferior = NULL; /* to_acknowledge_created_inferior */ | |
361 | child_ops.to_clone_and_follow_inferior = NULL; /* to_clone_and_follow_inferior */ | |
362 | child_ops.to_post_follow_inferior_by_clone = NULL; /* to_post_follow_inferior_by_clone */ | |
c906108c SS |
363 | child_ops.to_insert_fork_catchpoint = NULL; |
364 | child_ops.to_remove_fork_catchpoint = NULL; | |
365 | child_ops.to_insert_vfork_catchpoint = NULL; | |
366 | child_ops.to_remove_vfork_catchpoint = NULL; | |
c5aa993b JM |
367 | child_ops.to_has_forked = NULL; /* to_has_forked */ |
368 | child_ops.to_has_vforked = NULL; /* to_has_vforked */ | |
c906108c | 369 | child_ops.to_can_follow_vfork_prior_to_exec = NULL; |
c5aa993b | 370 | child_ops.to_post_follow_vfork = NULL; /* to_post_follow_vfork */ |
c906108c SS |
371 | child_ops.to_insert_exec_catchpoint = NULL; |
372 | child_ops.to_remove_exec_catchpoint = NULL; | |
373 | child_ops.to_has_execd = NULL; | |
374 | child_ops.to_reported_exec_events_per_exec_call = NULL; | |
375 | child_ops.to_has_exited = NULL; | |
c5aa993b JM |
376 | child_ops.to_mourn_inferior = child_mourn_inferior; |
377 | child_ops.to_can_run = child_can_run; | |
378 | child_ops.to_notice_signals = 0; | |
379 | child_ops.to_thread_alive = 0; | |
380 | child_ops.to_stop = child_stop; | |
381 | child_ops.to_pid_to_exec_file = NULL; /* to_pid_to_exec_file */ | |
c906108c | 382 | child_ops.to_core_file_to_sym_file = NULL; |
c5aa993b JM |
383 | child_ops.to_stratum = process_stratum; |
384 | child_ops.DONT_USE = 0; | |
385 | child_ops.to_has_all_memory = 1; | |
386 | child_ops.to_has_memory = 1; | |
387 | child_ops.to_has_stack = 1; | |
388 | child_ops.to_has_registers = 1; | |
389 | child_ops.to_has_execution = 1; | |
390 | child_ops.to_sections = 0; | |
391 | child_ops.to_sections_end = 0; | |
392 | child_ops.to_magic = OPS_MAGIC; | |
c906108c SS |
393 | }; |
394 | ||
395 | void | |
fba45db2 | 396 | _initialize_mac_nat (void) |
c906108c | 397 | { |
c5aa993b | 398 | init_child_ops (); |
c906108c SS |
399 | |
400 | add_info ("proc", info_proc, | |
401 | "Show information about processes."); | |
402 | } |