2013-04-20

daily debugging tips and toolbox





debugging.org



* debugging tips
strace::
http://www.thegeekstuff.com/2011/11/strace-examples/

where is the log files of specified process (e.g. nginx)?
@firstly, find your process's pid
ubuntu@ip:~ $ ps -ef | grep nginx
root     13622     1  0 Mar03 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data 13623 13622  0 Mar03 ?        00:03:19 nginx: worker process
www-data 13624 13622  0 Mar03 ?        00:00:00 nginx: worker process
www-data 13625 13622  0 Mar03 ?        00:03:19 nginx: worker process
www-data 13626 13622  0 Mar03 ?        00:03:19 nginx: worker process
ubuntu   19058 18961  0 10:31 pts/0    00:00:00 grep --color=auto nginx
@secondly, using lsof to find all handle of that "pid", then grep it
ubuntu@ip:~ $ sudo lsof -f -p 13622 | grep log
nginx   13622 root    2w   REG              202,1        0 145222 /var/log/nginx/error.log
nginx   13622 root    5w   REG              202,1        0 135398 /var/log/nginx/access.log
nginx   13622 root    6w   REG              202,1        0 145222 /var/log/nginx/error.log

which process is listen on port 80?
ubuntu@ip:~ $ sudo lsof -i:80
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   13622     root    7u  IPv4  27232      0t0  TCP *:http (LISTEN)
nginx   13623 www-data    7u  IPv4  27232      0t0  TCP *:http (LISTEN)
nginx   13624 www-data    7u  IPv4  27232      0t0  TCP *:http (LISTEN)
nginx   13625 www-data    7u  IPv4  27232      0t0  TCP *:http (LISTEN)
nginx   13626 www-data    7u  IPv4  27232      0t0  TCP *:http (LISTEN)

what's my process's environemnt variables?
[vagrant@vagrant-centos-6-64 ~] $ ps -ef | grep apache
apache    2136  1382  0 10:11 ?        00:00:00 /usr/sbin/httpd
[vagrant@vagrant-centos-6-64 ~] $ sudo cat /proc/1382/environ
TERM=linuxPATH=/sbin:/usr/sbin:/bin:/usr/binrunlevel=3RUNLEVEL=3LANGSH_SOURCED=1PWD=/LANG=Cprevious=NPREVLEVEL=NCONSOLETYPE=vtSHLVL=3UPSTART_INSTANCE=UPSTART_EVENTS=runlevelUPSTART_JOB=rc_=/usr/sbin/httpd

let mysql log the queries?
@execute following command in mysql console
SET GLOBAL log_output = 'FILE'; Set GLOBAL general_log_file = '/tmp/mysql.log'; SET GLOBAL general_log = 'ON';
@ or directly in bash
 $ mysql -uroot -pYOUR-PASSWORD -e "SET GLOBAL log_output = 'FILE'; Set GLOBAL general_log_file = '/tmp/mysql.log'; SET GLOBAL general_log = 'ON';"
@ then tail /tmp/mysql.log, see the quries against mysql
 $ tail -f /tmp/mysql.log

how to monitor http request/response of this machine?
@install justniffer on mac
 $ brew install boost
 $ brew install justniffer
@ Capture all tcp traffic
 $ sudo justniffer -i eth0 -r
        GET /null_main.c HTTP/1.1
        Host: localhost:9000
        Connection: keep-alive
        Cache-Control: max-age=0
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
        User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36
        Accept-Encoding: gzip,deflate,sdch
        Accept-Language: en-US,en;q=0.8
        If-Modified-Since: Sat, 20 Oct 2012 09:17:23 GMT
        
        HTTP/1.0 200 OK
        Server: SimpleHTTP/0.6 Python/2.7.1
        Date: Thu, 06 Jun 2013 13:27:41 GMT
        Content-type: text/plain
        Content-Length: 46
        Last-Modified: Sat, 20 Oct 2012 09:17:23 GMT
        
        /* null_main.c */
        
        int main() {
          return 0;
        }
