Terminology of C++

Terminology list

  • argument
  • block
  • buffer
  • built-in type
  • cerr
  • cin
  • class
  • class type
  • clog
  • comment
  • condition
  • cout
  • curly brace
  • data structure
  • edit-compile-debug
  • end-of-file
  • expression
  • for statement
  • function
  • function body
  • function name
  • header
  • if statement
  • iostream
  • istream
  • library type
  • main function
  • manipulator
  • member function
  • method
  • namespace
  • ostream
  • parameter list
  • preprocessor directive
  • return type
  • source life
  • standard error
  • standard input
  • standard library
  • standard ouput
  • statement
  • std
  • string literal
  • uninitialzed variable
  • variable
  • while statement
  • () operator
  • ++ operator
  • += operator
  • . operator
  • :: operator
  • = operator
  • << operator
  • >> operator
  • == operator
  • != operator
  • <= operator
  • < operator
  • >= operator
  • > operator

Reference

debugging programs with gdb

Features

  • let program run by your order
  • set breakpoints and let program stop
  • check values when program stop
  • change environment of program

Basic

  1. add -g option to compile, e.g: gcc -g -Wall -o test test.c

  2. run

    gdb ./test   <--- start gdb debug
    l            <--- list source code
    break 5      <--- break at 5th line
    break fun1   <--- break at func1 (function name)
    info break   <--- show all break info
    r            <--- run ./test
    n            <--- execute next line
    c            <--- continue 
    p i          <--- print value of i
    bt           <--- backtrace of the stack (same as info stack)
    finish       <--- exit function
    q            <--- exit gdb
    

Advance

  1. modify program running environment

    (gdb) set args -f -i /tmp/rssp.pid  
    (gdb) show args  
    Argument list to give program being debugged when it is started is "-f -i /tmp/rssp.pid".  
    
  2. debug running programm

    find PID by command pgrep, then using `gdb <program> PID` start debugging  
    using command `gdb <program>`,then using `attach PID` to start debugging, `detach` to cancel.
    
  3. info command

    info address -- Describe where symbol SYM is stored  
    info args -- Argument variables of current stack frame  
    info bookmarks -- Status of user-settable bookmarks  
    info breakpoints -- Status of specified breakpoints (all user-settable breakpoints if no argument)  
    info checkpoints -- IDs of currently known checkpoints  
    info classes -- All Objective-C classes  
    info common -- Print out the values contained in a Fortran COMMON block  
    info files -- Names of targets and files being debugged  
    info float -- Print the status of the floating point unit  
    info frame -- All about selected stack frame  
    info functions -- All function names  
    info line -- Core addresses of the code for a source line  
    info locals -- Local variables of current stack frame  
    info macro -- Show the definition of MACRO  
    info macros -- Show the definitions of all macros at LINESPEC  
    info mem -- Memory region attributes  
    info os -- Show OS data ARG  
    info pretty-printer -- GDB command to list all registered pretty-printers  
    info proc -- Show /proc process information about any running process  
    info program -- Execution status of the program  
    info signals -- What debugger does when program gets various signals  
    info source -- Information about the current source file  
    info sources -- Source files in the program  
    info stack -- Backtrace of the stack  
    info symbol -- Describe what symbol is at location ADDR  
    info threads -- Display currently known threads  
    info tracepoints -- Status of specified tracepoints (all tracepoints if no argument)  
    info watchpoints -- Status of specified watchpoints (all watchpoints if no argument)  
    
  4. break command

    break file:linenumber  
    break file:function  
    break *address  
    break ... if <condition>  
    
    ofcourse, you can clear or disable or enable the breakpoint, 
    
    clear
    clear <function>
    clear <filename:function>
    clear <linenum>
    clear <filename:linenum>
    
    delete [breakpoints] [range...]
    
    disable [breakpoints] [range...]
    
    enable [breakpoints] [range...]
    
  5. watch

    watch <expr>
    rwatch <expr>
    awatch <expr>
    info watchpoints
    
  6. catch point
    catch , event list as below:

    1).throw
    2).catch
    3).exec
    4).fork
    5).vfork
    6).load
    7).unload
    
  7. condition

  8. signal

  9. thread

    - info threads 显示当前可调试的所有线程,每个线程会有一个GDB为其分配的ID,后面操  
                   作线程的时候会用到这个ID。 前面有*的是当前调试的线程。
    - thread ID 切换当前调试的线程为指定ID的线程。
    - break thread_test.c:123 thread all 在所有线程中相应的行上设置断点
    - thread apply ID1 ID2 command 让一个或者多个线程执行GDB命令command。 
    - thread apply all command 让所有被调试线程执行GDB命令command。
    - set scheduler-locking off|on|step   
      估计是实际使用过多线程调试的人都可以发现,在使用step或者continue命令调试当前被  
      调试线程的时候,其他线程也是同时执行的,怎么只让被调试程序执行呢?通过这个命令  
      就可以实现这个需求。
      + off 不锁定任何线程,也就是所有线程都执行,这是默认值。   
      + on只有当前被调试程序会执行。 
      + step 在单步的时候,除了next过一个函数的情况(熟悉情况的人可能知道,这其实是一  
        个设置断点然后continue的行为)以外,只有当前线程会执行。
    

