Merge branch 'vendor/MPFR' into gcc441
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt.c
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt.c,v 1.4 2005/09/10 18:25:53 marcel Exp $
27  * $DragonFly: src/gnu/usr.bin/gdb/kgdb/trgt.c,v 1.2 2008/01/14 21:36:38 corecode Exp $
28  */
29
30 #include <sys/cdefs.h>
31
32 #include <sys/param.h>
33 /*#include <sys/proc.h>*/
34 #include <sys/sysctl.h>
35 #include <sys/user.h>
36 #include <kvm.h>
37
38 #include <defs.h>
39 #include <command.h>
40 #include <frame-unwind.h>
41 #include <gdbthread.h>
42 #include <inferior.h>
43 #include <regcache.h>
44 #include <target.h>
45
46 #include "kgdb.h"
47
48 static struct target_ops kgdb_trgt_ops;
49
50 static char *
51 kgdb_trgt_extra_thread_info(struct thread_info *ti)
52 {
53         static char buf[64];
54         char *p, *s;
55
56         p = buf + snprintf(buf, sizeof(buf), "PID=%d", ptid_get_pid(ti->ptid));
57         s = kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid));
58         if (s != NULL)
59                 snprintf(p, sizeof(buf) - (p - buf), ": %s", s);
60         return (buf);
61 }
62
63 static void
64 kgdb_trgt_files_info(struct target_ops *target)
65 {
66         struct target_ops *tb;
67
68         tb = find_target_beneath(target);
69         if (tb->to_files_info != NULL)
70                 tb->to_files_info(tb);
71 }
72
73 static void
74 kgdb_trgt_find_new_threads(void)
75 {
76         struct target_ops *tb;
77
78         if (kvm != NULL)
79                 return;
80
81         tb = find_target_beneath(&kgdb_trgt_ops);
82         if (tb->to_find_new_threads != NULL)
83                 tb->to_find_new_threads();
84 }
85
86 static char *
87 kgdb_trgt_pid_to_str(ptid_t ptid)
88 {
89         static char buf[33];
90
91         snprintf(buf, sizeof(buf), "Thread %#lx", ptid_get_tid(ptid));
92         return (buf);
93 }
94
95 static int
96 kgdb_trgt_thread_alive(ptid_t ptid)
97 {
98         return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
99 }
100
101 static LONGEST
102 kgdb_trgt_xfer_partial(struct target_ops *ops, enum target_object object,
103                        const char *annex, gdb_byte *readbuf,
104                        const gdb_byte *writebuf,
105                        ULONGEST offset, LONGEST len)
106 {
107         if (kvm != NULL) {
108                 if (len == 0)
109                         return (0);
110                 if (writebuf != NULL)
111                         return (kvm_write(kvm, offset, writebuf, len));
112                 if (readbuf != NULL)
113                         return (kvm_read(kvm, offset, readbuf, len));
114         }
115         return (ops->beneath->to_xfer_partial(ops->beneath, object, annex,
116                                               readbuf, writebuf, offset, len));
117 }
118
119 void
120 kgdb_target(void)
121 {
122         struct kthr *kt;
123         struct thread_info *ti;
124
125         kgdb_trgt_ops.to_magic = OPS_MAGIC;
126         kgdb_trgt_ops.to_shortname = "kernel";
127         kgdb_trgt_ops.to_longname = "kernel core files.";
128         kgdb_trgt_ops.to_doc = "Kernel core files.";
129         kgdb_trgt_ops.to_stratum = thread_stratum;
130         kgdb_trgt_ops.to_has_memory = 1;
131         kgdb_trgt_ops.to_has_registers = 1;
132         kgdb_trgt_ops.to_has_stack = 1;
133
134         kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
135         kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
136         kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
137         kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
138         kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
139         kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
140         kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
141         kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial;
142         add_target(&kgdb_trgt_ops);
143         push_target(&kgdb_trgt_ops);
144
145         kt = kgdb_thr_first();
146         while (kt != NULL) {
147                 ti = add_thread(ptid_build(kt->pid, 0, kt->tid));
148                 kt = kgdb_thr_next(kt);
149         }
150         if (curkthr != 0)
151                 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid);
152 }