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