MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / kernel / ptrace.c
blobb14b4a4677296a2e9fc2c4c961b314ffd64e623d
1 /*
2 * linux/kernel/ptrace.c
4 * (C) Copyright 1999 Linus Torvalds
6 * Common interfaces for "ptrace()" which we do not want
7 * to continually duplicate across every architecture.
8 */
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/mm.h>
14 #include <linux/highmem.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
17 #include <linux/ptrace.h>
18 #include <linux/security.h>
20 #include <asm/pgtable.h>
21 #include <asm/uaccess.h>
24 * ptrace a task: make the debugger its new parent and
25 * move it to the ptrace list.
27 * Must be called with the tasklist lock write-held.
29 void __ptrace_link(task_t *child, task_t *new_parent)
31 if (!list_empty(&child->ptrace_list))
32 BUG();
33 if (child->parent == new_parent)
34 return;
35 list_add(&child->ptrace_list, &child->parent->ptrace_children);
36 REMOVE_LINKS(child);
37 child->parent = new_parent;
38 SET_LINKS(child);
42 * unptrace a task: move it back to its original parent and
43 * remove it from the ptrace list.
45 * Must be called with the tasklist lock write-held.
47 void __ptrace_unlink(task_t *child)
49 if (!child->ptrace)
50 BUG();
51 child->ptrace = 0;
52 if (list_empty(&child->ptrace_list))
53 return;
54 list_del_init(&child->ptrace_list);
55 REMOVE_LINKS(child);
56 child->parent = child->real_parent;
57 SET_LINKS(child);
59 if (child->state == TASK_TRACED) {
61 * Turn a tracing stop into a normal stop now,
62 * since with no tracer there would be no way
63 * to wake it up with SIGCONT or SIGKILL.
65 child->state = TASK_STOPPED;
70 * Check that we have indeed attached to the thing..
72 int ptrace_check_attach(struct task_struct *child, int kill)
74 int ret = -ESRCH;
77 * We take the read lock around doing both checks to close a
78 * possible race where someone else was tracing our child and
79 * detached between these two checks. After this locked check,
80 * we are sure that this is our traced child and that can only
81 * be changed by us so it's not changing right after this.
83 read_lock(&tasklist_lock);
84 if ((child->ptrace & PT_PTRACED) && child->parent == current &&
85 child->signal != NULL) {
86 ret = 0;
87 spin_lock_irq(&child->sighand->siglock);
88 if (child->state == TASK_STOPPED) {
89 child->state = TASK_TRACED;
90 } else if (child->state != TASK_TRACED && !kill) {
91 ret = -ESRCH;
93 spin_unlock_irq(&child->sighand->siglock);
95 read_unlock(&tasklist_lock);
97 if (!ret && !kill) {
98 wait_task_inactive(child);
101 /* All systems go.. */
102 return ret;
105 int ptrace_attach(struct task_struct *task)
107 int retval;
108 task_lock(task);
109 retval = -EPERM;
110 if (task->pid <= 1)
111 goto bad;
112 if (task == current)
113 goto bad;
114 if (!task->mm)
115 goto bad;
116 if(((current->uid != task->euid) ||
117 (current->uid != task->suid) ||
118 (current->uid != task->uid) ||
119 (current->gid != task->egid) ||
120 (current->gid != task->sgid) ||
121 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
122 goto bad;
123 rmb();
124 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
125 goto bad;
126 /* the same process cannot be attached many times */
127 if (task->ptrace & PT_PTRACED)
128 goto bad;
129 retval = security_ptrace(current, task);
130 if (retval)
131 goto bad;
133 /* Go */
134 task->ptrace |= PT_PTRACED;
135 if (capable(CAP_SYS_PTRACE))
136 task->ptrace |= PT_PTRACE_CAP;
137 task_unlock(task);
139 write_lock_irq(&tasklist_lock);
140 __ptrace_link(task, current);
141 write_unlock_irq(&tasklist_lock);
143 force_sig_specific(SIGSTOP, task);
144 return 0;
146 bad:
147 task_unlock(task);
148 return retval;
151 int ptrace_detach(struct task_struct *child, unsigned int data)
153 if ((unsigned long) data > _NSIG)
154 return -EIO;
156 /* Architecture-specific hardware disable .. */
157 ptrace_disable(child);
159 /* .. re-parent .. */
160 child->exit_code = data;
162 write_lock_irq(&tasklist_lock);
163 __ptrace_unlink(child);
164 /* .. and wake it up. */
165 if (child->state != TASK_ZOMBIE)
166 wake_up_process(child);
167 write_unlock_irq(&tasklist_lock);
169 return 0;
173 * Access another process' address space.
174 * Source/target buffer must be kernel space,
175 * Do not walk the page table directly, use get_user_pages
178 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
180 struct mm_struct *mm;
181 struct vm_area_struct *vma;
182 struct page *page;
183 void *old_buf = buf;
185 mm = get_task_mm(tsk);
186 if (!mm)
187 return 0;
189 down_read(&mm->mmap_sem);
190 /* ignore errors, just check how much was sucessfully transfered */
191 while (len) {
192 int bytes, ret, offset;
193 void *maddr;
195 ret = get_user_pages(tsk, mm, addr, 1,
196 write, 1, &page, &vma);
197 if (ret <= 0)
198 break;
200 bytes = len;
201 offset = addr & (PAGE_SIZE-1);
202 if (bytes > PAGE_SIZE-offset)
203 bytes = PAGE_SIZE-offset;
205 maddr = kmap(page);
206 if (write) {
207 copy_to_user_page(vma, page, addr,
208 maddr + offset, buf, bytes);
209 set_page_dirty_lock(page);
210 } else {
211 copy_from_user_page(vma, page, addr,
212 buf, maddr + offset, bytes);
214 kunmap(page);
215 page_cache_release(page);
216 len -= bytes;
217 buf += bytes;
218 addr += bytes;
220 up_read(&mm->mmap_sem);
221 mmput(mm);
223 return buf - old_buf;
226 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
228 int copied = 0;
230 while (len > 0) {
231 char buf[128];
232 int this_len, retval;
234 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
235 retval = access_process_vm(tsk, src, buf, this_len, 0);
236 if (!retval) {
237 if (copied)
238 break;
239 return -EIO;
241 if (copy_to_user(dst, buf, retval))
242 return -EFAULT;
243 copied += retval;
244 src += retval;
245 dst += retval;
246 len -= retval;
248 return copied;
251 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
253 int copied = 0;
255 while (len > 0) {
256 char buf[128];
257 int this_len, retval;
259 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
260 if (copy_from_user(buf, src, this_len))
261 return -EFAULT;
262 retval = access_process_vm(tsk, dst, buf, this_len, 1);
263 if (!retval) {
264 if (copied)
265 break;
266 return -EIO;
268 copied += retval;
269 src += retval;
270 dst += retval;
271 len -= retval;
273 return copied;
276 static int ptrace_setoptions(struct task_struct *child, long data)
278 child->ptrace &= ~PT_TRACE_MASK;
280 if (data & PTRACE_O_TRACESYSGOOD)
281 child->ptrace |= PT_TRACESYSGOOD;
283 if (data & PTRACE_O_TRACEFORK)
284 child->ptrace |= PT_TRACE_FORK;
286 if (data & PTRACE_O_TRACEVFORK)
287 child->ptrace |= PT_TRACE_VFORK;
289 if (data & PTRACE_O_TRACECLONE)
290 child->ptrace |= PT_TRACE_CLONE;
292 if (data & PTRACE_O_TRACEEXEC)
293 child->ptrace |= PT_TRACE_EXEC;
295 if (data & PTRACE_O_TRACEVFORKDONE)
296 child->ptrace |= PT_TRACE_VFORK_DONE;
298 if (data & PTRACE_O_TRACEEXIT)
299 child->ptrace |= PT_TRACE_EXIT;
301 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
304 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
306 if (child->last_siginfo == NULL)
307 return -EINVAL;
308 return copy_siginfo_to_user(data, child->last_siginfo);
311 static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
313 if (child->last_siginfo == NULL)
314 return -EINVAL;
315 if (copy_from_user(child->last_siginfo, data, sizeof (siginfo_t)) != 0)
316 return -EFAULT;
317 return 0;
320 int ptrace_request(struct task_struct *child, long request,
321 long addr, long data)
323 int ret = -EIO;
325 switch (request) {
326 #ifdef PTRACE_OLDSETOPTIONS
327 case PTRACE_OLDSETOPTIONS:
328 #endif
329 case PTRACE_SETOPTIONS:
330 ret = ptrace_setoptions(child, data);
331 break;
332 case PTRACE_GETEVENTMSG:
333 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
334 break;
335 case PTRACE_GETSIGINFO:
336 ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
337 break;
338 case PTRACE_SETSIGINFO:
339 ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
340 break;
341 default:
342 break;
345 return ret;