1 /* Copyright (C) 2009-2013 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/ptrace.h>
24 #include <sys/piddef.h> /* Provides PIDGET, TIDGET, BUILDPID, etc. */
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
31 int using_threads
= 1;
33 /* Per-process private data. */
35 struct process_info_private
37 /* The PTID obtained from the last wait performed on this process.
38 Initialized to null_ptid until the first wait is performed. */
39 ptid_t last_wait_event_ptid
;
42 /* Print a debug trace on standard output if debug_threads is set. */
45 lynx_debug (char *string
, ...)
52 va_start (args
, string
);
53 fprintf (stderr
, "DEBUG(lynx): ");
54 vfprintf (stderr
, string
, args
);
55 fprintf (stderr
, "\n");
59 /* Build a ptid_t given a PID and a LynxOS TID. */
62 lynx_ptid_build (int pid
, long tid
)
64 /* brobecker/2010-06-21: It looks like the LWP field in ptids
65 should be distinct for each thread (see write_ptid where it
66 writes the thread ID from the LWP). So instead of storing
67 the LynxOS tid in the tid field of the ptid, we store it in
69 return ptid_build (pid
, tid
, 0);
72 /* Return the process ID of the given PTID.
74 This function has little reason to exist, it's just a wrapper around
75 ptid_get_pid. But since we have a getter function for the lynxos
76 ptid, it feels cleaner to have a getter for the pid as well. */
79 lynx_ptid_get_pid (ptid_t ptid
)
81 return ptid_get_pid (ptid
);
84 /* Return the LynxOS tid of the given PTID. */
87 lynx_ptid_get_tid (ptid_t ptid
)
89 /* See lynx_ptid_build: The LynxOS tid is stored inside the lwp field
91 return ptid_get_lwp (ptid
);
94 /* For a given PTID, return the associated PID as known by the LynxOS
98 lynx_ptrace_pid_from_ptid (ptid_t ptid
)
100 return BUILDPID (lynx_ptid_get_pid (ptid
), lynx_ptid_get_tid (ptid
));
103 /* Return a string image of the ptrace REQUEST number. */
106 ptrace_request_to_str (int request
)
108 #define CASE(X) case X: return #X
111 CASE(PTRACE_TRACEME
);
112 CASE(PTRACE_PEEKTEXT
);
113 CASE(PTRACE_PEEKDATA
);
114 CASE(PTRACE_PEEKUSER
);
115 CASE(PTRACE_POKETEXT
);
116 CASE(PTRACE_POKEDATA
);
117 CASE(PTRACE_POKEUSER
);
120 CASE(PTRACE_SINGLESTEP
);
123 CASE(PTRACE_GETREGS
);
124 CASE(PTRACE_SETREGS
);
125 CASE(PTRACE_GETFPREGS
);
126 CASE(PTRACE_SETFPREGS
);
127 CASE(PTRACE_READDATA
);
128 CASE(PTRACE_WRITEDATA
);
129 CASE(PTRACE_READTEXT
);
130 CASE(PTRACE_WRITETEXT
);
131 CASE(PTRACE_GETFPAREGS
);
132 CASE(PTRACE_SETFPAREGS
);
133 CASE(PTRACE_GETWINDOW
);
134 CASE(PTRACE_SETWINDOW
);
135 CASE(PTRACE_SYSCALL
);
136 CASE(PTRACE_DUMPCORE
);
137 CASE(PTRACE_SETWRBKPT
);
138 CASE(PTRACE_SETACBKPT
);
139 CASE(PTRACE_CLRBKPT
);
140 CASE(PTRACE_GET_UCODE
);
159 #ifdef PTRACE_PEEKUSP
160 CASE(PTRACE_PEEKUSP
);
162 #ifdef PTRACE_POKEUSP
163 CASE(PTRACE_POKEUSP
);
165 CASE(PTRACE_PEEKTHREAD
);
166 CASE(PTRACE_THREADUSER
);
168 CASE(PTRACE_FPWRITE
);
170 CASE(PTRACE_CONT_ONE
);
171 CASE(PTRACE_KILL_ONE
);
172 CASE(PTRACE_SINGLESTEP_ONE
);
173 CASE(PTRACE_GETLOADINFO
);
174 CASE(PTRACE_GETTRACESIG
);
175 #ifdef PTRACE_GETTHREADLIST
176 CASE(PTRACE_GETTHREADLIST
);
181 return "<unknown-request>";
184 /* A wrapper around ptrace that allows us to print debug traces of
185 ptrace calls if debug traces are activated. */
188 lynx_ptrace (int request
, ptid_t ptid
, int addr
, int data
, int addr2
)
191 const int pid
= lynx_ptrace_pid_from_ptid (ptid
);
195 fprintf (stderr
, "PTRACE (%s, pid=%d(pid=%d, tid=%d), addr=0x%x, "
196 "data=0x%x, addr2=0x%x)",
197 ptrace_request_to_str (request
), pid
, PIDGET (pid
), TIDGET (pid
),
199 result
= ptrace (request
, pid
, addr
, data
, addr2
);
202 fprintf (stderr
, " -> %d (=0x%x)\n", result
, result
);
208 /* Call add_process with the given parameters, and initializes
209 the process' private data. */
211 static struct process_info
*
212 lynx_add_process (int pid
, int attached
)
214 struct process_info
*proc
;
216 proc
= add_process (pid
, attached
);
217 proc
->private = xcalloc (1, sizeof (*proc
->private));
218 proc
->private->last_wait_event_ptid
= null_ptid
;
223 /* Implement the create_inferior method of the target_ops vector. */
226 lynx_create_inferior (char *program
, char **allargs
)
230 lynx_debug ("lynx_create_inferior ()");
234 perror_with_name ("fork");
240 /* Switch child to its own process group so that signals won't
241 directly affect gdbserver. */
244 ioctl (0, TIOCSPGRP
, &pgrp
);
245 lynx_ptrace (PTRACE_TRACEME
, null_ptid
, 0, 0, 0);
246 execv (program
, allargs
);
247 fprintf (stderr
, "Cannot exec %s: %s.\n", program
, strerror (errno
));
252 lynx_add_process (pid
, 0);
253 /* Do not add the process thread just yet, as we do not know its tid.
254 We will add it later, during the wait for the STOP event corresponding
255 to the lynx_ptrace (PTRACE_TRACEME) call above. */
259 /* Implement the attach target_ops method. */
262 lynx_attach (unsigned long pid
)
264 ptid_t ptid
= lynx_ptid_build (pid
, 0);
266 if (lynx_ptrace (PTRACE_ATTACH
, ptid
, 0, 0, 0) != 0)
267 error ("Cannot attach to process %lu: %s (%d)\n", pid
,
268 strerror (errno
), errno
);
270 lynx_add_process (pid
, 1);
271 add_thread (ptid
, NULL
);
276 /* Implement the resume target_ops method. */
279 lynx_resume (struct thread_resume
*resume_info
, size_t n
)
281 /* FIXME: Assume for now that n == 1. */
282 ptid_t ptid
= resume_info
[0].thread
;
283 const int request
= (resume_info
[0].kind
== resume_step
284 ? PTRACE_SINGLESTEP
: PTRACE_CONT
);
285 const int signal
= resume_info
[0].sig
;
287 /* If given a minus_one_ptid, then try using the current_process'
288 private->last_wait_event_ptid. On most LynxOS versions,
289 using any of the process' thread works well enough, but
290 LynxOS 178 is a little more sensitive, and triggers some
291 unexpected signals (Eg SIG61) when we resume the inferior
292 using a different thread. */
293 if (ptid_equal (ptid
, minus_one_ptid
))
294 ptid
= current_process()->private->last_wait_event_ptid
;
296 /* The ptid might still be minus_one_ptid; this can happen between
297 the moment we create the inferior or attach to a process, and
298 the moment we resume its execution for the first time. It is
299 fine to use the current_inferior's ptid in those cases. */
300 if (ptid_equal (ptid
, minus_one_ptid
))
301 ptid
= thread_to_gdb_id (current_inferior
);
303 regcache_invalidate ();
306 lynx_ptrace (request
, ptid
, 1, signal
, 0);
308 perror_with_name ("ptrace");
311 /* Resume the execution of the given PTID. */
314 lynx_continue (ptid_t ptid
)
316 struct thread_resume resume_info
;
318 resume_info
.thread
= ptid
;
319 resume_info
.kind
= resume_continue
;
322 lynx_resume (&resume_info
, 1);
325 /* A wrapper around waitpid that handles the various idiosyncrasies
326 of LynxOS' waitpid. */
329 lynx_waitpid (int pid
, int *stat_loc
)
335 ret
= waitpid (pid
, stat_loc
, WNOHANG
);
338 /* An ECHILD error is not indicative of a real problem.
339 It happens for instance while waiting for the inferior
340 to stop after attaching to it. */
342 perror_with_name ("waitpid (WNOHANG)");
346 /* No event with WNOHANG. See if there is one with WUNTRACED. */
347 ret
= waitpid (pid
, stat_loc
, WNOHANG
| WUNTRACED
);
350 /* An ECHILD error is not indicative of a real problem.
351 It happens for instance while waiting for the inferior
352 to stop after attaching to it. */
354 perror_with_name ("waitpid (WNOHANG|WUNTRACED)");
363 /* Implement the wait target_ops method. */
366 lynx_wait_1 (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
373 if (ptid_equal (ptid
, minus_one_ptid
))
374 pid
= lynx_ptid_get_pid (thread_to_gdb_id (current_inferior
));
376 pid
= BUILDPID (lynx_ptid_get_pid (ptid
), lynx_ptid_get_tid (ptid
));
380 ret
= lynx_waitpid (pid
, &wstat
);
381 new_ptid
= lynx_ptid_build (ret
, ((union wait
*) &wstat
)->w_tid
);
382 find_process_pid (ret
)->private->last_wait_event_ptid
= new_ptid
;
384 /* If this is a new thread, then add it now. The reason why we do
385 this here instead of when handling new-thread events is because
386 we need to add the thread associated to the "main" thread - even
387 for non-threaded applications where the new-thread events are not
389 if (!find_thread_ptid (new_ptid
))
391 lynx_debug ("New thread: (pid = %d, tid = %d)",
392 lynx_ptid_get_pid (new_ptid
), lynx_ptid_get_tid (new_ptid
));
393 add_thread (new_ptid
, NULL
);
396 if (WIFSTOPPED (wstat
))
398 status
->kind
= TARGET_WAITKIND_STOPPED
;
399 status
->value
.integer
= gdb_signal_from_host (WSTOPSIG (wstat
));
400 lynx_debug ("process stopped with signal: %d",
401 status
->value
.integer
);
403 else if (WIFEXITED (wstat
))
405 status
->kind
= TARGET_WAITKIND_EXITED
;
406 status
->value
.integer
= WEXITSTATUS (wstat
);
407 lynx_debug ("process exited with code: %d", status
->value
.integer
);
409 else if (WIFSIGNALED (wstat
))
411 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
412 status
->value
.integer
= gdb_signal_from_host (WTERMSIG (wstat
));
413 lynx_debug ("process terminated with code: %d",
414 status
->value
.integer
);
418 /* Not sure what happened if we get here, or whether we can
419 in fact get here. But if we do, handle the event the best
421 status
->kind
= TARGET_WAITKIND_STOPPED
;
422 status
->value
.integer
= gdb_signal_from_host (0);
423 lynx_debug ("unknown event ????");
426 /* SIGTRAP events are generated for situations other than single-step/
427 breakpoint events (Eg. new-thread events). Handle those other types
428 of events, and resume the execution if necessary. */
429 if (status
->kind
== TARGET_WAITKIND_STOPPED
430 && status
->value
.integer
== GDB_SIGNAL_TRAP
)
432 const int realsig
= lynx_ptrace (PTRACE_GETTRACESIG
, new_ptid
, 0, 0, 0);
434 lynx_debug ("(realsig = %d)", realsig
);
438 /* We just added the new thread above. No need to do anything
439 further. Just resume the execution again. */
440 lynx_continue (new_ptid
);
444 remove_thread (find_thread_ptid (new_ptid
));
445 lynx_continue (new_ptid
);
453 /* A wrapper around lynx_wait_1 that also prints debug traces when
454 such debug traces have been activated. */
457 lynx_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
461 lynx_debug ("lynx_wait (pid = %d, tid = %ld)",
462 lynx_ptid_get_pid (ptid
), lynx_ptid_get_tid (ptid
));
463 new_ptid
= lynx_wait_1 (ptid
, status
, options
);
464 lynx_debug (" -> (pid=%d, tid=%ld, status->kind = %d)",
465 lynx_ptid_get_pid (new_ptid
), lynx_ptid_get_tid (new_ptid
),
470 /* Implement the kill target_ops method. */
475 ptid_t ptid
= lynx_ptid_build (pid
, 0);
476 struct target_waitstatus status
;
477 struct process_info
*process
;
479 process
= find_process_pid (pid
);
483 lynx_ptrace (PTRACE_KILL
, ptid
, 0, 0, 0);
484 lynx_wait (ptid
, &status
, 0);
485 the_target
->mourn (process
);
489 /* Implement the detach target_ops method. */
492 lynx_detach (int pid
)
494 ptid_t ptid
= lynx_ptid_build (pid
, 0);
495 struct process_info
*process
;
497 process
= find_process_pid (pid
);
501 lynx_ptrace (PTRACE_DETACH
, ptid
, 0, 0, 0);
502 the_target
->mourn (process
);
506 /* Implement the mourn target_ops method. */
509 lynx_mourn (struct process_info
*proc
)
511 /* Free our private data. */
512 free (proc
->private);
513 proc
->private = NULL
;
518 /* Implement the join target_ops method. */
523 /* The PTRACE_DETACH is sufficient to detach from the process.
524 So no need to do anything extra. */
527 /* Implement the thread_alive target_ops method. */
530 lynx_thread_alive (ptid_t ptid
)
532 /* The list of threads is updated at the end of each wait, so it
533 should be up to date. No need to re-fetch it. */
534 return (find_thread_ptid (ptid
) != NULL
);
537 /* Implement the fetch_registers target_ops method. */
540 lynx_fetch_registers (struct regcache
*regcache
, int regno
)
542 struct lynx_regset_info
*regset
= lynx_target_regsets
;
543 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
545 lynx_debug ("lynx_fetch_registers (regno = %d)", regno
);
547 while (regset
->size
>= 0)
552 buf
= xmalloc (regset
->size
);
553 res
= lynx_ptrace (regset
->get_request
, inferior_ptid
, (int) buf
, 0, 0);
556 regset
->store_function (regcache
, buf
);
562 /* Implement the store_registers target_ops method. */
565 lynx_store_registers (struct regcache
*regcache
, int regno
)
567 struct lynx_regset_info
*regset
= lynx_target_regsets
;
568 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
570 lynx_debug ("lynx_store_registers (regno = %d)", regno
);
572 while (regset
->size
>= 0)
577 buf
= xmalloc (regset
->size
);
578 res
= lynx_ptrace (regset
->get_request
, inferior_ptid
, (int) buf
, 0, 0);
581 /* Then overlay our cached registers on that. */
582 regset
->fill_function (regcache
, buf
);
583 /* Only now do we write the register set. */
584 res
= lynx_ptrace (regset
->set_request
, inferior_ptid
, (int) buf
,
594 /* Implement the read_memory target_ops method. */
597 lynx_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
599 /* On LynxOS, memory reads needs to be performed in chunks the size
600 of int types, and they should also be aligned accordingly. */
602 const int xfer_size
= sizeof (buf
);
603 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) xfer_size
;
604 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
606 while (addr
< memaddr
+ len
)
613 skip
= memaddr
- addr
;
614 if (addr
+ xfer_size
> memaddr
+ len
)
615 truncate
= addr
+ xfer_size
- memaddr
- len
;
616 buf
= lynx_ptrace (PTRACE_PEEKTEXT
, inferior_ptid
, addr
, 0, 0);
619 memcpy (myaddr
+ (addr
- memaddr
) + skip
, (gdb_byte
*) &buf
+ skip
,
620 xfer_size
- skip
- truncate
);
627 /* Implement the write_memory target_ops method. */
630 lynx_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
632 /* On LynxOS, memory writes needs to be performed in chunks the size
633 of int types, and they should also be aligned accordingly. */
635 const int xfer_size
= sizeof (buf
);
636 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) xfer_size
;
637 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
639 while (addr
< memaddr
+ len
)
645 skip
= memaddr
- addr
;
646 if (addr
+ xfer_size
> memaddr
+ len
)
647 truncate
= addr
+ xfer_size
- memaddr
- len
;
648 if (skip
> 0 || truncate
> 0)
649 /* We need to read the memory at this address in order to preserve
650 the data that we are not overwriting. */
651 lynx_read_memory (addr
, (unsigned char *) &buf
, xfer_size
);
654 memcpy ((gdb_byte
*) &buf
+ skip
, myaddr
+ (addr
- memaddr
) + skip
,
655 xfer_size
- skip
- truncate
);
657 lynx_ptrace (PTRACE_POKETEXT
, inferior_ptid
, addr
, buf
, 0);
666 /* Implement the kill_request target_ops method. */
669 lynx_request_interrupt (void)
671 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
673 kill (lynx_ptid_get_pid (inferior_ptid
), SIGINT
);
676 /* The LynxOS target_ops vector. */
678 static struct target_ops lynx_target_ops
= {
679 lynx_create_inferior
,
688 lynx_fetch_registers
,
689 lynx_store_registers
,
690 NULL
, /* prepare_to_access_memory */
691 NULL
, /* done_accessing_memory */
694 NULL
, /* look_up_symbols */
695 lynx_request_interrupt
,
696 NULL
, /* read_auxv */
697 NULL
, /* insert_point */
698 NULL
, /* remove_point */
699 NULL
, /* stopped_by_watchpoint */
700 NULL
, /* stopped_data_address */
701 NULL
, /* read_offsets */
702 NULL
, /* get_tls_address */
703 NULL
, /* qxfer_spu */
704 NULL
, /* hostio_last_error */
705 NULL
, /* qxfer_osdata */
706 NULL
, /* qxfer_siginfo */
707 NULL
, /* supports_non_stop */
709 NULL
, /* start_non_stop */
710 NULL
, /* supports_multi_process */
711 NULL
, /* handle_monitor_command */
715 initialize_low (void)
717 set_target_ops (&lynx_target_ops
);
718 the_low_target
.arch_setup ();