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>
30 #include "filestuff.h"
32 int using_threads
= 1;
34 const struct target_desc
*lynx_tdesc
;
36 /* Per-process private data. */
38 struct process_info_private
40 /* The PTID obtained from the last wait performed on this process.
41 Initialized to null_ptid until the first wait is performed. */
42 ptid_t last_wait_event_ptid
;
45 /* Print a debug trace on standard output if debug_threads is set. */
48 lynx_debug (char *string
, ...)
55 va_start (args
, string
);
56 fprintf (stderr
, "DEBUG(lynx): ");
57 vfprintf (stderr
, string
, args
);
58 fprintf (stderr
, "\n");
62 /* Build a ptid_t given a PID and a LynxOS TID. */
65 lynx_ptid_build (int pid
, long tid
)
67 /* brobecker/2010-06-21: It looks like the LWP field in ptids
68 should be distinct for each thread (see write_ptid where it
69 writes the thread ID from the LWP). So instead of storing
70 the LynxOS tid in the tid field of the ptid, we store it in
72 return ptid_build (pid
, tid
, 0);
75 /* Return the process ID of the given PTID.
77 This function has little reason to exist, it's just a wrapper around
78 ptid_get_pid. But since we have a getter function for the lynxos
79 ptid, it feels cleaner to have a getter for the pid as well. */
82 lynx_ptid_get_pid (ptid_t ptid
)
84 return ptid_get_pid (ptid
);
87 /* Return the LynxOS tid of the given PTID. */
90 lynx_ptid_get_tid (ptid_t ptid
)
92 /* See lynx_ptid_build: The LynxOS tid is stored inside the lwp field
94 return ptid_get_lwp (ptid
);
97 /* For a given PTID, return the associated PID as known by the LynxOS
101 lynx_ptrace_pid_from_ptid (ptid_t ptid
)
103 return BUILDPID (lynx_ptid_get_pid (ptid
), lynx_ptid_get_tid (ptid
));
106 /* Return a string image of the ptrace REQUEST number. */
109 ptrace_request_to_str (int request
)
111 #define CASE(X) case X: return #X
114 CASE(PTRACE_TRACEME
);
115 CASE(PTRACE_PEEKTEXT
);
116 CASE(PTRACE_PEEKDATA
);
117 CASE(PTRACE_PEEKUSER
);
118 CASE(PTRACE_POKETEXT
);
119 CASE(PTRACE_POKEDATA
);
120 CASE(PTRACE_POKEUSER
);
123 CASE(PTRACE_SINGLESTEP
);
126 CASE(PTRACE_GETREGS
);
127 CASE(PTRACE_SETREGS
);
128 CASE(PTRACE_GETFPREGS
);
129 CASE(PTRACE_SETFPREGS
);
130 CASE(PTRACE_READDATA
);
131 CASE(PTRACE_WRITEDATA
);
132 CASE(PTRACE_READTEXT
);
133 CASE(PTRACE_WRITETEXT
);
134 CASE(PTRACE_GETFPAREGS
);
135 CASE(PTRACE_SETFPAREGS
);
136 CASE(PTRACE_GETWINDOW
);
137 CASE(PTRACE_SETWINDOW
);
138 CASE(PTRACE_SYSCALL
);
139 CASE(PTRACE_DUMPCORE
);
140 CASE(PTRACE_SETWRBKPT
);
141 CASE(PTRACE_SETACBKPT
);
142 CASE(PTRACE_CLRBKPT
);
143 CASE(PTRACE_GET_UCODE
);
162 #ifdef PTRACE_PEEKUSP
163 CASE(PTRACE_PEEKUSP
);
165 #ifdef PTRACE_POKEUSP
166 CASE(PTRACE_POKEUSP
);
168 CASE(PTRACE_PEEKTHREAD
);
169 CASE(PTRACE_THREADUSER
);
171 CASE(PTRACE_FPWRITE
);
173 CASE(PTRACE_CONT_ONE
);
174 CASE(PTRACE_KILL_ONE
);
175 CASE(PTRACE_SINGLESTEP_ONE
);
176 CASE(PTRACE_GETLOADINFO
);
177 CASE(PTRACE_GETTRACESIG
);
178 #ifdef PTRACE_GETTHREADLIST
179 CASE(PTRACE_GETTHREADLIST
);
184 return "<unknown-request>";
187 /* A wrapper around ptrace that allows us to print debug traces of
188 ptrace calls if debug traces are activated. */
191 lynx_ptrace (int request
, ptid_t ptid
, int addr
, int data
, int addr2
)
194 const int pid
= lynx_ptrace_pid_from_ptid (ptid
);
198 fprintf (stderr
, "PTRACE (%s, pid=%d(pid=%d, tid=%d), addr=0x%x, "
199 "data=0x%x, addr2=0x%x)",
200 ptrace_request_to_str (request
), pid
, PIDGET (pid
), TIDGET (pid
),
202 result
= ptrace (request
, pid
, addr
, data
, addr2
);
205 fprintf (stderr
, " -> %d (=0x%x)\n", result
, result
);
211 /* Call add_process with the given parameters, and initializes
212 the process' private data. */
214 static struct process_info
*
215 lynx_add_process (int pid
, int attached
)
217 struct process_info
*proc
;
219 proc
= add_process (pid
, attached
);
220 proc
->tdesc
= lynx_tdesc
;
221 proc
->private = xcalloc (1, sizeof (*proc
->private));
222 proc
->private->last_wait_event_ptid
= null_ptid
;
227 /* Implement the create_inferior method of the target_ops vector. */
230 lynx_create_inferior (char *program
, char **allargs
)
234 lynx_debug ("lynx_create_inferior ()");
238 perror_with_name ("fork");
246 /* Switch child to its own process group so that signals won't
247 directly affect gdbserver. */
250 ioctl (0, TIOCSPGRP
, &pgrp
);
251 lynx_ptrace (PTRACE_TRACEME
, null_ptid
, 0, 0, 0);
252 execv (program
, allargs
);
253 fprintf (stderr
, "Cannot exec %s: %s.\n", program
, strerror (errno
));
258 lynx_add_process (pid
, 0);
259 /* Do not add the process thread just yet, as we do not know its tid.
260 We will add it later, during the wait for the STOP event corresponding
261 to the lynx_ptrace (PTRACE_TRACEME) call above. */
265 /* Assuming we've just attached to a running inferior whose pid is PID,
266 add all threads running in that process. */
269 lynx_add_threads_after_attach (int pid
)
271 /* Ugh! There appears to be no way to get the list of threads
272 in the program we just attached to. So get the list by calling
273 the "ps" command. This is only needed now, as we will then
274 keep the thread list up to date thanks to thread creation and
275 exit notifications. */
278 int thread_pid
, thread_tid
;
280 f
= popen ("ps atx", "r");
282 perror_with_name ("Cannot get thread list");
284 while (fgets (buf
, sizeof (buf
), f
) != NULL
)
285 if ((sscanf (buf
, "%d %d", &thread_pid
, &thread_tid
) == 2
286 && thread_pid
== pid
))
288 ptid_t thread_ptid
= lynx_ptid_build (pid
, thread_tid
);
290 if (!find_thread_ptid (thread_ptid
))
292 lynx_debug ("New thread: (pid = %d, tid = %d)",
294 add_thread (thread_ptid
, NULL
);
301 /* Implement the attach target_ops method. */
304 lynx_attach (unsigned long pid
)
306 ptid_t ptid
= lynx_ptid_build (pid
, 0);
308 if (lynx_ptrace (PTRACE_ATTACH
, ptid
, 0, 0, 0) != 0)
309 error ("Cannot attach to process %lu: %s (%d)\n", pid
,
310 strerror (errno
), errno
);
312 lynx_add_process (pid
, 1);
313 lynx_add_threads_after_attach (pid
);
318 /* Implement the resume target_ops method. */
321 lynx_resume (struct thread_resume
*resume_info
, size_t n
)
323 /* FIXME: Assume for now that n == 1. */
324 ptid_t ptid
= resume_info
[0].thread
;
325 const int request
= (resume_info
[0].kind
== resume_step
326 ? PTRACE_SINGLESTEP
: PTRACE_CONT
);
327 const int signal
= resume_info
[0].sig
;
329 /* If given a minus_one_ptid, then try using the current_process'
330 private->last_wait_event_ptid. On most LynxOS versions,
331 using any of the process' thread works well enough, but
332 LynxOS 178 is a little more sensitive, and triggers some
333 unexpected signals (Eg SIG61) when we resume the inferior
334 using a different thread. */
335 if (ptid_equal (ptid
, minus_one_ptid
))
336 ptid
= current_process()->private->last_wait_event_ptid
;
338 /* The ptid might still be minus_one_ptid; this can happen between
339 the moment we create the inferior or attach to a process, and
340 the moment we resume its execution for the first time. It is
341 fine to use the current_inferior's ptid in those cases. */
342 if (ptid_equal (ptid
, minus_one_ptid
))
343 ptid
= thread_to_gdb_id (current_inferior
);
345 regcache_invalidate ();
348 lynx_ptrace (request
, ptid
, 1, signal
, 0);
350 perror_with_name ("ptrace");
353 /* Resume the execution of the given PTID. */
356 lynx_continue (ptid_t ptid
)
358 struct thread_resume resume_info
;
360 resume_info
.thread
= ptid
;
361 resume_info
.kind
= resume_continue
;
364 lynx_resume (&resume_info
, 1);
367 /* A wrapper around waitpid that handles the various idiosyncrasies
368 of LynxOS' waitpid. */
371 lynx_waitpid (int pid
, int *stat_loc
)
377 ret
= waitpid (pid
, stat_loc
, WNOHANG
);
380 /* An ECHILD error is not indicative of a real problem.
381 It happens for instance while waiting for the inferior
382 to stop after attaching to it. */
384 perror_with_name ("waitpid (WNOHANG)");
388 /* No event with WNOHANG. See if there is one with WUNTRACED. */
389 ret
= waitpid (pid
, stat_loc
, WNOHANG
| WUNTRACED
);
392 /* An ECHILD error is not indicative of a real problem.
393 It happens for instance while waiting for the inferior
394 to stop after attaching to it. */
396 perror_with_name ("waitpid (WNOHANG|WUNTRACED)");
405 /* Implement the wait target_ops method. */
408 lynx_wait_1 (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
415 if (ptid_equal (ptid
, minus_one_ptid
))
416 pid
= lynx_ptid_get_pid (thread_to_gdb_id (current_inferior
));
418 pid
= BUILDPID (lynx_ptid_get_pid (ptid
), lynx_ptid_get_tid (ptid
));
422 ret
= lynx_waitpid (pid
, &wstat
);
423 new_ptid
= lynx_ptid_build (ret
, ((union wait
*) &wstat
)->w_tid
);
424 find_process_pid (ret
)->private->last_wait_event_ptid
= new_ptid
;
426 /* If this is a new thread, then add it now. The reason why we do
427 this here instead of when handling new-thread events is because
428 we need to add the thread associated to the "main" thread - even
429 for non-threaded applications where the new-thread events are not
431 if (!find_thread_ptid (new_ptid
))
433 lynx_debug ("New thread: (pid = %d, tid = %d)",
434 lynx_ptid_get_pid (new_ptid
), lynx_ptid_get_tid (new_ptid
));
435 add_thread (new_ptid
, NULL
);
438 if (WIFSTOPPED (wstat
))
440 status
->kind
= TARGET_WAITKIND_STOPPED
;
441 status
->value
.integer
= gdb_signal_from_host (WSTOPSIG (wstat
));
442 lynx_debug ("process stopped with signal: %d",
443 status
->value
.integer
);
445 else if (WIFEXITED (wstat
))
447 status
->kind
= TARGET_WAITKIND_EXITED
;
448 status
->value
.integer
= WEXITSTATUS (wstat
);
449 lynx_debug ("process exited with code: %d", status
->value
.integer
);
451 else if (WIFSIGNALED (wstat
))
453 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
454 status
->value
.integer
= gdb_signal_from_host (WTERMSIG (wstat
));
455 lynx_debug ("process terminated with code: %d",
456 status
->value
.integer
);
460 /* Not sure what happened if we get here, or whether we can
461 in fact get here. But if we do, handle the event the best
463 status
->kind
= TARGET_WAITKIND_STOPPED
;
464 status
->value
.integer
= gdb_signal_from_host (0);
465 lynx_debug ("unknown event ????");
468 /* SIGTRAP events are generated for situations other than single-step/
469 breakpoint events (Eg. new-thread events). Handle those other types
470 of events, and resume the execution if necessary. */
471 if (status
->kind
== TARGET_WAITKIND_STOPPED
472 && status
->value
.integer
== GDB_SIGNAL_TRAP
)
474 const int realsig
= lynx_ptrace (PTRACE_GETTRACESIG
, new_ptid
, 0, 0, 0);
476 lynx_debug ("(realsig = %d)", realsig
);
480 /* We just added the new thread above. No need to do anything
481 further. Just resume the execution again. */
482 lynx_continue (new_ptid
);
486 remove_thread (find_thread_ptid (new_ptid
));
487 lynx_continue (new_ptid
);
495 /* A wrapper around lynx_wait_1 that also prints debug traces when
496 such debug traces have been activated. */
499 lynx_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
503 lynx_debug ("lynx_wait (pid = %d, tid = %ld)",
504 lynx_ptid_get_pid (ptid
), lynx_ptid_get_tid (ptid
));
505 new_ptid
= lynx_wait_1 (ptid
, status
, options
);
506 lynx_debug (" -> (pid=%d, tid=%ld, status->kind = %d)",
507 lynx_ptid_get_pid (new_ptid
), lynx_ptid_get_tid (new_ptid
),
512 /* Implement the kill target_ops method. */
517 ptid_t ptid
= lynx_ptid_build (pid
, 0);
518 struct target_waitstatus status
;
519 struct process_info
*process
;
521 process
= find_process_pid (pid
);
525 lynx_ptrace (PTRACE_KILL
, ptid
, 0, 0, 0);
526 lynx_wait (ptid
, &status
, 0);
527 the_target
->mourn (process
);
531 /* Implement the detach target_ops method. */
534 lynx_detach (int pid
)
536 ptid_t ptid
= lynx_ptid_build (pid
, 0);
537 struct process_info
*process
;
539 process
= find_process_pid (pid
);
543 lynx_ptrace (PTRACE_DETACH
, ptid
, 0, 0, 0);
544 the_target
->mourn (process
);
548 /* Implement the mourn target_ops method. */
551 lynx_mourn (struct process_info
*proc
)
553 /* Free our private data. */
554 free (proc
->private);
555 proc
->private = NULL
;
560 /* Implement the join target_ops method. */
565 /* The PTRACE_DETACH is sufficient to detach from the process.
566 So no need to do anything extra. */
569 /* Implement the thread_alive target_ops method. */
572 lynx_thread_alive (ptid_t ptid
)
574 /* The list of threads is updated at the end of each wait, so it
575 should be up to date. No need to re-fetch it. */
576 return (find_thread_ptid (ptid
) != NULL
);
579 /* Implement the fetch_registers target_ops method. */
582 lynx_fetch_registers (struct regcache
*regcache
, int regno
)
584 struct lynx_regset_info
*regset
= lynx_target_regsets
;
585 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
587 lynx_debug ("lynx_fetch_registers (regno = %d)", regno
);
589 while (regset
->size
>= 0)
594 buf
= xmalloc (regset
->size
);
595 res
= lynx_ptrace (regset
->get_request
, inferior_ptid
, (int) buf
, 0, 0);
598 regset
->store_function (regcache
, buf
);
604 /* Implement the store_registers target_ops method. */
607 lynx_store_registers (struct regcache
*regcache
, int regno
)
609 struct lynx_regset_info
*regset
= lynx_target_regsets
;
610 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
612 lynx_debug ("lynx_store_registers (regno = %d)", regno
);
614 while (regset
->size
>= 0)
619 buf
= xmalloc (regset
->size
);
620 res
= lynx_ptrace (regset
->get_request
, inferior_ptid
, (int) buf
, 0, 0);
623 /* Then overlay our cached registers on that. */
624 regset
->fill_function (regcache
, buf
);
625 /* Only now do we write the register set. */
626 res
= lynx_ptrace (regset
->set_request
, inferior_ptid
, (int) buf
,
636 /* Implement the read_memory target_ops method. */
639 lynx_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
641 /* On LynxOS, memory reads needs to be performed in chunks the size
642 of int types, and they should also be aligned accordingly. */
644 const int xfer_size
= sizeof (buf
);
645 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) xfer_size
;
646 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
648 while (addr
< memaddr
+ len
)
655 skip
= memaddr
- addr
;
656 if (addr
+ xfer_size
> memaddr
+ len
)
657 truncate
= addr
+ xfer_size
- memaddr
- len
;
658 buf
= lynx_ptrace (PTRACE_PEEKTEXT
, inferior_ptid
, addr
, 0, 0);
661 memcpy (myaddr
+ (addr
- memaddr
) + skip
, (gdb_byte
*) &buf
+ skip
,
662 xfer_size
- skip
- truncate
);
669 /* Implement the write_memory target_ops method. */
672 lynx_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
674 /* On LynxOS, memory writes needs to be performed in chunks the size
675 of int types, and they should also be aligned accordingly. */
677 const int xfer_size
= sizeof (buf
);
678 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) xfer_size
;
679 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
681 while (addr
< memaddr
+ len
)
687 skip
= memaddr
- addr
;
688 if (addr
+ xfer_size
> memaddr
+ len
)
689 truncate
= addr
+ xfer_size
- memaddr
- len
;
690 if (skip
> 0 || truncate
> 0)
691 /* We need to read the memory at this address in order to preserve
692 the data that we are not overwriting. */
693 lynx_read_memory (addr
, (unsigned char *) &buf
, xfer_size
);
696 memcpy ((gdb_byte
*) &buf
+ skip
, myaddr
+ (addr
- memaddr
) + skip
,
697 xfer_size
- skip
- truncate
);
699 lynx_ptrace (PTRACE_POKETEXT
, inferior_ptid
, addr
, buf
, 0);
708 /* Implement the kill_request target_ops method. */
711 lynx_request_interrupt (void)
713 ptid_t inferior_ptid
= thread_to_gdb_id (current_inferior
);
715 kill (lynx_ptid_get_pid (inferior_ptid
), SIGINT
);
718 /* The LynxOS target_ops vector. */
720 static struct target_ops lynx_target_ops
= {
721 lynx_create_inferior
,
730 lynx_fetch_registers
,
731 lynx_store_registers
,
732 NULL
, /* prepare_to_access_memory */
733 NULL
, /* done_accessing_memory */
736 NULL
, /* look_up_symbols */
737 lynx_request_interrupt
,
738 NULL
, /* read_auxv */
739 NULL
, /* insert_point */
740 NULL
, /* remove_point */
741 NULL
, /* stopped_by_watchpoint */
742 NULL
, /* stopped_data_address */
743 NULL
, /* read_offsets */
744 NULL
, /* get_tls_address */
745 NULL
, /* qxfer_spu */
746 NULL
, /* hostio_last_error */
747 NULL
, /* qxfer_osdata */
748 NULL
, /* qxfer_siginfo */
749 NULL
, /* supports_non_stop */
751 NULL
, /* start_non_stop */
752 NULL
, /* supports_multi_process */
753 NULL
, /* handle_monitor_command */
757 initialize_low (void)
759 set_target_ops (&lynx_target_ops
);
760 the_low_target
.arch_setup ();