example:

(gdb) r
Starting program: /root/thread
[New Thread 1073951360 (LWP 12900)]
[New Thread 1082342592 (LWP 12907)]---以下三个为新产生的线程
[New Thread 1090731072 (LWP 12908)]
[New Thread 1099119552 (LWP 12909)]

查看线程:使用info threads可以查看运行的线程。 

(gdb) info threads
  4 Thread 1099119552 (LWP 12940)   0xffffe002 in ?? ()
  3 Thread 1090731072 (LWP 12939)   0xffffe002 in ?? ()
  2 Thread 1082342592 (LWP 12938)   0xffffe002 in ?? ()
* 1 Thread 1073951360 (LWP 12931)   main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb)

注意,行首的蓝色文字为gdb分配的线程号,对线程进行切换时,使用该该号码,而不是
上文标出的绿色数字。

另外,行首的红色星号标识了当前活动的线程

切换线程:使用 thread THREADNUMBER 进行切换,THREADNUMBER 为上文提到的线程号。
下例显示将活动线程从 1 切换至 4。 

(gdb) info threads
   4 Thread 1099119552 (LWP 12940)   0xffffe002 in ?? ()
   3 Thread 1090731072 (LWP 12939)   0xffffe002 in ?? ()
   2 Thread 1082342592 (LWP 12938)   0xffffe002 in ?? ()
* 1 Thread 1073951360 (LWP 12931)   main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb) thread 4
[Switching to thread 4 (Thread 1099119552 (LWP 12940))]#0   0xffffe002 in ?? ()
(gdb) info threads
* 4 Thread 1099119552 (LWP 12940)   0xffffe002 in ?? ()
   3 Thread 1090731072 (LWP 12939)   0xffffe002 in ?? ()
   2 Thread 1082342592 (LWP 12938)   0xffffe002 in ?? ()
   1 Thread 1073951360 (LWP 12931)   main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb)

打印线程信息:

usage:
    break <linespec> thread <threadno>
    break <linespec> thread <threadno> if ...

example:
(gdb) break frik.c:13 thread 28 if bartab > lim

例子:

(gdb) r
Starting program: /home/dennis/work/test/test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77f5700 (LWP 5073)]
thr_fun_1: 9
[New Thread 0x7ffff6ff4700 (LWP 5074)]
thr_fun_2: 69
[New Thread 0x7ffff67f3700 (LWP 5075)]
 >> func_1
 >> func_2
 >> func_3
 >> func_4
 >> func_5
thr_fun_3: 99
thr_fun_1: 8
thr_fun_3: 98
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7bc566b in pthread_join (threadid=140737345705728, thread_return=0x0) at pthread_join.c:92
92    pthread_join.c: No such file or directory.
(gdb) break main.c:45 thread 4
Breakpoint 1 at 0x400849: file main.c, line 45.
(gdb) c
Continuing.
thr_fun_3: 97
thr_fun_1: 7
thr_fun_2: 68
[Switching to Thread 0x7ffff67f3700 (LWP 5075)]

Breakpoint 1, thr_fun_3 (parm=0x0) at main.c:45
45            sleep(1);
(gdb) l
40    void* thr_fun_3 (void *parm)
41    {
42        int count = 100;
43        while (count--) {
44            printf("%s: %d\n",__FUNCTION__, count);
45            sleep(1);
46        }
47        return NULL;
48    }
49    
(gdb) print count
$1 = 97
(gdb) 
  1. stack info

  2. memory of code

    info line test.c:func

    disassemble func

  3. view values of running data

    (gdb) p file::variable
    (gdb) p function::variable

