Changes between Initial Version and Version 1 of Dev/KernelHack/COINS/worklog/201110


Ignore:
Timestamp:
Oct 12, 2011 12:38:06 PM (13 years ago)
Author:
mitty
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Dev/KernelHack/COINS/worklog/201110

    v1 v1  
     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 
     10Copying output to gdb.getpid.log. 
     11(gdb) file vmlinux 
     12Reading 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 
     14Breakpoint 1 at 0xffffffff81048ce4: file kernel/timer.c, line 1344. 
     15(gdb) target remote localhost:8864 
     16Remote debugging using localhost:8864 
     170xffffffff810097a9 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 
     1949              asm volatile("sti; hlt": : :"memory"); 
     20(gdb) c 
     21Continuing. 
     22 
     23Breakpoint 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{{{ 
     30getpid() -> 2143 
     31}}} 
     32 
     33 == trace log with source list and print data == 
     34* windell57:x86_64 s0711489$ gdb 
     35{{{ 
     36 
     37(gdb) file vmlinux 
     38Reading 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 
     40Breakpoint 1 at 0xffffffff81048ce4: file kernel/timer.c, line 1344. 
     41(gdb) target remote localhost:8864 
     42Remote debugging using localhost:8864 
     430xffffffff810097a9 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 
     4549              asm volatile("sti; hlt": : :"memory"); 
     46(gdb) c 
     47Continuing. 
     48 
     49Breakpoint 1, sys_getpid () at kernel/timer.c:1344 
     501344    { 
     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 
     611339     * which case the tgid is the same in all threads of the same group. 
     621340     * 
     631341     * This is SMP safe as current->tgid does not change. 
     641342     */ 
     651343    SYSCALL_DEFINE0(getpid) 
     661344    { 
     671345            return task_tgid_vnr(current); 
     681346    } 
     691347 
     701348    /* 
     71(gdb) s 
     721345            return task_tgid_vnr(current); 
     73(gdb) 
     74get_current () 
     75    at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 
     7614              return percpu_read_stable(current_task); 
     77(gdb) l 
     789 
     7910      DECLARE_PER_CPU(struct task_struct *, current_task); 
     8011 
     8112      static __always_inline struct task_struct *get_current(void) 
     8213      { 
     8314              return percpu_read_stable(current_task); 
     8415      } 
     8516 
     8617      #define current get_current() 
     8718 
     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 
     100Cannot access memory at address 0xb540 
     101(gdb) ptype current_task 
     102type = struct task_struct { 
     103 
     104(snip) 
     105 
     106(gdb) s 
     107sys_getpid () at kernel/timer.c:1344 
     1081344    { 
     109(gdb) l 
     1101339     * which case the tgid is the same in all threads of the same group. 
     1111340     * 
     1121341     * This is SMP safe as current->tgid does not change. 
     1131342     */ 
     1141343    SYSCALL_DEFINE0(getpid) 
     1151344    { 
     1161345            return task_tgid_vnr(current); 
     1171346    } 
     1181347 
     1191348    /* 
     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 
     1301345            return task_tgid_vnr(current); 
     131(gdb) s 
     132task_tgid_vnr (tsk=0xffff88001bbe5880) at include/linux/sched.h:1606 
     1331606    { 
     134(gdb) l 
     1351601    } 
     1361602 
     1371603    pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns); 
     1381604 
     1391605    static inline pid_t task_tgid_vnr(struct task_struct *tsk) 
     1401606    { 
     1411607            return pid_vnr(task_tgid(tsk)); 
     1421608    } 
     1431609 
     1441610 
     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 
     158type = 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 
     175type = struct pid_link { 
     176    struct hlist_node node; 
     177    struct pid *pid; 
     178} [3] 
     179 
     180(snip) 
     181 
     182(gdb) s 
     1831607            return pid_vnr(task_tgid(tsk)); 
     184(gdb) 
     1851606    { 
     186(gdb) 
     1871607            return pid_vnr(task_tgid(tsk)); 
     188(gdb) 
     189pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:444 
     190444     { 
     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 
     204439             } 
     205440             return nr; 
     206441     } 
     207442 
     208443     pid_t pid_vnr(struct pid *pid) 
     209444     { 
     210445             return pid_nr_ns(pid, current->nsproxy->pid_ns); 
     211446     } 
     212447     EXPORT_SYMBOL_GPL(pid_vnr); 
     213448 
     214(gdb) ptype pid 
     215type = 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 
     2251607            return pid_vnr(task_tgid(tsk)); 
     226(gdb) ptype tsk->nsproxy->pid_ns 
     227type = 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 
     240444     { 
     241(gdb) s 
     242445             return pid_nr_ns(pid, current->nsproxy->pid_ns); 
     243(gdb) 
     244get_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 
     24614              return percpu_read_stable(current_task); 
     247(gdb) p current_task 
     248Cannot access memory at address 0xb540 
     249(gdb) s 
     250pid_vnr (pid=0xffff88001fb55400) at kernel/pid.c:445 
     251445             return pid_nr_ns(pid, current->nsproxy->pid_ns); 
     252(gdb) s 
     253444     { 
     254(gdb) 
     255445             return pid_nr_ns(pid, current->nsproxy->pid_ns); 
     256(gdb) 
     257pid_nr_ns (pid=0xffff88001fb55400, ns=0xffffffff8181bfe0) at kernel/pid.c:431 
     258431     { 
     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 
     274426             return pid; 
     275427     } 
     276428     EXPORT_SYMBOL_GPL(find_get_pid); 
     277429 
     278430     pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) 
     279431     { 
     280432             struct upid *upid; 
     281433             pid_t nr = 0; 
     282434 
     283435             if (pid && ns->level <= pid->level) { 
     284(gdb) l 
     285436                     upid = &pid->numbers[ns->level]; 
     286437                     if (upid->ns == ns) 
     287438                             nr = upid->nr; 
     288439             } 
     289440             return nr; 
     290441     } 
     291442 
     292443     pid_t pid_vnr(struct pid *pid) 
     293444     { 
     294445             return pid_nr_ns(pid, current->nsproxy->pid_ns); 
     295(gdb) ptype pid 
     296type = 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 
     304type = 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} * 
     315type = struct upid { 
     316    int nr; 
     317    struct pid_namespace *ns; 
     318    struct hlist_node pid_chain; 
     319} * 
     320(gdb) ptype pid_t 
     321type = 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 
     335upid = <value optimized out> 
     336nr = <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 
     342435             if (pid && ns->level <= pid->level) { 
     343(gdb) 
     344431     { 
     345(gdb) 
     346435             if (pid && ns->level <= pid->level) { 
     347(gdb) 
     348436                     upid = &pid->numbers[ns->level]; 
     349(gdb) 
     350437                     if (upid->ns == ns) 
     351(gdb) p upid 
     352$13 = (struct upid *) 0xffff88001fb55430 
     353(gdb) s 
     354438                             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 
     362441     } 
     363(gdb) 
     364pid_vnr (pid=<value optimized out>) at kernel/pid.c:446 
     365446     } 
     366(gdb) s 
     367task_tgid_vnr (tsk=<value optimized out>) at include/linux/sched.h:1608 
     3681608    } 
     369(gdb) 
     370sys_getpid () at kernel/timer.c:1346 
     3711346    } 
     372(gdb) 
     373sys_getpid () at kernel/timer.c:1345 
     3741345            return task_tgid_vnr(current); 
     375(gdb) 
     376sys_getpid () at kernel/timer.c:1346 
     3771346    } 
     378(gdb) 
     379}}} 
     380 
     381 * on VM guest 
     382  * s0711489@ubuntu-lucid64:~/coursework/KernelHack/02$ ./getpid 
     383{{{ 
     384getpid() -> 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{{{ 
     408static 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{{{ 
     417In 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{{{ 
     423static __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}}}