gdbserver/lynx178: spurious SIG61 signal when resuming inferior.
[deliverable/binutils-gdb.git] / gdb / gdbserver / lynx-low.c
1 /* Copyright (C) 2009-2013 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
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.
9
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.
14
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/>. */
17
18 #include "server.h"
19 #include "target.h"
20 #include "lynx-low.h"
21
22 #include <limits.h>
23 #include <sys/ptrace.h>
24 #include <sys/piddef.h> /* Provides PIDGET, TIDGET, BUILDPID, etc. */
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include "gdb_wait.h"
29 #include <signal.h>
30
31 int using_threads = 1;
32
33 /* Per-process private data. */
34
35 struct process_info_private
36 {
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;
40 };
41
42 /* Print a debug trace on standard output if debug_threads is set. */
43
44 static void
45 lynx_debug (char *string, ...)
46 {
47 va_list args;
48
49 if (!debug_threads)
50 return;
51
52 va_start (args, string);
53 fprintf (stderr, "DEBUG(lynx): ");
54 vfprintf (stderr, string, args);
55 fprintf (stderr, "\n");
56 va_end (args);
57 }
58
59 /* Build a ptid_t given a PID and a LynxOS TID. */
60
61 static ptid_t
62 lynx_ptid_build (int pid, long tid)
63 {
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
68 the lwp field. */
69 return ptid_build (pid, tid, 0);
70 }
71
72 /* Return the process ID of the given PTID.
73
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. */
77
78 static int
79 lynx_ptid_get_pid (ptid_t ptid)
80 {
81 return ptid_get_pid (ptid);
82 }
83
84 /* Return the LynxOS tid of the given PTID. */
85
86 static long
87 lynx_ptid_get_tid (ptid_t ptid)
88 {
89 /* See lynx_ptid_build: The LynxOS tid is stored inside the lwp field
90 of the ptid. */
91 return ptid_get_lwp (ptid);
92 }
93
94 /* For a given PTID, return the associated PID as known by the LynxOS
95 ptrace layer. */
96
97 static int
98 lynx_ptrace_pid_from_ptid (ptid_t ptid)
99 {
100 return BUILDPID (lynx_ptid_get_pid (ptid), lynx_ptid_get_tid (ptid));
101 }
102
103 /* Return a string image of the ptrace REQUEST number. */
104
105 static char *
106 ptrace_request_to_str (int request)
107 {
108 #define CASE(X) case X: return #X
109 switch (request)
110 {
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);
118 CASE(PTRACE_CONT);
119 CASE(PTRACE_KILL);
120 CASE(PTRACE_SINGLESTEP);
121 CASE(PTRACE_ATTACH);
122 CASE(PTRACE_DETACH);
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);
141 #ifdef PT_READ_GPR
142 CASE(PT_READ_GPR);
143 #endif
144 #ifdef PT_WRITE_GPR
145 CASE(PT_WRITE_GPR);
146 #endif
147 #ifdef PT_READ_FPR
148 CASE(PT_READ_FPR);
149 #endif
150 #ifdef PT_WRITE_FPR
151 CASE(PT_WRITE_FPR);
152 #endif
153 #ifdef PT_READ_VPR
154 CASE(PT_READ_VPR);
155 #endif
156 #ifdef PT_WRITE_VPR
157 CASE(PT_WRITE_VPR);
158 #endif
159 #ifdef PTRACE_PEEKUSP
160 CASE(PTRACE_PEEKUSP);
161 #endif
162 #ifdef PTRACE_POKEUSP
163 CASE(PTRACE_POKEUSP);
164 #endif
165 CASE(PTRACE_PEEKTHREAD);
166 CASE(PTRACE_THREADUSER);
167 CASE(PTRACE_FPREAD);
168 CASE(PTRACE_FPWRITE);
169 CASE(PTRACE_SETSIG);
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);
177 #endif
178 }
179 #undef CASE
180
181 return "<unknown-request>";
182 }
183
184 /* A wrapper around ptrace that allows us to print debug traces of
185 ptrace calls if debug traces are activated. */
186
187 static int
188 lynx_ptrace (int request, ptid_t ptid, int addr, int data, int addr2)
189 {
190 int result;
191 const int pid = lynx_ptrace_pid_from_ptid (ptid);
192 int saved_errno;
193
194 if (debug_threads)
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),
198 addr, data, addr2);
199 result = ptrace (request, pid, addr, data, addr2);
200 saved_errno = errno;
201 if (debug_threads)
202 fprintf (stderr, " -> %d (=0x%x)\n", result, result);
203
204 errno = saved_errno;
205 return result;
206 }
207
208 /* Call add_process with the given parameters, and initializes
209 the process' private data. */
210
211 static struct process_info *
212 lynx_add_process (int pid, int attached)
213 {
214 struct process_info *proc;
215
216 proc = add_process (pid, attached);
217 proc->private = xcalloc (1, sizeof (*proc->private));
218 proc->private->last_wait_event_ptid = null_ptid;
219
220 return proc;
221 }
222
223 /* Implement the create_inferior method of the target_ops vector. */
224
225 static int
226 lynx_create_inferior (char *program, char **allargs)
227 {
228 int pid;
229
230 lynx_debug ("lynx_create_inferior ()");
231
232 pid = fork ();
233 if (pid < 0)
234 perror_with_name ("fork");
235
236 if (pid == 0)
237 {
238 int pgrp;
239
240 /* Switch child to its own process group so that signals won't
241 directly affect gdbserver. */
242 pgrp = getpid();
243 setpgid (0, pgrp);
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));
248 fflush (stderr);
249 _exit (0177);
250 }
251
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. */
256 return pid;
257 }
258
259 /* Implement the attach target_ops method. */
260
261 static int
262 lynx_attach (unsigned long pid)
263 {
264 ptid_t ptid = lynx_ptid_build (pid, 0);
265
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);
269
270 lynx_add_process (pid, 1);
271 add_thread (ptid, NULL);
272
273 return 0;
274 }
275
276 /* Implement the resume target_ops method. */
277
278 static void
279 lynx_resume (struct thread_resume *resume_info, size_t n)
280 {
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;
286
287 /* If given a null_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;
295
296 /* The ptid might still be NULL; this can happen between the moment
297 we create the inferior or attach to a process, and the moment
298 we resume its execution for the first time. It is fine to
299 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);
302
303 regcache_invalidate ();
304
305 errno = 0;
306 lynx_ptrace (request, ptid, 1, signal, 0);
307 if (errno)
308 perror_with_name ("ptrace");
309 }
310
311 /* Resume the execution of the given PTID. */
312
313 static void
314 lynx_continue (ptid_t ptid)
315 {
316 struct thread_resume resume_info;
317
318 resume_info.thread = ptid;
319 resume_info.kind = resume_continue;
320 resume_info.sig = 0;
321
322 lynx_resume (&resume_info, 1);
323 }
324
325 /* A wrapper around waitpid that handles the various idiosyncrasies
326 of LynxOS' waitpid. */
327
328 static int
329 lynx_waitpid (int pid, int *stat_loc)
330 {
331 int ret = 0;
332
333 while (1)
334 {
335 ret = waitpid (pid, stat_loc, WNOHANG);
336 if (ret < 0)
337 {
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. */
341 if (errno != ECHILD)
342 perror_with_name ("waitpid (WNOHANG)");
343 }
344 if (ret > 0)
345 break;
346 /* No event with WNOHANG. See if there is one with WUNTRACED. */
347 ret = waitpid (pid, stat_loc, WNOHANG | WUNTRACED);
348 if (ret < 0)
349 {
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. */
353 if (errno != ECHILD)
354 perror_with_name ("waitpid (WNOHANG|WUNTRACED)");
355 }
356 if (ret > 0)
357 break;
358 usleep (1000);
359 }
360 return ret;
361 }
362
363 /* Implement the wait target_ops method. */
364
365 static ptid_t
366 lynx_wait_1 (ptid_t ptid, struct target_waitstatus *status, int options)
367 {
368 int pid;
369 int ret;
370 int wstat;
371 ptid_t new_ptid;
372
373 if (ptid_equal (ptid, minus_one_ptid))
374 pid = lynx_ptid_get_pid (thread_to_gdb_id (current_inferior));
375 else
376 pid = BUILDPID (lynx_ptid_get_pid (ptid), lynx_ptid_get_tid (ptid));
377
378 retry:
379
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;
383
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
388 generated. */
389 if (!find_thread_ptid (new_ptid))
390 {
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);
394 }
395
396 if (WIFSTOPPED (wstat))
397 {
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);
402 }
403 else if (WIFEXITED (wstat))
404 {
405 status->kind = TARGET_WAITKIND_EXITED;
406 status->value.integer = WEXITSTATUS (wstat);
407 lynx_debug ("process exited with code: %d", status->value.integer);
408 }
409 else if (WIFSIGNALED (wstat))
410 {
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);
415 }
416 else
417 {
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
420 we can. */
421 status->kind = TARGET_WAITKIND_STOPPED;
422 status->value.integer = gdb_signal_from_host (0);
423 lynx_debug ("unknown event ????");
424 }
425
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)
431 {
432 const int realsig = lynx_ptrace (PTRACE_GETTRACESIG, new_ptid, 0, 0, 0);
433
434 lynx_debug ("(realsig = %d)", realsig);
435 switch (realsig)
436 {
437 case SIGNEWTHREAD:
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);
441 goto retry;
442
443 case SIGTHREADEXIT:
444 remove_thread (find_thread_ptid (new_ptid));
445 lynx_continue (new_ptid);
446 goto retry;
447 }
448 }
449
450 return new_ptid;
451 }
452
453 /* A wrapper around lynx_wait_1 that also prints debug traces when
454 such debug traces have been activated. */
455
456 static ptid_t
457 lynx_wait (ptid_t ptid, struct target_waitstatus *status, int options)
458 {
459 ptid_t new_ptid;
460
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),
466 status->kind);
467 return new_ptid;
468 }
469
470 /* Implement the kill target_ops method. */
471
472 static int
473 lynx_kill (int pid)
474 {
475 ptid_t ptid = lynx_ptid_build (pid, 0);
476 struct target_waitstatus status;
477 struct process_info *process;
478
479 process = find_process_pid (pid);
480 if (process == NULL)
481 return -1;
482
483 lynx_ptrace (PTRACE_KILL, ptid, 0, 0, 0);
484 lynx_wait (ptid, &status, 0);
485 the_target->mourn (process);
486 return 0;
487 }
488
489 /* Implement the detach target_ops method. */
490
491 static int
492 lynx_detach (int pid)
493 {
494 ptid_t ptid = lynx_ptid_build (pid, 0);
495 struct process_info *process;
496
497 process = find_process_pid (pid);
498 if (process == NULL)
499 return -1;
500
501 lynx_ptrace (PTRACE_DETACH, ptid, 0, 0, 0);
502 the_target->mourn (process);
503 return 0;
504 }
505
506 /* Implement the mourn target_ops method. */
507
508 static void
509 lynx_mourn (struct process_info *proc)
510 {
511 /* Free our private data. */
512 free (proc->private);
513 proc->private = NULL;
514
515 clear_inferiors ();
516 }
517
518 /* Implement the join target_ops method. */
519
520 static void
521 lynx_join (int pid)
522 {
523 /* The PTRACE_DETACH is sufficient to detach from the process.
524 So no need to do anything extra. */
525 }
526
527 /* Implement the thread_alive target_ops method. */
528
529 static int
530 lynx_thread_alive (ptid_t ptid)
531 {
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);
535 }
536
537 /* Implement the fetch_registers target_ops method. */
538
539 static void
540 lynx_fetch_registers (struct regcache *regcache, int regno)
541 {
542 struct lynx_regset_info *regset = lynx_target_regsets;
543 ptid_t inferior_ptid = thread_to_gdb_id (current_inferior);
544
545 lynx_debug ("lynx_fetch_registers (regno = %d)", regno);
546
547 while (regset->size >= 0)
548 {
549 char *buf;
550 int res;
551
552 buf = xmalloc (regset->size);
553 res = lynx_ptrace (regset->get_request, inferior_ptid, (int) buf, 0, 0);
554 if (res < 0)
555 perror ("ptrace");
556 regset->store_function (regcache, buf);
557 free (buf);
558 regset++;
559 }
560 }
561
562 /* Implement the store_registers target_ops method. */
563
564 static void
565 lynx_store_registers (struct regcache *regcache, int regno)
566 {
567 struct lynx_regset_info *regset = lynx_target_regsets;
568 ptid_t inferior_ptid = thread_to_gdb_id (current_inferior);
569
570 lynx_debug ("lynx_store_registers (regno = %d)", regno);
571
572 while (regset->size >= 0)
573 {
574 char *buf;
575 int res;
576
577 buf = xmalloc (regset->size);
578 res = lynx_ptrace (regset->get_request, inferior_ptid, (int) buf, 0, 0);
579 if (res == 0)
580 {
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,
585 0, 0);
586 }
587 if (res < 0)
588 perror ("ptrace");
589 free (buf);
590 regset++;
591 }
592 }
593
594 /* Implement the read_memory target_ops method. */
595
596 static int
597 lynx_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
598 {
599 /* On LynxOS, memory reads needs to be performed in chunks the size
600 of int types, and they should also be aligned accordingly. */
601 int buf;
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);
605
606 while (addr < memaddr + len)
607 {
608 int skip = 0;
609 int truncate = 0;
610
611 errno = 0;
612 if (addr < memaddr)
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);
617 if (errno)
618 return errno;
619 memcpy (myaddr + (addr - memaddr) + skip, (gdb_byte *) &buf + skip,
620 xfer_size - skip - truncate);
621 addr += xfer_size;
622 }
623
624 return 0;
625 }
626
627 /* Implement the write_memory target_ops method. */
628
629 static int
630 lynx_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
631 {
632 /* On LynxOS, memory writes needs to be performed in chunks the size
633 of int types, and they should also be aligned accordingly. */
634 int buf;
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);
638
639 while (addr < memaddr + len)
640 {
641 int skip = 0;
642 int truncate = 0;
643
644 if (addr < memaddr)
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);
652 if (errno)
653 return errno;
654 memcpy ((gdb_byte *) &buf + skip, myaddr + (addr - memaddr) + skip,
655 xfer_size - skip - truncate);
656 errno = 0;
657 lynx_ptrace (PTRACE_POKETEXT, inferior_ptid, addr, buf, 0);
658 if (errno)
659 return errno;
660 addr += xfer_size;
661 }
662
663 return 0;
664 }
665
666 /* Implement the kill_request target_ops method. */
667
668 static void
669 lynx_request_interrupt (void)
670 {
671 ptid_t inferior_ptid = thread_to_gdb_id (current_inferior);
672
673 kill (lynx_ptid_get_pid (inferior_ptid), SIGINT);
674 }
675
676 /* The LynxOS target_ops vector. */
677
678 static struct target_ops lynx_target_ops = {
679 lynx_create_inferior,
680 lynx_attach,
681 lynx_kill,
682 lynx_detach,
683 lynx_mourn,
684 lynx_join,
685 lynx_thread_alive,
686 lynx_resume,
687 lynx_wait,
688 lynx_fetch_registers,
689 lynx_store_registers,
690 NULL, /* prepare_to_access_memory */
691 NULL, /* done_accessing_memory */
692 lynx_read_memory,
693 lynx_write_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 */
708 NULL, /* async */
709 NULL, /* start_non_stop */
710 NULL, /* supports_multi_process */
711 NULL, /* handle_monitor_command */
712 };
713
714 void
715 initialize_low (void)
716 {
717 set_target_ops (&lynx_target_ops);
718 the_low_target.arch_setup ();
719 }
720
This page took 0.061301 seconds and 4 git commands to generate.