@ Capture only http traffic
 $ sudo justniffer -i eth0 -r -p "port 80 or port 8080"
 
@Capture smtp traffic (usually using tcp port 25)
 $ sudo justniffer -i eth0 -r -p "port 25"
        220 plecno.com ESMTP Postfix (Ubuntu)
        
        EHLO unknown.localnet
        250-plecno.com       
        250-PIPELINING       
        250-SIZE             
        250-VRFY             
        250-ETRN             
        250-STARTTLS         
        250-ENHANCEDSTATUSCODES
        250-8BITMIME           
        250 DSN
        ...
        --Boundary-00=_ZI47J3FTNXn+25g
        Content-Type: text/html;
        charset="us-ascii"
        Content-Transfer-Encoding: 7bit
        
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
        "http://www.w3.org/TR/REC-html40/strict.dtd">
        <html>
        <head>
        ...

How to monitor changes in directory?
@run following command, and try to touch/rm files. the results of `watch` will changing.
 $ watch -d -n 0.1 "ls"


How to start a HTTP server on port 3000 for files in directory ~?
 $ cd ~
 $ python -m SimpleHTTPServer 3000

        

TO BE CONTINUE::::


2013-04-06

express-IOC

https://github.com/whunmr/express-ioc
https://github.com/johnelf/express-mvc
https://github.com/whunmr/express-orm



Framework design:
  • less intrusiveness
  • simple to use
  • Flent API: User.find_all().includes(Article.class);
  • Convention over configuration

DEMO CODE
git@github.com:whunmr/express-orm.git


IMPLEMENTATION of Lazy List


  • allOf
  • anyOf
  • both
  • either
  • describedAs
  • everyItem
  • is
  • anything
  • hasItem
  • hasItems
  • equalTo
  • instanceOf
  • any
  • not
  • nullValue
  • notNullValue
  • sameInstance
  • containsString
  • endsWith
  • startsWith
  • hasProperty
  • samePropertyValuesAs
  • array
  • hasItemInArray
  • arrayContainingInAnyOrder
  • arrayContaining
  • arrayWithSize
  • emptyArray
  • hasSize
  • empty
  • emptyIterable
  • isIn
  • isOneOf
  • containsInAnyOrder
  • contains
  • iterableWithSize
  • hasEntry
  • hasKey
  • hasValue
  • closeTo
  • comparesEqualTo
  • greaterThan
  • greaterThanOrEqualTo
  • lessThan
  • lessThanOrEqualTo
  • hasToString
  • typeCompatibleWith
  • eventFrom
  • isEmptyString
  • isEmptyOrNullString
  • equalToIgnoringCase
  • equalToIgnoringWhiteSpace
  • stringContainsInOrder
  • hasXPath
  • matcher1
  • matcher2
  • matcher3
  • firstMethod
  • secondMethod
  • notStatic
  • notPublic
  • goodMethod
  • anotherGoodMethod
  • wrongReturnType
  • generifiedType
  • noGenerifiedType
  • crazyType
  • withParam
  • withArray
  • withVarArgs
  • withGenerifiedParam
  • withExceptions
  • documented
  • subclassMethod
GUAVA
moco:

cglib introduction:

dynamic-proxy:
git@github.com:whunmr/java-demos.git

intelliJ IDEA
foreach:
  • iter (for each..in)
  • itin (for..in)
  • itli Iterate List itar Iterate array
  • ritar reverse order

WORKSHOP about dynamic-proxy/cglib/javassist
a. how to using instrument lib
  1. gradle clean idea
  2. in instrument folder, execute gradle jar
  3. setup configuration so we can using the instrument agent jar, when running junit test. In IntelliJ IDEA, Run-->Edit Configurations…-->Defaults-->JUnit: set "VM Options:" to -javaagent:./instrument/build/libs/instrument.jar


b. get the task from here https://github.com/whunmr/java-demos

c. find //TASK1 //TASK2 //TASK3, and finish the //TODOs in the task.


FUTHER READING:
Inside the Java Virtual Machine