(gdb) p *array@len
  1. view memory
    examine

  2. auto display
    display

  3. set print option

  4. history

  5. view register

    info registers
    info all-registers
    info registers

  6. change program value

    (gdb) print x=4
    (gdb) set var width=47

  7. jump

  1. run shell command

    (gdb) shell

  2. view all backtrace of all thread: thread apply all bt, short type t a a bt

  3. set print elements 1024, if want unlimited, use set print elements 0, use
    for print std::string contents. Type help set print elements for more

  4. x/20xb &arr, print 20 bytes start from address of variable arr, help with help x

  5. dump binary memory filename start_addr end_addr dump memory to files
    ref : https://sourceware.org/gdb/onlinedocs/gdb/Dump_002fRestore-Files.html

Reference

John Von Neumann and the Mathematician's Trap

From : John Von Neumann and the Mathematician’s Trap

The story goes that someone once posed to Von Neumann the following problem:

Two trains are 20 miles apart on the same track heading towards each other at 10
miles per hour, on a collision course. At the same time, a bee takes off from
the nose of one train at 20 miles per hour, towards the other train. As soon as
the bee reaches the other train, it bangs huwey and heads off at 20 miles per
hour back towards the first train. It continues to do this until the trains
collide, killing the bee.

The question is, how far does the bee fly (d) before the collision?

When posed with the above problem (or some variation of it), JVN took all of
five to ten seconds to come up with the correct solution. This floored the
questioner who said “I’m impressed that you didn’t fall for the Mathematician’s
Trap.” After getting a perplexed look from our genius, he asked “How did you
solve the problem?”

“By infinite series, of course!”

nginx

Target

资深Linux服务端开发工程师(C/C++)

工作职责
• 负责基础Web服务器软件设计与开发;
• 设计、优化Web平台与架构以满足日益增长的平台的性能要求;
• 直接参与到系统设计和核心代码开发;

职位要求
• 3年以上系统软件开发工作经验;
• 精通C/C++、HTTP协议;
• 精通高性能网络服务器编程,如异步、事件驱动;
• 具有良好的沟通能力,有较强的独立工作能力和解决问题的能力;
• 有4/7层负载均衡软件开发经验者优先;
• 熟悉开源的Web服务器(如Nginx、Apache、Lighttpd等),有分析其代码实现或模块编写经验者优先;
• 具有深厚算法及数据结构知识优先;
• 有大规模Web架构系统设计经验者优先;
• 开源社区成员优先;

Reference

how to do git push under vpn

