Commit | Line | Data |
---|---|---|
ac8c974e AR |
1 | /* QNX Neutrino specific low level interface, for the remote server |
2 | for GDB. | |
61baf725 | 3 | Copyright (C) 2009-2017 Free Software Foundation, Inc. |
ac8c974e AR |
4 | |
5 | This file is part of GDB. | |
6 | ||
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 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | ||
21 | #include "server.h" | |
623b6bdf | 22 | #include "gdbthread.h" |
ac8c974e | 23 | #include "nto-low.h" |
533b0600 | 24 | #include "hostio.h" |
ac8c974e AR |
25 | |
26 | #include <limits.h> | |
27 | #include <fcntl.h> | |
28 | #include <spawn.h> | |
29 | #include <sys/procfs.h> | |
30 | #include <sys/auxv.h> | |
ac8c974e AR |
31 | #include <sys/iomgr.h> |
32 | #include <sys/neutrino.h> | |
33 | ||
34 | ||
35 | extern int using_threads; | |
36 | int using_threads = 1; | |
37 | ||
3aee8918 PA |
38 | const struct target_desc *nto_tdesc; |
39 | ||
ac8c974e AR |
40 | static void |
41 | nto_trace (const char *fmt, ...) | |
42 | { | |
43 | va_list arg_list; | |
44 | ||
45 | if (debug_threads == 0) | |
46 | return; | |
47 | fprintf (stderr, "nto:"); | |
48 | va_start (arg_list, fmt); | |
49 | vfprintf (stderr, fmt, arg_list); | |
50 | va_end (arg_list); | |
51 | } | |
52 | ||
53 | #define TRACE nto_trace | |
54 | ||
55 | /* Structure holding neutrino specific information about | |
56 | inferior. */ | |
57 | ||
58 | struct nto_inferior | |
59 | { | |
60 | char nto_procfs_path[PATH_MAX]; | |
61 | int ctl_fd; | |
62 | pid_t pid; | |
63 | int exit_signo; /* For tracking exit status. */ | |
64 | }; | |
65 | ||
66 | static struct nto_inferior nto_inferior; | |
67 | ||
68 | static void | |
69 | init_nto_inferior (struct nto_inferior *nto_inferior) | |
70 | { | |
71 | memset (nto_inferior, 0, sizeof (struct nto_inferior)); | |
72 | nto_inferior->ctl_fd = -1; | |
73 | nto_inferior->pid = -1; | |
74 | } | |
75 | ||
76 | static void | |
77 | do_detach (void) | |
78 | { | |
79 | if (nto_inferior.ctl_fd != -1) | |
80 | { | |
81 | nto_trace ("Closing fd\n"); | |
82 | close (nto_inferior.ctl_fd); | |
83 | init_nto_inferior (&nto_inferior); | |
84 | } | |
85 | } | |
86 | ||
87 | /* Set current thread. Return 1 on success, 0 otherwise. */ | |
88 | ||
89 | static int | |
90 | nto_set_thread (ptid_t ptid) | |
91 | { | |
92 | int res = 0; | |
93 | ||
94 | TRACE ("%s pid: %d tid: %ld\n", __func__, ptid_get_pid (ptid), | |
95 | ptid_get_lwp (ptid)); | |
96 | if (nto_inferior.ctl_fd != -1 | |
97 | && !ptid_equal (ptid, null_ptid) | |
98 | && !ptid_equal (ptid, minus_one_ptid)) | |
99 | { | |
100 | pthread_t tid = ptid_get_lwp (ptid); | |
101 | ||
102 | if (EOK == devctl (nto_inferior.ctl_fd, DCMD_PROC_CURTHREAD, &tid, | |
103 | sizeof (tid), 0)) | |
104 | res = 1; | |
105 | else | |
106 | TRACE ("%s: Error: failed to set current thread\n", __func__); | |
107 | } | |
108 | return res; | |
109 | } | |
110 | ||
111 | /* This function will determine all alive threads. Note that we do not list | |
112 | dead but unjoined threads even though they are still in the process' thread | |
113 | list. | |
114 | ||
115 | NTO_INFERIOR must not be NULL. */ | |
116 | ||
117 | static void | |
118 | nto_find_new_threads (struct nto_inferior *nto_inferior) | |
119 | { | |
120 | pthread_t tid; | |
121 | ||
122 | TRACE ("%s pid:%d\n", __func__, nto_inferior->pid); | |
123 | ||
124 | if (nto_inferior->ctl_fd == -1) | |
125 | return; | |
126 | ||
127 | for (tid = 1;; ++tid) | |
128 | { | |
129 | procfs_status status; | |
130 | ptid_t ptid; | |
131 | int err; | |
132 | ||
133 | status.tid = tid; | |
134 | err = devctl (nto_inferior->ctl_fd, DCMD_PROC_TIDSTATUS, &status, | |
135 | sizeof (status), 0); | |
136 | ||
137 | if (err != EOK || status.tid == 0) | |
138 | break; | |
139 | ||
140 | /* All threads in between are gone. */ | |
141 | while (tid != status.tid || status.state == STATE_DEAD) | |
142 | { | |
143 | struct thread_info *ti; | |
144 | ||
145 | ptid = ptid_build (nto_inferior->pid, tid, 0); | |
146 | ti = find_thread_ptid (ptid); | |
147 | if (ti != NULL) | |
148 | { | |
149 | TRACE ("Removing thread %d\n", tid); | |
150 | remove_thread (ti); | |
151 | } | |
152 | if (tid == status.tid) | |
153 | break; | |
154 | ++tid; | |
155 | } | |
156 | ||
157 | if (status.state != STATE_DEAD) | |
158 | { | |
159 | TRACE ("Adding thread %d\n", tid); | |
160 | ptid = ptid_build (nto_inferior->pid, tid, 0); | |
161 | if (!find_thread_ptid (ptid)) | |
162 | add_thread (ptid, NULL); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | /* Given pid, open procfs path. */ | |
168 | ||
169 | static pid_t | |
170 | do_attach (pid_t pid) | |
171 | { | |
172 | procfs_status status; | |
173 | struct sigevent event; | |
174 | ||
175 | if (nto_inferior.ctl_fd != -1) | |
176 | { | |
177 | close (nto_inferior.ctl_fd); | |
178 | init_nto_inferior (&nto_inferior); | |
179 | } | |
6cebaf6e | 180 | xsnprintf (nto_inferior.nto_procfs_path, PATH_MAX - 1, "/proc/%d/as", pid); |
ac8c974e AR |
181 | nto_inferior.ctl_fd = open (nto_inferior.nto_procfs_path, O_RDWR); |
182 | if (nto_inferior.ctl_fd == -1) | |
183 | { | |
184 | TRACE ("Failed to open %s\n", nto_inferior.nto_procfs_path); | |
185 | init_nto_inferior (&nto_inferior); | |
186 | return -1; | |
187 | } | |
188 | if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) | |
189 | != EOK) | |
190 | { | |
191 | do_detach (); | |
192 | return -1; | |
193 | } | |
194 | nto_inferior.pid = pid; | |
195 | /* Define a sigevent for process stopped notification. */ | |
196 | event.sigev_notify = SIGEV_SIGNAL_THREAD; | |
197 | event.sigev_signo = SIGUSR1; | |
198 | event.sigev_code = 0; | |
199 | event.sigev_value.sival_ptr = NULL; | |
200 | event.sigev_priority = -1; | |
201 | devctl (nto_inferior.ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0); | |
202 | ||
203 | if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), | |
204 | 0) == EOK | |
205 | && (status.flags & _DEBUG_FLAG_STOPPED)) | |
206 | { | |
207 | ptid_t ptid; | |
3aee8918 | 208 | struct process_info *proc; |
ac8c974e AR |
209 | |
210 | kill (pid, SIGCONT); | |
211 | ptid = ptid_build (status.pid, status.tid, 0); | |
212 | the_low_target.arch_setup (); | |
3aee8918 PA |
213 | proc = add_process (status.pid, 1); |
214 | proc->tdesc = nto_tdesc; | |
ac8c974e AR |
215 | TRACE ("Adding thread: pid=%d tid=%ld\n", status.pid, |
216 | ptid_get_lwp (ptid)); | |
217 | nto_find_new_threads (&nto_inferior); | |
218 | } | |
219 | else | |
220 | { | |
221 | do_detach (); | |
222 | return -1; | |
223 | } | |
224 | ||
225 | return pid; | |
226 | } | |
227 | ||
228 | /* Read or write LEN bytes from/to inferior's MEMADDR memory address | |
229 | into gdbservers's MYADDR buffer. Return number of bytes actually | |
230 | transfered. */ | |
231 | ||
232 | static int | |
233 | nto_xfer_memory (off_t memaddr, unsigned char *myaddr, int len, | |
234 | int dowrite) | |
235 | { | |
236 | int nbytes = 0; | |
237 | ||
238 | if (lseek (nto_inferior.ctl_fd, memaddr, SEEK_SET) == memaddr) | |
239 | { | |
240 | if (dowrite) | |
241 | nbytes = write (nto_inferior.ctl_fd, myaddr, len); | |
242 | else | |
243 | nbytes = read (nto_inferior.ctl_fd, myaddr, len); | |
244 | if (nbytes < 0) | |
245 | nbytes = 0; | |
246 | } | |
247 | if (nbytes == 0) | |
248 | { | |
249 | int e = errno; | |
250 | TRACE ("Error in %s : errno=%d (%s)\n", __func__, e, strerror (e)); | |
251 | } | |
252 | return nbytes; | |
253 | } | |
254 | ||
255 | /* Insert or remove breakpoint or watchpoint at address ADDR. | |
256 | TYPE can be one of Neutrino breakpoint types. SIZE must be 0 for | |
257 | inserting the point, -1 for removing it. | |
258 | ||
259 | Return 0 on success, 1 otherwise. */ | |
260 | ||
261 | static int | |
262 | nto_breakpoint (CORE_ADDR addr, int type, int size) | |
263 | { | |
264 | procfs_break brk; | |
265 | ||
266 | brk.type = type; | |
267 | brk.addr = addr; | |
268 | brk.size = size; | |
269 | if (devctl (nto_inferior.ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0) | |
270 | != EOK) | |
271 | return 1; | |
272 | return 0; | |
273 | } | |
274 | ||
275 | /* Read auxiliary vector from inferior's initial stack into gdbserver's | |
276 | MYADDR buffer, up to LEN bytes. | |
277 | ||
278 | Return number of bytes read. */ | |
279 | ||
280 | static int | |
281 | nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, | |
282 | unsigned char *myaddr, | |
283 | unsigned int len) | |
284 | { | |
285 | int data_ofs = 0; | |
286 | int anint; | |
287 | unsigned int len_read = 0; | |
288 | ||
289 | /* Skip over argc, argv and envp... Comment from ldd.c: | |
290 | ||
291 | The startup frame is set-up so that we have: | |
292 | auxv | |
293 | NULL | |
294 | ... | |
295 | envp2 | |
296 | envp1 <----- void *frame + (argc + 2) * sizeof(char *) | |
297 | NULL | |
298 | ... | |
299 | argv2 | |
300 | argv1 | |
301 | argc <------ void * frame | |
302 | ||
303 | On entry to ldd, frame gives the address of argc on the stack. */ | |
304 | if (nto_xfer_memory (initial_stack, (unsigned char *)&anint, | |
305 | sizeof (anint), 0) != sizeof (anint)) | |
306 | return 0; | |
307 | ||
308 | /* Size of pointer is assumed to be 4 bytes (32 bit arch. ) */ | |
309 | data_ofs += (anint + 2) * sizeof (void *); /* + 2 comes from argc itself and | |
310 | NULL terminating pointer in | |
311 | argv. */ | |
312 | ||
313 | /* Now loop over env table: */ | |
314 | while (nto_xfer_memory (initial_stack + data_ofs, | |
315 | (unsigned char *)&anint, sizeof (anint), 0) | |
316 | == sizeof (anint)) | |
317 | { | |
318 | data_ofs += sizeof (anint); | |
319 | if (anint == 0) | |
320 | break; | |
321 | } | |
322 | initial_stack += data_ofs; | |
323 | ||
324 | memset (myaddr, 0, len); | |
325 | while (len_read <= len - sizeof (auxv_t)) | |
326 | { | |
327 | auxv_t *auxv = (auxv_t *)myaddr; | |
328 | ||
329 | /* Search backwards until we have read AT_PHDR (num. 3), | |
330 | AT_PHENT (num 4), AT_PHNUM (num 5) */ | |
331 | if (nto_xfer_memory (initial_stack, (unsigned char *)auxv, | |
332 | sizeof (auxv_t), 0) == sizeof (auxv_t)) | |
333 | { | |
334 | if (auxv->a_type != AT_NULL) | |
335 | { | |
336 | auxv++; | |
337 | len_read += sizeof (auxv_t); | |
338 | } | |
339 | if (auxv->a_type == AT_PHNUM) /* That's all we need. */ | |
340 | break; | |
341 | initial_stack += sizeof (auxv_t); | |
342 | } | |
343 | else | |
344 | break; | |
345 | } | |
346 | TRACE ("auxv: len_read: %d\n", len_read); | |
347 | return len_read; | |
348 | } | |
349 | ||
2090129c SDJ |
350 | /* Start inferior specified by PROGRAM, using PROGRAM_ARGS as its |
351 | arguments. */ | |
ac8c974e AR |
352 | |
353 | static int | |
2090129c SDJ |
354 | nto_create_inferior (const char *program, |
355 | const std::vector<char *> &program_args) | |
ac8c974e AR |
356 | { |
357 | struct inheritance inherit; | |
358 | pid_t pid; | |
359 | sigset_t set; | |
2090129c | 360 | std::string str_program_args = stringify_argv (program_args); |
ac8c974e AR |
361 | |
362 | TRACE ("%s %s\n", __func__, program); | |
363 | /* Clear any pending SIGUSR1's but keep the behavior the same. */ | |
364 | signal (SIGUSR1, signal (SIGUSR1, SIG_IGN)); | |
365 | ||
366 | sigemptyset (&set); | |
367 | sigaddset (&set, SIGUSR1); | |
368 | sigprocmask (SIG_UNBLOCK, &set, NULL); | |
369 | ||
370 | memset (&inherit, 0, sizeof (inherit)); | |
371 | inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD; | |
372 | inherit.pgroup = SPAWN_NEWPGROUP; | |
2090129c SDJ |
373 | pid = spawnp (program, 0, NULL, &inherit, |
374 | (char *) str_program_args.c_str (), 0); | |
ac8c974e AR |
375 | sigprocmask (SIG_BLOCK, &set, NULL); |
376 | ||
377 | if (pid == -1) | |
378 | return -1; | |
379 | ||
380 | if (do_attach (pid) != pid) | |
381 | return -1; | |
382 | ||
383 | return pid; | |
384 | } | |
385 | ||
386 | /* Attach to process PID. */ | |
387 | ||
388 | static int | |
389 | nto_attach (unsigned long pid) | |
390 | { | |
391 | TRACE ("%s %ld\n", __func__, pid); | |
392 | if (do_attach (pid) != pid) | |
393 | error ("Unable to attach to %ld\n", pid); | |
394 | return 0; | |
395 | } | |
396 | ||
397 | /* Send signal to process PID. */ | |
398 | ||
399 | static int | |
400 | nto_kill (int pid) | |
401 | { | |
402 | TRACE ("%s %d\n", __func__, pid); | |
403 | kill (pid, SIGKILL); | |
404 | do_detach (); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | /* Detach from process PID. */ | |
409 | ||
410 | static int | |
411 | nto_detach (int pid) | |
412 | { | |
413 | TRACE ("%s %d\n", __func__, pid); | |
414 | do_detach (); | |
415 | return 0; | |
416 | } | |
417 | ||
505106cd PA |
418 | static void |
419 | nto_mourn (struct process_info *process) | |
420 | { | |
421 | remove_process (process); | |
422 | } | |
423 | ||
ac8c974e AR |
424 | /* Check if the given thread is alive. |
425 | ||
426 | Return 1 if alive, 0 otherwise. */ | |
427 | ||
428 | static int | |
429 | nto_thread_alive (ptid_t ptid) | |
430 | { | |
431 | int res; | |
432 | ||
433 | TRACE ("%s pid:%d tid:%d\n", __func__, ptid_get_pid (ptid), | |
434 | ptid_get_lwp (ptid)); | |
435 | if (SignalKill (0, ptid_get_pid (ptid), ptid_get_lwp (ptid), | |
436 | 0, 0, 0) == -1) | |
437 | res = 0; | |
438 | else | |
439 | res = 1; | |
440 | TRACE ("%s: %s\n", __func__, res ? "yes" : "no"); | |
441 | return res; | |
442 | } | |
443 | ||
444 | /* Resume inferior's execution. */ | |
445 | ||
446 | static void | |
447 | nto_resume (struct thread_resume *resume_info, size_t n) | |
448 | { | |
449 | /* We can only work in all-stop mode. */ | |
450 | procfs_status status; | |
451 | procfs_run run; | |
452 | int err; | |
453 | ||
454 | TRACE ("%s\n", __func__); | |
455 | /* Workaround for aliasing rules violation. */ | |
456 | sigset_t *run_fault = (sigset_t *) (void *) &run.fault; | |
457 | ||
458 | nto_set_thread (resume_info->thread); | |
459 | ||
460 | run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE; | |
461 | if (resume_info->kind == resume_step) | |
462 | run.flags |= _DEBUG_RUN_STEP; | |
463 | run.flags |= _DEBUG_RUN_ARM; | |
464 | ||
465 | sigemptyset (run_fault); | |
466 | sigaddset (run_fault, FLTBPT); | |
467 | sigaddset (run_fault, FLTTRACE); | |
468 | sigaddset (run_fault, FLTILL); | |
469 | sigaddset (run_fault, FLTPRIV); | |
470 | sigaddset (run_fault, FLTBOUNDS); | |
471 | sigaddset (run_fault, FLTIOVF); | |
472 | sigaddset (run_fault, FLTIZDIV); | |
473 | sigaddset (run_fault, FLTFPE); | |
474 | sigaddset (run_fault, FLTPAGE); | |
475 | sigaddset (run_fault, FLTSTACK); | |
476 | sigaddset (run_fault, FLTACCESS); | |
477 | ||
478 | sigemptyset (&run.trace); | |
479 | if (resume_info->sig) | |
480 | { | |
481 | int signal_to_pass; | |
482 | ||
483 | devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), | |
484 | 0); | |
485 | signal_to_pass = resume_info->sig; | |
486 | if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED)) | |
487 | { | |
488 | if (signal_to_pass != status.info.si_signo) | |
489 | { | |
490 | kill (status.pid, signal_to_pass); | |
491 | run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG; | |
492 | } | |
493 | else /* Let it kill the program without telling us. */ | |
494 | sigdelset (&run.trace, signal_to_pass); | |
495 | } | |
496 | } | |
497 | else | |
498 | run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT; | |
499 | ||
500 | sigfillset (&run.trace); | |
501 | ||
502 | regcache_invalidate (); | |
503 | ||
504 | err = devctl (nto_inferior.ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0); | |
505 | if (err != EOK) | |
506 | TRACE ("Error: %d \"%s\"\n", err, strerror (err)); | |
507 | } | |
508 | ||
509 | /* Wait for inferior's event. | |
510 | ||
511 | Return ptid of thread that caused the event. */ | |
512 | ||
513 | static ptid_t | |
514 | nto_wait (ptid_t ptid, | |
515 | struct target_waitstatus *ourstatus, int target_options) | |
516 | { | |
517 | sigset_t set; | |
518 | siginfo_t info; | |
519 | procfs_status status; | |
520 | const int trace_mask = (_DEBUG_FLAG_TRACE_EXEC | _DEBUG_FLAG_TRACE_RD | |
521 | | _DEBUG_FLAG_TRACE_WR | _DEBUG_FLAG_TRACE_MODIFY); | |
522 | ||
523 | TRACE ("%s\n", __func__); | |
524 | ||
525 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
526 | ||
527 | sigemptyset (&set); | |
528 | sigaddset (&set, SIGUSR1); | |
529 | ||
530 | devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0); | |
531 | while (!(status.flags & _DEBUG_FLAG_ISTOP)) | |
532 | { | |
533 | sigwaitinfo (&set, &info); | |
534 | devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), | |
535 | 0); | |
536 | } | |
537 | nto_find_new_threads (&nto_inferior); | |
538 | ||
539 | if (status.flags & _DEBUG_FLAG_SSTEP) | |
540 | { | |
541 | TRACE ("SSTEP\n"); | |
542 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
a493e3e2 | 543 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
ac8c974e AR |
544 | } |
545 | /* Was it a breakpoint? */ | |
546 | else if (status.flags & trace_mask) | |
547 | { | |
548 | TRACE ("STOPPED\n"); | |
549 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
a493e3e2 | 550 | ourstatus->value.sig = GDB_SIGNAL_TRAP; |
ac8c974e AR |
551 | } |
552 | else if (status.flags & _DEBUG_FLAG_ISTOP) | |
553 | { | |
554 | TRACE ("ISTOP\n"); | |
555 | switch (status.why) | |
556 | { | |
557 | case _DEBUG_WHY_SIGNALLED: | |
558 | TRACE (" SIGNALLED\n"); | |
559 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
560 | ourstatus->value.sig = | |
2ea28649 | 561 | gdb_signal_from_host (status.info.si_signo); |
ac8c974e AR |
562 | nto_inferior.exit_signo = ourstatus->value.sig; |
563 | break; | |
564 | case _DEBUG_WHY_FAULTED: | |
565 | TRACE (" FAULTED\n"); | |
566 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
567 | if (status.info.si_signo == SIGTRAP) | |
568 | { | |
569 | ourstatus->value.sig = 0; | |
570 | nto_inferior.exit_signo = 0; | |
571 | } | |
572 | else | |
573 | { | |
574 | ourstatus->value.sig = | |
2ea28649 | 575 | gdb_signal_from_host (status.info.si_signo); |
ac8c974e AR |
576 | nto_inferior.exit_signo = ourstatus->value.sig; |
577 | } | |
578 | break; | |
579 | ||
580 | case _DEBUG_WHY_TERMINATED: | |
581 | { | |
582 | int waitval = 0; | |
583 | ||
584 | TRACE (" TERMINATED\n"); | |
585 | waitpid (ptid_get_pid (ptid), &waitval, WNOHANG); | |
586 | if (nto_inferior.exit_signo) | |
587 | { | |
588 | /* Abnormal death. */ | |
589 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
590 | ourstatus->value.sig = nto_inferior.exit_signo; | |
591 | } | |
592 | else | |
593 | { | |
594 | /* Normal death. */ | |
595 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
596 | ourstatus->value.integer = WEXITSTATUS (waitval); | |
597 | } | |
598 | nto_inferior.exit_signo = 0; | |
599 | break; | |
600 | } | |
601 | ||
602 | case _DEBUG_WHY_REQUESTED: | |
603 | TRACE ("REQUESTED\n"); | |
604 | /* We are assuming a requested stop is due to a SIGINT. */ | |
605 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
a493e3e2 | 606 | ourstatus->value.sig = GDB_SIGNAL_INT; |
ac8c974e AR |
607 | nto_inferior.exit_signo = 0; |
608 | break; | |
609 | } | |
610 | } | |
611 | ||
612 | return ptid_build (status.pid, status.tid, 0); | |
613 | } | |
614 | ||
615 | /* Fetch inferior's registers for currently selected thread (CURRENT_INFERIOR). | |
616 | If REGNO is -1, fetch all registers, or REGNO register only otherwise. */ | |
617 | ||
618 | static void | |
442ea881 | 619 | nto_fetch_registers (struct regcache *regcache, int regno) |
ac8c974e AR |
620 | { |
621 | int regsize; | |
622 | procfs_greg greg; | |
623 | ptid_t ptid; | |
624 | ||
625 | TRACE ("%s (regno=%d)\n", __func__, regno); | |
626 | if (regno >= the_low_target.num_regs) | |
627 | return; | |
628 | ||
0bfdf32f | 629 | if (current_thread == NULL) |
ac8c974e | 630 | { |
0bfdf32f | 631 | TRACE ("current_thread is NULL\n"); |
ac8c974e AR |
632 | return; |
633 | } | |
0bfdf32f | 634 | ptid = thread_to_gdb_id (current_thread); |
ac8c974e AR |
635 | if (!nto_set_thread (ptid)) |
636 | return; | |
637 | ||
638 | if (devctl (nto_inferior.ctl_fd, DCMD_PROC_GETGREG, &greg, sizeof (greg), | |
639 | ®size) == EOK) | |
640 | { | |
641 | if (regno == -1) /* All registers. */ | |
642 | { | |
643 | for (regno = 0; regno != the_low_target.num_regs; ++regno) | |
644 | { | |
645 | const unsigned int registeroffset | |
646 | = the_low_target.register_offset (regno); | |
493e2a69 MS |
647 | supply_register (regcache, regno, |
648 | ((char *)&greg) + registeroffset); | |
ac8c974e AR |
649 | } |
650 | } | |
651 | else | |
652 | { | |
653 | const unsigned int registeroffset | |
654 | = the_low_target.register_offset (regno); | |
655 | if (registeroffset == -1) | |
656 | return; | |
442ea881 | 657 | supply_register (regcache, regno, ((char *)&greg) + registeroffset); |
ac8c974e AR |
658 | } |
659 | } | |
660 | else | |
661 | TRACE ("ERROR reading registers from inferior.\n"); | |
662 | } | |
663 | ||
664 | /* Store registers for currently selected thread (CURRENT_INFERIOR). | |
665 | We always store all registers, regardless of REGNO. */ | |
666 | ||
667 | static void | |
442ea881 | 668 | nto_store_registers (struct regcache *regcache, int regno) |
ac8c974e AR |
669 | { |
670 | procfs_greg greg; | |
671 | int err; | |
672 | ptid_t ptid; | |
673 | ||
674 | TRACE ("%s (regno:%d)\n", __func__, regno); | |
675 | ||
0bfdf32f | 676 | if (current_thread == NULL) |
ac8c974e | 677 | { |
0bfdf32f | 678 | TRACE ("current_thread is NULL\n"); |
ac8c974e AR |
679 | return; |
680 | } | |
0bfdf32f | 681 | ptid = thread_to_gdb_id (current_thread); |
ac8c974e AR |
682 | if (!nto_set_thread (ptid)) |
683 | return; | |
684 | ||
685 | memset (&greg, 0, sizeof (greg)); | |
686 | for (regno = 0; regno != the_low_target.num_regs; ++regno) | |
687 | { | |
688 | const unsigned int regoffset | |
689 | = the_low_target.register_offset (regno); | |
442ea881 | 690 | collect_register (regcache, regno, ((char *)&greg) + regoffset); |
ac8c974e AR |
691 | } |
692 | err = devctl (nto_inferior.ctl_fd, DCMD_PROC_SETGREG, &greg, sizeof (greg), | |
693 | 0); | |
694 | if (err != EOK) | |
695 | TRACE ("Error: setting registers.\n"); | |
696 | } | |
697 | ||
698 | /* Read LEN bytes from inferior's memory address MEMADDR into | |
699 | gdbserver's MYADDR buffer. | |
700 | ||
701 | Return 0 on success -1 otherwise. */ | |
702 | ||
703 | static int | |
704 | nto_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) | |
705 | { | |
706 | TRACE ("%s memaddr:0x%08lx, len:%d\n", __func__, memaddr, len); | |
707 | ||
708 | if (nto_xfer_memory (memaddr, myaddr, len, 0) != len) | |
709 | { | |
710 | TRACE ("Failed to read memory\n"); | |
711 | return -1; | |
712 | } | |
713 | ||
714 | return 0; | |
715 | } | |
716 | ||
717 | /* Write LEN bytes from gdbserver's buffer MYADDR into inferior's | |
718 | memory at address MEMADDR. | |
719 | ||
720 | Return 0 on success -1 otherwise. */ | |
721 | ||
722 | static int | |
723 | nto_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) | |
724 | { | |
725 | int len_written; | |
726 | ||
727 | TRACE ("%s memaddr: 0x%08llx len: %d\n", __func__, memaddr, len); | |
728 | if ((len_written = nto_xfer_memory (memaddr, (unsigned char *)myaddr, len, | |
729 | 1)) | |
730 | != len) | |
731 | { | |
732 | TRACE ("Wanted to write: %d but written: %d\n", len, len_written); | |
733 | return -1; | |
734 | } | |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
739 | /* Stop inferior. We always stop all threads. */ | |
740 | ||
741 | static void | |
742 | nto_request_interrupt (void) | |
743 | { | |
744 | TRACE ("%s\n", __func__); | |
745 | nto_set_thread (ptid_build (nto_inferior.pid, 1, 0)); | |
746 | if (EOK != devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, NULL, 0, 0)) | |
747 | TRACE ("Error stopping inferior.\n"); | |
748 | } | |
749 | ||
750 | /* Read auxiliary vector from inferior's memory into gdbserver's buffer | |
751 | MYADDR. We always read whole auxv. | |
752 | ||
753 | Return number of bytes stored in MYADDR buffer, 0 if OFFSET > 0 | |
754 | or -1 on error. */ | |
755 | ||
756 | static int | |
757 | nto_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len) | |
758 | { | |
759 | int err; | |
760 | CORE_ADDR initial_stack; | |
761 | procfs_info procinfo; | |
762 | ||
763 | TRACE ("%s\n", __func__); | |
764 | if (offset > 0) | |
765 | return 0; | |
766 | ||
767 | err = devctl (nto_inferior.ctl_fd, DCMD_PROC_INFO, &procinfo, | |
768 | sizeof procinfo, 0); | |
769 | if (err != EOK) | |
770 | return -1; | |
771 | ||
772 | initial_stack = procinfo.initial_stack; | |
773 | ||
774 | return nto_read_auxv_from_initial_stack (initial_stack, myaddr, len); | |
775 | } | |
776 | ||
802e8e6d PA |
777 | static int |
778 | nto_supports_z_point_type (char z_type) | |
779 | { | |
780 | switch (z_type) | |
781 | { | |
782 | case Z_PACKET_SW_BP: | |
783 | case Z_PACKET_HW_BP: | |
784 | case Z_PACKET_WRITE_WP: | |
785 | case Z_PACKET_READ_WP: | |
786 | case Z_PACKET_ACCESS_WP: | |
787 | return 1; | |
788 | default: | |
789 | return 0; | |
790 | } | |
791 | } | |
792 | ||
793 | /* Insert {break/watch}point at address ADDR. SIZE is not used. */ | |
ac8c974e AR |
794 | |
795 | static int | |
802e8e6d PA |
796 | nto_insert_point (enum raw_bkpt_type type, CORE_ADDR addr, |
797 | int size, struct raw_breakpoint *bp) | |
ac8c974e AR |
798 | { |
799 | int wtype = _DEBUG_BREAK_HW; /* Always request HW. */ | |
800 | ||
774ee6d2 | 801 | TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, size); |
ac8c974e AR |
802 | switch (type) |
803 | { | |
802e8e6d | 804 | case raw_bkpt_type_sw: |
ac8c974e AR |
805 | wtype = _DEBUG_BREAK_EXEC; |
806 | break; | |
802e8e6d | 807 | case raw_bkpt_type_hw: |
ac8c974e AR |
808 | wtype |= _DEBUG_BREAK_EXEC; |
809 | break; | |
802e8e6d | 810 | case raw_bkpt_type_write_wp: |
ac8c974e AR |
811 | wtype |= _DEBUG_BREAK_RW; |
812 | break; | |
802e8e6d | 813 | case raw_bkpt_type_read_wp: |
ac8c974e AR |
814 | wtype |= _DEBUG_BREAK_RD; |
815 | break; | |
802e8e6d | 816 | case raw_bkpt_type_access_wp: |
ac8c974e AR |
817 | wtype |= _DEBUG_BREAK_RW; |
818 | break; | |
819 | default: | |
820 | return 1; /* Not supported. */ | |
821 | } | |
822 | return nto_breakpoint (addr, wtype, 0); | |
823 | } | |
824 | ||
802e8e6d | 825 | /* Remove {break/watch}point at address ADDR. SIZE is not used. */ |
ac8c974e AR |
826 | |
827 | static int | |
802e8e6d PA |
828 | nto_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, |
829 | int size, struct raw_breakpoint *bp) | |
ac8c974e AR |
830 | { |
831 | int wtype = _DEBUG_BREAK_HW; /* Always request HW. */ | |
832 | ||
774ee6d2 | 833 | TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, size); |
ac8c974e AR |
834 | switch (type) |
835 | { | |
802e8e6d | 836 | case raw_bkpt_type_sw: |
ac8c974e AR |
837 | wtype = _DEBUG_BREAK_EXEC; |
838 | break; | |
802e8e6d | 839 | case raw_bkpt_type_hw: |
ac8c974e AR |
840 | wtype |= _DEBUG_BREAK_EXEC; |
841 | break; | |
802e8e6d | 842 | case raw_bkpt_type_write_wp: |
ac8c974e AR |
843 | wtype |= _DEBUG_BREAK_RW; |
844 | break; | |
802e8e6d | 845 | case raw_bkpt_type_read_wp: |
ac8c974e AR |
846 | wtype |= _DEBUG_BREAK_RD; |
847 | break; | |
802e8e6d | 848 | case raw_bkpt_type_access_wp: |
ac8c974e AR |
849 | wtype |= _DEBUG_BREAK_RW; |
850 | break; | |
851 | default: | |
852 | return 1; /* Not supported. */ | |
853 | } | |
854 | return nto_breakpoint (addr, wtype, -1); | |
855 | } | |
856 | ||
857 | /* Check if the reason of stop for current thread (CURRENT_INFERIOR) is | |
858 | a watchpoint. | |
859 | ||
860 | Return 1 if stopped by watchpoint, 0 otherwise. */ | |
861 | ||
862 | static int | |
863 | nto_stopped_by_watchpoint (void) | |
864 | { | |
865 | int ret = 0; | |
866 | ||
867 | TRACE ("%s\n", __func__); | |
0bfdf32f | 868 | if (nto_inferior.ctl_fd != -1 && current_thread != NULL) |
ac8c974e AR |
869 | { |
870 | ptid_t ptid; | |
871 | ||
0bfdf32f | 872 | ptid = thread_to_gdb_id (current_thread); |
ac8c974e AR |
873 | if (nto_set_thread (ptid)) |
874 | { | |
875 | const int watchmask = _DEBUG_FLAG_TRACE_RD | _DEBUG_FLAG_TRACE_WR | |
876 | | _DEBUG_FLAG_TRACE_MODIFY; | |
877 | procfs_status status; | |
878 | int err; | |
879 | ||
880 | err = devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, | |
881 | sizeof (status), 0); | |
882 | if (err == EOK && (status.flags & watchmask)) | |
883 | ret = 1; | |
884 | } | |
885 | } | |
886 | TRACE ("%s: %s\n", __func__, ret ? "yes" : "no"); | |
887 | return ret; | |
888 | } | |
889 | ||
890 | /* Get instruction pointer for CURRENT_INFERIOR thread. | |
891 | ||
892 | Return inferior's instruction pointer value, or 0 on error. */ | |
893 | ||
894 | static CORE_ADDR | |
895 | nto_stopped_data_address (void) | |
896 | { | |
897 | CORE_ADDR ret = (CORE_ADDR)0; | |
898 | ||
899 | TRACE ("%s\n", __func__); | |
0bfdf32f | 900 | if (nto_inferior.ctl_fd != -1 && current_thread != NULL) |
ac8c974e AR |
901 | { |
902 | ptid_t ptid; | |
903 | ||
0bfdf32f | 904 | ptid = thread_to_gdb_id (current_thread); |
ac8c974e AR |
905 | |
906 | if (nto_set_thread (ptid)) | |
907 | { | |
908 | procfs_status status; | |
909 | ||
910 | if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, | |
911 | sizeof (status), 0) == EOK) | |
912 | ret = status.ip; | |
913 | } | |
914 | } | |
915 | TRACE ("%s: 0x%08lx\n", __func__, ret); | |
916 | return ret; | |
917 | } | |
918 | ||
919 | /* We do not currently support non-stop. */ | |
920 | ||
921 | static int | |
922 | nto_supports_non_stop (void) | |
923 | { | |
924 | TRACE ("%s\n", __func__); | |
925 | return 0; | |
926 | } | |
927 | ||
fb78e89c AT |
928 | /* Implementation of the target_ops method "sw_breakpoint_from_kind". */ |
929 | ||
930 | static const gdb_byte * | |
931 | nto_sw_breakpoint_from_kind (int kind, int *size) | |
932 | { | |
933 | *size = the_low_target.breakpoint_len; | |
934 | return the_low_target.breakpoint; | |
935 | } | |
ac8c974e AR |
936 | |
937 | ||
938 | static struct target_ops nto_target_ops = { | |
939 | nto_create_inferior, | |
ece66d65 | 940 | NULL, /* post_create_inferior */ |
ac8c974e AR |
941 | nto_attach, |
942 | nto_kill, | |
943 | nto_detach, | |
505106cd | 944 | nto_mourn, |
ac8c974e AR |
945 | NULL, /* nto_join */ |
946 | nto_thread_alive, | |
947 | nto_resume, | |
948 | nto_wait, | |
949 | nto_fetch_registers, | |
950 | nto_store_registers, | |
90d74c30 | 951 | NULL, /* prepare_to_access_memory */ |
0146f85b | 952 | NULL, /* done_accessing_memory */ |
ac8c974e AR |
953 | nto_read_memory, |
954 | nto_write_memory, | |
955 | NULL, /* nto_look_up_symbols */ | |
956 | nto_request_interrupt, | |
957 | nto_read_auxv, | |
802e8e6d | 958 | nto_supports_z_point_type, |
ac8c974e AR |
959 | nto_insert_point, |
960 | nto_remove_point, | |
f5771b1d PA |
961 | NULL, /* stopped_by_sw_breakpoint */ |
962 | NULL, /* supports_stopped_by_sw_breakpoint */ | |
963 | NULL, /* stopped_by_hw_breakpoint */ | |
964 | NULL, /* supports_stopped_by_hw_breakpoint */ | |
70b90b91 | 965 | target_can_do_hardware_single_step, |
ac8c974e AR |
966 | nto_stopped_by_watchpoint, |
967 | nto_stopped_data_address, | |
968 | NULL, /* nto_read_offsets */ | |
969 | NULL, /* thread_db_set_tls_address */ | |
970 | NULL, | |
971 | hostio_last_error_from_errno, | |
972 | NULL, /* nto_qxfer_osdata */ | |
973 | NULL, /* xfer_siginfo */ | |
974 | nto_supports_non_stop, | |
975 | NULL, /* async */ | |
fb78e89c AT |
976 | NULL, /* start_non_stop */ |
977 | NULL, /* supports_multi_process */ | |
978 | NULL, /* supports_fork_events */ | |
979 | NULL, /* supports_vfork_events */ | |
980 | NULL, /* supports_exec_events */ | |
981 | NULL, /* handle_new_gdb_connection */ | |
982 | NULL, /* handle_monitor_command */ | |
983 | NULL, /* core_of_thread */ | |
984 | NULL, /* read_loadmap */ | |
985 | NULL, /* process_qsupported */ | |
986 | NULL, /* supports_tracepoints */ | |
987 | NULL, /* read_pc */ | |
988 | NULL, /* write_pc */ | |
989 | NULL, /* thread_stopped */ | |
990 | NULL, /* get_tib_address */ | |
991 | NULL, /* pause_all */ | |
992 | NULL, /* unpause_all */ | |
993 | NULL, /* stabilize_threads */ | |
994 | NULL, /* install_fast_tracepoint_jump_pad */ | |
995 | NULL, /* emit_ops */ | |
996 | NULL, /* supports_disable_randomization */ | |
997 | NULL, /* get_min_fast_tracepoint_insn_len */ | |
998 | NULL, /* qxfer_libraries_svr4 */ | |
999 | NULL, /* support_agent */ | |
1000 | NULL, /* support_btrace */ | |
1001 | NULL, /* enable_btrace */ | |
1002 | NULL, /* disable_btrace */ | |
1003 | NULL, /* read_btrace */ | |
1004 | NULL, /* read_btrace_conf */ | |
1005 | NULL, /* supports_range_stepping */ | |
1006 | NULL, /* pid_to_exec_file */ | |
1007 | NULL, /* multifs_open */ | |
1008 | NULL, /* multifs_unlink */ | |
1009 | NULL, /* multifs_readlink */ | |
1010 | NULL, /* breakpoint_kind_from_pc */ | |
1011 | nto_sw_breakpoint_from_kind, | |
ac8c974e AR |
1012 | }; |
1013 | ||
1014 | ||
1015 | /* Global function called by server.c. Initializes QNX Neutrino | |
1016 | gdbserver. */ | |
1017 | ||
1018 | void | |
1019 | initialize_low (void) | |
1020 | { | |
1021 | sigset_t set; | |
1022 | ||
1023 | TRACE ("%s\n", __func__); | |
1024 | set_target_ops (&nto_target_ops); | |
ac8c974e AR |
1025 | |
1026 | /* We use SIGUSR1 to gain control after we block waiting for a process. | |
1027 | We use sigwaitevent to wait. */ | |
1028 | sigemptyset (&set); | |
1029 | sigaddset (&set, SIGUSR1); | |
1030 | sigprocmask (SIG_BLOCK, &set, NULL); | |
1031 | } | |
1032 |