| | 1 | [[PageOutline]] |
| | 2 | |
| | 3 | = 10/12 = |
| | 4 | == trace getpid() syscall with GDB == |
| | 5 | * windell57:x86_64 s0711489$ gdb |
| | 6 | {{{ |
| | 7 | |
| | 8 | (gdb) set logging file gdb.getpid.log |
| | 9 | (gdb) set logging on |
| | 10 | Copying output to gdb.getpid.log. |
| | 11 | (gdb) file vmlinux |
| | 12 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| | 13 | (gdb) b sys_getpid |
| | 14 | Breakpoint 1 at 0xffffffff81048ce4: file kernel/timer.c, line 1344. |
| | 15 | (gdb) target remote localhost:8864 |
| | 16 | Remote debugging using localhost:8864 |
| | 17 | 0xffffffff810097a9 in native_safe_halt () |
| | 18 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| | 19 | 49 asm volatile("sti; hlt": : :"memory"); |
| | 20 | (gdb) c |
| | 21 | Continuing. |
| | 22 | |
| | 23 | Breakpoint 1, sys_getpid () at kernel/timer.c:1344 |
| | 24 | }}} |
| | 25 | * => attachment:gdb.getpid.log |
| | 26 | |
| | 27 | * on VM guest |
| | 28 | * s0711489@ubuntu-lucid64:~/coursework/KernelHack/02$ ./getpid |
| | 29 | {{{ |
| | 30 | getpid() -> 2143 |
| | 31 | }}} |
| | 32 | |
| | 33 | == trace log with source list and print data == |
| | 34 | * windell57:x86_64 s0711489$ gdb |
| | 35 | {{{ |
| | 36 | |
| | 37 | (gdb) file vmlinux |
| | 38 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| | 39 | (gdb) b sys_getpid |
| | 40 | Breakpoint 1 at 0xffffffff81048ce4: file kernel/timer.c, line 1344. |
| | 41 | (gdb) target remote localhost:8864 |
| | 42 | Remote debugging using localhost:8864 |
| | 43 | 0xffffffff810097a9 in native_safe_halt () |
| | 44 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| | 45 | 49 asm volatile("sti; hlt": : :"memory"); |
| | 46 | (gdb) c |
| | 47 | Continuing. |
| | 48 | |
| | 49 | Breakpoint 1, sys_getpid () at kernel/timer.c:1344 |
| | 50 | 1344 { |
| | 51 | (gdb) bt |
| | 52 | #0 sys_getpid () at kernel/timer.c:1344 |
| | 53 | #1 0xffffffff810029eb in ?? () |
| | 54 | #2 0x0000000000000246 in ?? () |
| | 55 | #3 0x00007fff70d418e0 in ?? () |
| | 56 | #4 0x00007f6c11b19210 in ?? () |
| | 57 | #5 0x00007f6c11b05300 in ?? () |
| | 58 | #6 0x0000000000000027 in ?? () |
| | 59 | #7 0x0000000000000000 in ?? () |
| | 60 | (gdb) l |
| | 61 | 1339 * which case the tgid is the same in all threads of the same group. |
| | 62 | 1340 * |
| | 63 | 1341 * This is SMP safe as current->tgid does not change. |
| | 64 | 1342 */ |
| | 65 | 1343 SYSCALL_DEFINE0(getpid) |
| | 66 | 1344 { |
| | 67 | 1345 return task_tgid_vnr(current); |
| | 68 | 1346 } |
| | 69 | 1347 |
| | 70 | 1348 /* |
| | 71 | (gdb) s |
| | 72 | 1345 return task_tgid_vnr(current); |
| | 73 | (gdb) |
| | 74 | get_current () |
| | 75 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 |
| | 76 | 14 return percpu_read_stable(current_task); |
| | 77 | (gdb) l |
| | 78 | 9 |
| | 79 | 10 DECLARE_PER_CPU(struct task_struct *, current_task); |
| | 80 | 11 |
| | 81 | 12 static __always_inline struct task_struct *get_current(void) |
| | 82 | 13 { |
| | 83 | 14 return percpu_read_stable(current_task); |
| | 84 | 15 } |
| | 85 | 16 |
| | 86 | 17 #define current get_current() |
| | 87 | 18 |
| | 88 | (gdb) bt |
| | 89 | #0 get_current () |
| | 90 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 |
| | 91 | #1 sys_getpid () at kernel/timer.c:1345 |
| | 92 | #2 0xffffffff810029eb in ?? () |
| | 93 | #3 0x0000000000000246 in ?? () |
| | 94 | #4 0x00007fff63204890 in ?? () |
| | 95 | #5 0x00007fc615a98210 in ?? () |
| | 96 | #6 0x00007fc615a84300 in ?? () |
| | 97 | #7 0x0000000000000027 in ?? () |
| | 98 | #8 0x0000000000000000 in ?? () |
| | 99 | (gdb) p current_task |
| | 100 | Cannot access memory at address 0xb540 |
| | 101 | (gdb) ptype current_task |
| | 102 | type = struct task_struct { |
| | 103 | |
| | 104 | (snip) |
| | 105 | |
| | 106 | (gdb) s |
| | 107 | sys_getpid () at kernel/timer.c:1344 |
| | 108 | 1344 { |
| | 109 | (gdb) l |
| | 110 | 1339 * which case the tgid is the same in all threads of the same group. |
| | 111 | 1340 * |
| | 112 | 1341 * This is SMP safe as current->tgid does not change. |
| | 113 | 1342 */ |
| | 114 | 1343 SYSCALL_DEFINE0(getpid) |
| | 115 | 1344 { |
| | 116 | 1345 return task_tgid_vnr(current); |
| | 117 | 1346 } |
| | 118 | 1347 |
| | 119 | 1348 /* |
| | 120 | (gdb) bt |
| | 121 | #0 sys_getpid () at kernel/timer.c:1344 |
| | 122 | #1 0xffffffff810029eb in ?? () |
| | 123 | #2 0x0000000000000246 in ?? () |
| | 124 | #3 0x00007fff63204890 in ?? () |
| | 125 | #4 0x00007fc615a98210 in ?? () |
| | 126 | #5 0x00007fc615a84300 in ?? () |
| | 127 | #6 0x0000000000000027 in ?? () |
| | 128 | #7 0x0000000000000000 in ?? () |
| | 129 | (gdb) s |
| | 130 | 1345 return task_tgid_vnr(current); |
| | 131 | (gdb) s |
| | 132 | task_tgid_vnr (tsk=0xffff88001bbe5880) at include/linux/sched.h:1606 |
| | 133 | 1606 { |
| | 134 | (gdb) l |
| | 135 | 1601 } |
| | 136 | 1602 |
| | 137 | 1603 pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns); |
| | 138 | 1604 |
| | 139 | 1605 static inline pid_t task_tgid_vnr(struct task_struct *tsk) |
| | 140 | 1606 { |
| | 141 | 1607 return pid_vnr(task_tgid(tsk)); |
| | 142 | 1608 } |
| | 143 | 1609 |
| | 144 | 1610 |
| | 145 | (gdb) bt |
| | 146 | #0 task_tgid_vnr (tsk=0xffff88001bbe5880) at include/linux/sched.h:1606 |
| | 147 | #1 0xffffffff81048cf6 in sys_getpid () at kernel/timer.c:1345 |
| | 148 | #2 0xffffffff810029eb in ?? () |
| | 149 | #3 0x0000000000000246 in ?? () |
| | 150 | #4 0x00007fff63204890 in ?? () |
| | 151 | #5 0x00007fc615a98210 in ?? () |
| | 152 | #6 0x00007fc615a84300 in ?? () |
| | 153 | #7 0x0000000000000027 in ?? () |
| | 154 | #8 0x0000000000000000 in ?? () |
| | 155 | (gdb) p tsk |
| | 156 | $2 = (struct task_struct *) 0xffff88001bbe5880 |
| | 157 | (gdb) ptype tsk |
| | 158 | type = struct task_struct { |
| | 159 | |
| | 160 | (snip) |
| | 161 | |
| | 162 | pid_t pid; |
| | 163 | pid_t tgid; |
| | 164 | |
| | 165 | (snip) |
| | 166 | |
| | 167 | struct task_struct *group_leader; |
| | 168 | struct list_head ptraced; |
| | 169 | struct list_head ptrace_entry; |
| | 170 | struct pid_link pids[3]; |
| | 171 | |
| | 172 | (snip) |
| | 173 | |
| | 174 | (gdb) ptype tsk->group_leader->pids |
| | 175 | type = struct pid_link { |
| | 176 | struct hlist_node node; |
| | 177 | struct pid *pid; |
| | 178 | } [3] |
| | 179 | |
| | 180 | (snip) |
| | 181 | |
| | 182 | (gdb) s |
| | 183 | 1607 return pid_vnr(task_tgid(tsk)); |
| | 184 | (gdb) |
| | 185 | 1606 { |
| | 186 | (gdb) |
| | 187 | 1607 return pid_vnr(task_tgid(tsk)); |
| | 188 | (gdb) |
| | 189 | pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:444 |
| | 190 | 444 { |
| | 191 | (gdb) bt |
| | 192 | #0 pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:444 |
| | 193 | #1 0xffffffff81048783 in task_tgid_vnr (tsk=<value optimized out>) |
| | 194 | at include/linux/sched.h:1607 |
| | 195 | #2 0xffffffff81048cf6 in sys_getpid () at kernel/timer.c:1345 |
| | 196 | #3 0xffffffff810029eb in ?? () |
| | 197 | #4 0x0000000000000246 in ?? () |
| | 198 | #5 0x00007fff63204890 in ?? () |
| | 199 | #6 0x00007fc615a98210 in ?? () |
| | 200 | #7 0x00007fc615a84300 in ?? () |
| | 201 | #8 0x0000000000000027 in ?? () |
| | 202 | #9 0x0000000000000000 in ?? () |
| | 203 | (gdb) l |
| | 204 | 439 } |
| | 205 | 440 return nr; |
| | 206 | 441 } |
| | 207 | 442 |
| | 208 | 443 pid_t pid_vnr(struct pid *pid) |
| | 209 | 444 { |
| | 210 | 445 return pid_nr_ns(pid, current->nsproxy->pid_ns); |
| | 211 | 446 } |
| | 212 | 447 EXPORT_SYMBOL_GPL(pid_vnr); |
| | 213 | 448 |
| | 214 | (gdb) ptype pid |
| | 215 | type = struct pid { |
| | 216 | atomic_t count; |
| | 217 | unsigned int level; |
| | 218 | struct hlist_head tasks[3]; |
| | 219 | struct rcu_head rcu; |
| | 220 | struct upid numbers[1]; |
| | 221 | } * |
| | 222 | (gdb) up |
| | 223 | #1 0xffffffff81048783 in task_tgid_vnr (tsk=<value optimized out>) |
| | 224 | at include/linux/sched.h:1607 |
| | 225 | 1607 return pid_vnr(task_tgid(tsk)); |
| | 226 | (gdb) ptype tsk->nsproxy->pid_ns |
| | 227 | type = struct pid_namespace { |
| | 228 | struct kref kref; |
| | 229 | struct pidmap pidmap[128]; |
| | 230 | int last_pid; |
| | 231 | struct task_struct *child_reaper; |
| | 232 | struct kmem_cache *pid_cachep; |
| | 233 | unsigned int level; |
| | 234 | struct pid_namespace *parent; |
| | 235 | struct vfsmount *proc_mnt; |
| | 236 | struct bsd_acct_struct *bacct; |
| | 237 | } * |
| | 238 | (gdb) down |
| | 239 | #0 pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:444 |
| | 240 | 444 { |
| | 241 | (gdb) s |
| | 242 | 445 return pid_nr_ns(pid, current->nsproxy->pid_ns); |
| | 243 | (gdb) |
| | 244 | get_current (pid=0xffff88001fb55400) |
| | 245 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 |
| | 246 | 14 return percpu_read_stable(current_task); |
| | 247 | (gdb) p current_task |
| | 248 | Cannot access memory at address 0xb540 |
| | 249 | (gdb) s |
| | 250 | pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:445 |
| | 251 | 445 return pid_nr_ns(pid, current->nsproxy->pid_ns); |
| | 252 | (gdb) s |
| | 253 | 444 { |
| | 254 | (gdb) |
| | 255 | 445 return pid_nr_ns(pid, current->nsproxy->pid_ns); |
| | 256 | (gdb) |
| | 257 | pid_nr_ns (pid=0xffff88001fb55400, ns=0xffffffff8181bfe0) at kernel/pid.c:431 |
| | 258 | 431 { |
| | 259 | (gdb) bt |
| | 260 | #0 pid_nr_ns (pid=0xffff88001fb55400, ns=0xffffffff8181bfe0) |
| | 261 | at kernel/pid.c:431 |
| | 262 | #1 0xffffffff81052db6 in pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:445 |
| | 263 | #2 0xffffffff81048783 in task_tgid_vnr (tsk=<value optimized out>) |
| | 264 | at include/linux/sched.h:1607 |
| | 265 | #3 0xffffffff81048cf6 in sys_getpid () at kernel/timer.c:1345 |
| | 266 | #4 0xffffffff810029eb in ?? () |
| | 267 | #5 0x0000000000000246 in ?? () |
| | 268 | #6 0x00007fff63204890 in ?? () |
| | 269 | #7 0x00007fc615a98210 in ?? () |
| | 270 | #8 0x00007fc615a84300 in ?? () |
| | 271 | #9 0x0000000000000027 in ?? () |
| | 272 | #10 0x0000000000000000 in ?? () |
| | 273 | (gdb) l |
| | 274 | 426 return pid; |
| | 275 | 427 } |
| | 276 | 428 EXPORT_SYMBOL_GPL(find_get_pid); |
| | 277 | 429 |
| | 278 | 430 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) |
| | 279 | 431 { |
| | 280 | 432 struct upid *upid; |
| | 281 | 433 pid_t nr = 0; |
| | 282 | 434 |
| | 283 | 435 if (pid && ns->level <= pid->level) { |
| | 284 | (gdb) l |
| | 285 | 436 upid = &pid->numbers[ns->level]; |
| | 286 | 437 if (upid->ns == ns) |
| | 287 | 438 nr = upid->nr; |
| | 288 | 439 } |
| | 289 | 440 return nr; |
| | 290 | 441 } |
| | 291 | 442 |
| | 292 | 443 pid_t pid_vnr(struct pid *pid) |
| | 293 | 444 { |
| | 294 | 445 return pid_nr_ns(pid, current->nsproxy->pid_ns); |
| | 295 | (gdb) ptype pid |
| | 296 | type = struct pid { |
| | 297 | atomic_t count; |
| | 298 | unsigned int level; |
| | 299 | struct hlist_head tasks[3]; |
| | 300 | struct rcu_head rcu; |
| | 301 | struct upid numbers[1]; |
| | 302 | } * |
| | 303 | (gdb) ptype ns |
| | 304 | type = struct pid_namespace { |
| | 305 | struct kref kref; |
| | 306 | struct pidmap pidmap[128]; |
| | 307 | int last_pid; |
| | 308 | struct task_struct *child_reaper; |
| | 309 | struct kmem_cache *pid_cachep; |
| | 310 | unsigned int level; |
| | 311 | struct pid_namespace *parent; |
| | 312 | struct vfsmount *proc_mnt; |
| | 313 | struct bsd_acct_struct *bacct; |
| | 314 | } * |
| | 315 | type = struct upid { |
| | 316 | int nr; |
| | 317 | struct pid_namespace *ns; |
| | 318 | struct hlist_node pid_chain; |
| | 319 | } * |
| | 320 | (gdb) ptype pid_t |
| | 321 | type = int |
| | 322 | (gdb) p ns->level |
| | 323 | $5 = 0 |
| | 324 | (gdb) p pid->level |
| | 325 | $6 = 0 |
| | 326 | (gdb) p ns->level <= pid->level |
| | 327 | $7 = 1 |
| | 328 | (gdb) p ns |
| | 329 | $8 = (struct pid_namespace *) 0xffffffff8181bfe0 |
| | 330 | (gdb) p &pid->numbers[ns->level] |
| | 331 | $9 = (struct upid *) 0xffff88001fb55430 |
| | 332 | (gdb) p (&pid->numbers[ns->level])->ns |
| | 333 | $10 = (struct pid_namespace *) 0xffffffff8181bfe0 |
| | 334 | (gdb) info locals |
| | 335 | upid = <value optimized out> |
| | 336 | nr = <value optimized out> |
| | 337 | (gdb) p nr |
| | 338 | $11 = <value optimized out> |
| | 339 | (gdb) p (&pid->numbers[ns->level])->nr |
| | 340 | $12 = 2225 |
| | 341 | (gdb) s |
| | 342 | 435 if (pid && ns->level <= pid->level) { |
| | 343 | (gdb) |
| | 344 | 431 { |
| | 345 | (gdb) |
| | 346 | 435 if (pid && ns->level <= pid->level) { |
| | 347 | (gdb) |
| | 348 | 436 upid = &pid->numbers[ns->level]; |
| | 349 | (gdb) |
| | 350 | 437 if (upid->ns == ns) |
| | 351 | (gdb) p upid |
| | 352 | $13 = (struct upid *) 0xffff88001fb55430 |
| | 353 | (gdb) s |
| | 354 | 438 nr = upid->nr; |
| | 355 | (gdb) p upid->ns == ns |
| | 356 | $14 = 1 |
| | 357 | (gdb) p nr |
| | 358 | $15 = <value optimized out> |
| | 359 | (gdb) p upid->nr |
| | 360 | $16 = 2225 |
| | 361 | (gdb) s |
| | 362 | 441 } |
| | 363 | (gdb) |
| | 364 | pid_vnr (pid=<value optimized out>) at kernel/pid.c:446 |
| | 365 | 446 } |
| | 366 | (gdb) s |
| | 367 | task_tgid_vnr (tsk=<value optimized out>) at include/linux/sched.h:1608 |
| | 368 | 1608 } |
| | 369 | (gdb) |
| | 370 | sys_getpid () at kernel/timer.c:1346 |
| | 371 | 1346 } |
| | 372 | (gdb) |
| | 373 | sys_getpid () at kernel/timer.c:1345 |
| | 374 | 1345 return task_tgid_vnr(current); |
| | 375 | (gdb) |
| | 376 | sys_getpid () at kernel/timer.c:1346 |
| | 377 | 1346 } |
| | 378 | (gdb) |
| | 379 | }}} |
| | 380 | |
| | 381 | * on VM guest |
| | 382 | * s0711489@ubuntu-lucid64:~/coursework/KernelHack/02$ ./getpid |
| | 383 | {{{ |
| | 384 | getpid() -> 2225 |
| | 385 | }}} |
| | 386 | |
| | 387 | === location of functions on source file === |
| | 388 | * windell57:x86_64 s0711489$ grep percpu_read_stable -r . |
| | 389 | {{{ |
| | 390 | (snip) |
| | 391 | |
| | 392 | ./arch/x86/include/asm/percpu.h:#define percpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var))) |
| | 393 | |
| | 394 | (snip) |
| | 395 | }}} |
| | 396 | |
| | 397 | * windell57:x86_64 s0711489$ grep task_tgid -r . |
| | 398 | {{{ |
| | 399 | (snip) |
| | 400 | |
| | 401 | ./include/linux/sched.h:static inline struct pid *task_tgid(struct task_struct *task) |
| | 402 | |
| | 403 | (snip) |
| | 404 | }}} |
| | 405 | |
| | 406 | * include/linux/sched.h |
| | 407 | {{{ |
| | 408 | static inline struct pid *task_tgid(struct task_struct *task) |
| | 409 | { |
| | 410 | return task->group_leader->pids[PIDTYPE_PID].pid; |
| | 411 | } |
| | 412 | }}} |
| | 413 | |
| | 414 | === get_current function === |
| | 415 | * windell57:02 s0711489$ gcc -E ../linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h -I ~/coursework/KernelHack/linux-2.6.35.14/x86_64/include -I ~/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/ > current.h |
| | 416 | {{{ |
| | 417 | In file included from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/percpu.h:44, |
| | 418 | from ../linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:5: |
| | 419 | /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/include/linux/kernel.h:733:2: warning: #warning Attempt to use kernel headers from user space, see http: |
| | 420 | }}} |
| | 421 | * pre-processed arch/x86/include/asm/current.h |
| | 422 | {{{ |
| | 423 | static __always_inline struct task_struct *get_current(void) |
| | 424 | { |
| | 425 | return ({ typeof(current_task) pfo_ret__; switch (sizeof(current_task)) { case 1: asm("mov" "b ""%P" "1"",%0" : "=q" (pfo_ret__) : "p" (&(current_task))); break; case 2: asm("mov" "w ""%P" "1"",%0" : "=r" (pfo_ret__) : "p" (&(current_task))); break; case 4: asm("mov" "l ""%P" "1"",%0" : "=r" (pfo_ret__) : "p" (&(current_task))); break; case 8: asm("mov" "q ""%P" "1"",%0" : "=r" (pfo_ret__) : "p" (&(current_task))); break; default: __bad_percpu_size(); } pfo_ret__; }); |
| | 426 | } |
| | 427 | }}} |