Analysis

  1. Generally, when using ssh -vT git@github.com, it would show as below:

    [dennis@localhost ~]$ ssh -vT git@github.com
    OpenSSH_6.1p1, OpenSSL 1.0.1e-fips 11 Feb 2013
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 50: Applying options for *
    debug1: Connecting to github.com [192.30.252.129] port 22.
    debug1: Connection established.
    debug1: identity file /home/dennis/.ssh/id_rsa type 1
    debug1: identity file /home/dennis/.ssh/id_rsa-cert type -1
    debug1: identity file /home/dennis/.ssh/id_dsa type -1
    debug1: identity file /home/dennis/.ssh/id_dsa-cert type -1
    debug1: identity file /home/dennis/.ssh/id_ecdsa type -1
    debug1: identity file /home/dennis/.ssh/id_ecdsa-cert type -1
    debug1: Remote protocol version 2.0, remote software version libssh-0.6.0
    debug1: no match: libssh-0.6.0
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_6.1
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    debug1: kex: server->client aes128-ctr hmac-sha1 none
    debug1: kex: client->server aes128-ctr hmac-sha1 none
    debug1: sending SSH2_MSG_KEX_ECDH_INIT
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug1: Server host key: RSA xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
    debug1: Host 'github.com' is known and matches the RSA host key.
    debug1: Found key in /home/dennis/.ssh/known_hosts:1
    debug1: ssh_rsa_verify: signature correct
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug1: SSH2_MSG_NEWKEYS received
    debug1: Roaming not allowed by server
    debug1: SSH2_MSG_SERVICE_REQUEST sent
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug1: Authentications that can continue: publickey
    debug1: Next authentication method: publickey
    debug1: Offering RSA public key: /home/dennis/.ssh/id_rsa
    debug1: Server accepts key: pkalg ssh-rsa blen 279
    debug1: Authentication succeeded (publickey).
    Authenticated to github.com ([192.30.252.129]:22).
    debug1: channel 0: new [client-session]
    debug1: Entering interactive session.
    debug1: Sending environment.
    debug1: Sending env LC_MONETARY = en_HK.utf8
    debug1: Sending env LC_NUMERIC = en_HK.utf8
    debug1: Sending env XMODIFIERS = @im=ibus
    debug1: Sending env LANG = en_GB.utf8
    debug1: Sending env LC_MEASUREMENT = en_HK.utf8
    debug1: Sending env LC_TIME = en_HK.utf8
    Hi matrix207! You've successfully authenticated, but GitHub does not provide shell access.
    debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
    debug1: channel 0: free: client-session, nchannels 1
    Transferred: sent 2832, received 1816 bytes, in 1.5 seconds
    Bytes per second: sent 1950.6, received 1250.8
    debug1: Exit status 1
    [dennis@localhost ~]$ 
    
  2. But when using VPN, it show as below:

    [dennis@localhost ~]$ ssh -vT git@github.com
    OpenSSH_6.1p1, OpenSSL 1.0.1e-fips 11 Feb 2013
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 50: Applying options for *
    debug1: Connecting to github.com [192.30.252.128] port 22.
    debug1: connect to address 192.30.252.128 port 22: No route to host
    ssh: connect to host github.com port 22: No route to host
    
  3. Reason of the problem is that, your vpn setup routing all your traffic over
    the VPN, you can update your routing table to route traffic to github back
    over your ethernet interface rather than the VPN

  4. Not run VPN

    [dennis@localhost ~]$ route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
    192.168.1.0     *               255.255.255.0   U     9      0        0 wlan0
    192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0
    [dennis@localhost ~]$ 
    
    [dennis@localhost ~]$ ifconfig
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 9114  bytes 4451106 (4.2 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 9114  bytes 4451106 (4.2 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    p1p2: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            ether XX:XX:XX:XX:XX:XX  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 52:54:00:f3:fa:32  txqueuelen 0  (Ethernet)
            RX packets 118  bytes 19982 (19.5 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 30  bytes 4505 (4.3 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet6 fe80::fc54:ff:fedb:760f  prefixlen 64  scopeid 0x20<link>
            ether fe:54:00:db:76:0f  txqueuelen 500  (Ethernet)
            RX packets 118  bytes 21646 (21.1 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 6974  bytes 365825 (357.2 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::c617:feff:fec3:892d  prefixlen 64  scopeid 0x20<link>
            ether XX:XX:XX:XX:XX:XX  txqueuelen 1000  (Ethernet)
            RX packets 39375  bytes 25884803 (24.6 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 46628  bytes 7804313 (7.4 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
  5. Using VPN

    [dennis@localhost ~]$ route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         *               0.0.0.0         U     0      0        0 ppp0
    10.0.4.4        *               255.255.255.255 UH    0      0        0 ppp0
    192.168.1.0     *               255.255.255.0   U     9      0        0 wlan0
    192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0
    199.119.206.177 192.168.1.1     255.255.255.255 UGH   0      0        0 wlan0
    199.119.206.177 192.168.1.1     255.255.255.255 UGH   0      0        0 wlan0
    
    [dennis@localhost ~]$ ifconfig
    ...
    ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1400
            inet 10.0.4.123  netmask 255.255.255.255  destination 10.0.4.4
            ppp  txqueuelen 3  (Point-to-Point Protocol)
            RX packets 9  bytes 114 (114.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 11  bytes 176 (176.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    ...
    
  6. get ip from ping github

    [dennis@localhost ~]$ ping github.com
    PING github.com (192.30.252.129) 56(84) bytes of data.
    
  7. routing tables????(NOTICE: this section is unfinished.)

    [dennis@localhost ~]$ su -c 'route add 192.30.252.129 wlan0'
    Password: 
    [dennis@localhost ~]$ route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         *               0.0.0.0         U     0      0        0 ppp0
    10.0.4.4        *               255.255.255.255 UH    0      0        0 ppp0
    ip1d-lb3-prd.ia *               255.255.255.255 UH    0      0        0 wlan0
    192.168.1.0     *               255.255.255.0   U     9      0        0 wlan0
    192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0
    199.119.206.177 192.168.1.1     255.255.255.255 UGH   0      0        0 wlan0
    199.119.206.177 192.168.1.1     255.255.255.255 UGH   0      0        0 wlan0
    
    [dennis@localhost ~]$ su -c 'route add  192.30.252.129  gw 192.168.1.1'
    
  8. CONTINUE

Reference