ETW test

Created a test php page which simply sleeps for a specified seconds, and called it from Windows with ETW turned on.

On Mac: test.php

<?php
$delay = $_GET["delay"];
sleep($delay);
echo date('h:i:s') . "<br/>\n";
?>

On Windows:

start:
xperf.exe -on PROC_THREAD+LOADER+Base+CSWITCH+DISPATCHER -stackwalk Profile+CSwitch+ReadyThread

run the test:
Open Powershell Window and run -
 $wc = New-Object System.Net.WebClient
 $wc.DownloadString("http://hogeaddress:hogeport/hogedirectory/test.php?delay=5")

stop:
xperf.exe -stop -d %temp%\hoge.etl

see the trace:
xperf.exe %temp%\hoge.etl

It traced in kernel mode when the user mode thread waited on the sync object.
ETW

My first gdb

Wanted to use gdb since I’m always using Windows debuggers at work. Good thing is that now MacOS runs on Intel CPU and I don’t need to look into X68000 or PowerPC.

Here is today’s test target.

 #include <stdio.h>
 
 bool hogecheck();
 void page();
 
 bool hogecheck(){
     return false;
 }         
 
 void page(){
     printf("page");
 }
 
 int main(int args, char* argv[])
 {   
     int i;
     if(false == hogecheck()){
         return 1;
     }
     page();
     return 0;
 }

And here is how I changed the result of ‘hogecheck’ and run ‘page’ function.

(gdb) b main
Breakpoint 1 at 0x100000e7f: file first.c, line 17.
(gdb) g
Undefined command: "g".  Try "help".
(gdb) r
...
(gdb) set disassembly-flavor intel
(gdb) disas
Dump of assembler code for function main:
0x0000000100000e70 <main+0>:	push   rbp
0x0000000100000e71 <main+1>:	mov    rbp,rsp
0x0000000100000e74 <main+4>:	sub    rsp,0x20
0x0000000100000e78 <main+8>:	mov    DWORD PTR [rbp-0x4],edi
0x0000000100000e7b <main+11>:	mov    QWORD PTR [rbp-0x10],rsi
0x0000000100000e7f <main+15>:	call   0x100000e30 <_Z9hogecheckv>
0x0000000100000e84 <main+20>:	mov    cl,al
0x0000000100000e86 <main+22>:	cmp    cl,0x0
0x0000000100000e89 <main+25>:	setne  cl
0x0000000100000e8c <main+28>:	xor    cl,0x1
0x0000000100000e8f <main+31>:	and    cl,0x1
0x0000000100000e92 <main+34>:	mov    BYTE PTR [rbp-0x19],cl
0x0000000100000e95 <main+37>:	mov    cl,BYTE PTR [rbp-0x19]
0x0000000100000e98 <main+40>:	cmp    cl,0x0
0x0000000100000e9b <main+43>:	je     0x100000ea6 <main+54>
0x0000000100000e9d <main+45>:	mov    DWORD PTR [rbp-0x18],0x1
0x0000000100000ea4 <main+52>:	jmp    0x100000eb2 <main+66>
0x0000000100000ea6 <main+54>:	call   0x100000e50 <_Z4pagev>
0x0000000100000eab <main+59>:	mov    DWORD PTR [rbp-0x18],0x0
0x0000000100000eb2 <main+66>:	mov    eax,DWORD PTR [rbp-0x18]
0x0000000100000eb5 <main+69>:	mov    DWORD PTR [rbp-0x14],eax
0x0000000100000eb8 <main+72>:	mov    eax,DWORD PTR [rbp-0x14]
0x0000000100000ebb <main+75>:	add    rsp,0x20
0x0000000100000ebf <main+79>:	pop    rbp
0x0000000100000ec0 <main+80>:	ret    

(gdb) info r
rax            0x100000e70	4294970992
rbx            0x0	0
...
rip            0x100000e7f	0x100000e7f <main+15> <-- Stopped one step before

(gdb) n

Breakpoint 2, 0x0000000100000e84 in main (args=1, argv=0x7fff5fbff9e0) at first.c:17
17		if(false == hogecheck()){

(gdb) info r
rax            0x0	0
rbx            0x0	0
...
rip            0x100000e84	0x100000e84 <main+20>

(gdb) set $rax=1
(gdb) i r 
rax            0x1	1 <-- set
rbx            0x0	0

(gdb) c
Continuing.
page <-- displayed
Program exited normally.

Windbg version is here.

0:000> X first!*main*
00000000`00d03014 first!__native_dllmain_reason = 0xffffffff
00000000`00d03018 first!mainret = 0n0
00000000`00d01000 first!wmain (int, wchar_t **)
00000000`00d01119 first!__tmainCRTStartup (void)
00000000`00d0127e first!wmainCRTStartup (void)
00000000`00d02084 first!_imp____wgetmainargs = <no type information>
0:000> bp first!wmain
0:000> bl
 0 e x86 00000000`01131470     0001 (0001)  0:**** first!wmain
0:000:x86> g
Breakpoint 0 hit
first!wmain:
01131470 55              push    ebp
0:000:x86> u eip eip+100
first!wmain [c:\users\sokoide\projects\spike\first\first.cpp @ 19]:
01131470 55              push    ebp
01131471 8bec            mov     ebp,esp
01131473 81eccc000000    sub     esp,0CCh
01131479 53              push    ebx
0113147a 56              push    esi
0113147b 57              push    edi
0113147c 8dbd34ffffff    lea     edi,[ebp-0CCh]
01131482 b933000000      mov     ecx,33h
01131487 b8cccccccc      mov     eax,0CCCCCCCCh
0113148c f3ab            rep stos dword ptr es:[edi]
0113148e e881fbffff      call    first!ILT+15(?hogecheckYA_NXZ) (01131014)
01131493 0fb6c0          movzx   eax,al
01131496 85c0            test    eax,eax
01131498 7507            jne     first!wmain+0x31 (011314a1)
0113149a b801000000      mov     eax,1
0113149f eb07            jmp     first!wmain+0x38 (011314a8)
011314a1 e8e1fbffff      call    first!ILT+130(?pageYAXXZ) (01131087)
011314a6 33c0            xor     eax,eax
011314a8 5f              pop     edi
011314a9 5e              pop     esi
011314aa 5b              pop     ebx
011314ab 81c4cc000000    add     esp,0CCh
011314b1 3bec            cmp     ebp,esp
011314b3 e897fcffff      call    first!ILT+330(__RTC_CheckEsp) (0113114f)
011314b8 8be5            mov     esp,ebp
011314ba 5d              pop     ebp
011314bb c3              ret
011314bc cc              int     3
0:000:x86> bp 01131496
0:000:x86> g
Breakpoint 1 hit
first!wmain+0x26:
01131496 85c0            test    eax,eax
0:000:x86> r @eax=1
0:000:x86> r
eax=00000001 ebx=7f5fe000 ecx=00000000 edx=00000001 esi=00000000 edi=00b1fa4c
eip=01131496 esp=00b1f974 ebp=00b1fa4c iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
first!wmain+0x26:
01131496 85c0            test    eax,eax
0:000:x86> g

-> 'page' displayed in the console.

My uncrustify config

Diff from the default config for c++. Good parameter definition.

> input_tab_size                           = 4        # number
> output_tab_size                          = 4        # number
> indent_columns                           = 4        # number
> indent_with_tabs                         = 0        # number
> indent_class                             = true     # false/true
> indent_class_colon                       = true     # false/true
> nl_fdef_brace                            = force    # ignore/add/remove/force

before:

#include "global.h"

#ifndef __c2dxspike__Stone__
#define __c2dxspike__Stone__
#include "HelloWorldScene.h"
#include <iostream>

typedef enum tagState {
kStateGrabbed,
kStateUngrabbed
} touchState;

class Stone : public CCNode, CCTargetedTouchDelegate {
public:
Stone();
virtual ~Stone();
...

CCRect Stone::setStoneColor(int stoneType)
{
this->stoneType = stoneType;
CCRect color;

switch (this->stoneType) {
case 0:
color = CCRectMake(68, 2, 32, 32);
break;

case 1:
color = CCRectMake(132, 2, 32, 32);
break;

case 2:
color = CCRectMake(2, 2, 32, 32);
break;

case 3:
color = CCRectMake(100, 2, 32, 32);
break;
}

return color;
}

after uncrustify *.cpp *.h –no-backup:

#include "global.h"

#ifndef __c2dxspike__Stone__
#define __c2dxspike__Stone__
#include "HelloWorldScene.h"
#include <iostream>

typedef enum tagState {
    kStateGrabbed,
    kStateUngrabbed
} touchState;

class Stone : public CCNode, CCTargetedTouchDelegate {
public:
    Stone();
    virtual ~Stone();
...

CCRect Stone::setStoneColor(int stoneType)
{
    this->stoneType = stoneType;
    CCRect color;

    switch (this->stoneType) {
    case 0:
        color = CCRectMake(68, 2, 32, 32);
        break;

    case 1:
        color = CCRectMake(132, 2, 32, 32);
        break;

    case 2:
        color = CCRectMake(2, 2, 32, 32);
        break;

    case 3:
        color = CCRectMake(100, 2, 32, 32);
        break;
    }

    return color;
}

Cocos2d-x multiple resolution support

Found a nice article regarding multiple resolution support.
Since Cocos2d-x is cross platform, it’ll run on various Android devices.

After trying 4 patterns; aspect ration 1:1.5 x 2 patterns, 1:1.33 x 2 patterns, I noticed that it’s a pain to support different aspect ratios. I ended up having only 2 patterns to support low and high resolution below.

--- Uitl.h ---
class Util {
public:
    static void SetupContentScaleFactor();
};

--- Uitl.cpp ---
typedef struct tagResource
{
    cocos2d::CCSize size;
    char directory[64];
} Resource;

// 480 x 320 / aspect ratio 1.5 (iPhone/iPod touch) should use this
static const Resource resource15s =  { cocos2d::CCSizeMake(480, 320),   "15s" };

// 960 x 480 / aspect ratio 1.5 (iPhone/iPod touch retina) should use this
// 1024 x 600 / 1.7 (Kindle Fire) should use this
// 1024 x 768 / 1.33 (iPad), 800 x 600 (android)
// 1280 x 800 / 1.6 (Nexus 7) should use this
// 2048 x 1536 / 1.33 (iPad retina) should use this
// 2560 x 1600 / 1.6 (Nexus 10)
static const Resource resource15m =  { cocos2d::CCSizeMake(960, 640),   "15m" };

static const cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480, 320);

void Util::SetupContentScaleFactor() {
    CCDirector *pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionShowAll);
    
    CCSize frameSize = pEGLView->getFrameSize();
    CCLog("designResolutionSize: (%lf, %lf)", designResolutionSize.width, designResolutionSize.height);
    CCLog("frameSize: (%lf, %lf)", frameSize.width, frameSize.height);
    float scaleFactor = 1.0f;
    Resource resource;
    
    if ((frameSize.height == 640 && frameSize.width == 960) ||
        (frameSize.height == 640 && frameSize.width == 1136)) {
        // iPhone/iPod touch retina
        resource = resource15m;
    }
    else if (frameSize.height > resource15s.size.height)
    {
        resource = resource15m;
    }
    else
    {
        resource = resource15s;
    }
    
    CCFileUtils::sharedFileUtils()->setResourceDirectory(resource.directory);
    scaleFactor = resource.size.height/designResolutionSize.height;
    pDirector->setContentScaleFactor(scaleFactor);
    CCLog("using '%s' resource, scalefactor:%lf", resource.directory, scaleFactor);
}

Cocos2d-x Xcode config for iOS / Andriod

    1. Install Xcode, Android SDK/NDK
    2. Craete cocos 2d-x project in Xcode
    3. Update NDK_ROOT_LOCAL and ANDROID_SDK_ROOT in create-android-project.sh
    4. Run create-android-project.sh
    5. Copy generated proj.android folder into the iOS project root
    6. Copy platform/android/java/src/org/cocos2dx/lib/* into proj.android/src/org/cocos2dx/lib/*
    7. Update COCOS2DX_ROOT in build_native.sh
    8. Use Eclipse, or create a new target for Android in the Xcode project … I created a new target as ‘external build system’ which calls build_android_ver.sh.
#!/bin/sh

function test_ret_code(){
▸   ret=$?
▸   if [ $ret -ne 0 ]; then
▸   ▸   echo "error: $1 failed, ret:$ret"
▸   ▸   exit 1
▸   fi
}

export  NDK_ROOT="/Users/sokoide/android-ndk-r8b"
echo "*** custom build started"
/Users/sokoide/Projects/iPhone/Spike/c2dxspike/c2dxspike/proj.android/build_native.sh
test_ret_code "build_native.sh"
/usr/bin/ant -f /Users/sokoide/Projects/iPhone/Spike/c2dxspike/c2dxspike/proj.android/build.xml debug install
test_ret_code "install"
/Users/sokoide/android-sdks/platform-tools/adb shell am start -n com.sokoide.c2dx/.spike
test_ret_code "adb shell am start"

echo "*** custom build completed"
exit 0

cocos2dx on iOS and Android Test app on iOS (480×320) and Android (800×480). I scaled the background, but didn’t for the label on top.

X-code shortcuts

From here

Navigator

  • Command + 0: Show / Hide
  • Command + 1-7: Switch Navigators

Assistant Editor

  • Command + Option + Return: Show (split)
  • Command + Return: Hide

Utilities

  • Command + 0: Show / Hide
  • Command + 1-7: Switch Utilities

Others

  • Command + Shift + Y: Show/Hide Debug View
  • Command + Shift + O: Quick Open
  • Command + Shift + J: Show current file in Navigator
  • Command + Control + J: Jump to definition
  • Command + Control + Lef/Right: Backward/Forward
  • Command + Control + Up/Down: Switch .h/.m
  • Redis and Python

    Redis test memo in Python.

    installation:

    sudo port install redis
    sudo port intall py-redis
    mkdir ~/.redis
    touch ~/.redis.conf
    

    start redis server:

    redis-server
    

    test:

    insert:
    python redistest.py insert
    
    read:
    python redistest.py read
    page0
    page1
    page2
    page3
    page4
    page5
    page6
    page7
    page8
    page9
    
    pub:
    python redistest.py publish 'hello, hoge'
    python redistest.py publish 'bye, page'
    
    sub:
    python redistest.py subscribe
    {'pattern': None, 'type': 'subscribe', 'channel': '2channel', 'data': 1L}
    {'pattern': None, 'type': 'message', 'channel': '2channel', 'data': 'hello, hoge'}
    {'pattern': None, 'type': 'message', 'channel': '2channel', 'data': 'bye, page'}
    

    test script:

    import os
    import sys
    import redis
    
    channel='2channel'
    
    def connect():
    	# use default port, db index=0
    	r = redis.Redis(host='localhost', port=6379, db=0)
    	return r
    
    
    def data_delete():
    	r = connect()
    	# delete all data in all databases on the current host
    	r.flushall()
    
    def data_insert():
    	r = connect()
    	for i in range(0,10):
    		r.set('hoge%d' % i,'page%d' % i)
    
    
    def data_read():
    	r = connect()
    	for i in range(0,10):
    		print r.get('hoge%d' % i)
    
    def data_publish(message):
    	global channel
    	r = connect()
    	r.publish(channel, message)
    
    def data_subscribe():
    	global channel
    	r = connect()
    	ps = r.pubsub()
    	ps.subscribe(channel)
    	for message in ps.listen():
    		print message
    
    
    
    def main():
    	functions = {
    			"delete" : (lambda x:data_delete()),
    			"insert" : (lambda x:data_insert()),
    			"read" : (lambda x:data_read()),
    			"publish" : (lambda x:data_publish(x)),
    			"subscribe" : (lambda x:data_subscribe()),
    			}
    
    	param1 = sys.argv[2] if len(sys.argv)>2 else None
    	functions[sys.argv[1]](param1)
    
    main()
    

    To improve Time Warner cable Internet network stability

    I had no choice but to user Time Warner Internet in the old apartment. After some other initial troubles, finally I got the expected speed for the ‘extreme’ plan which is 30Mbps/5Mbps for download/upload. The slow speed is acceptable, but it had another annoying problem. The connection was intermittently closed.

    Yesterday, I finally found that the problem was in NAT feature on the cable modem UBee. I disabled it and changed it to ‘bridge’ mode which resolved the problem. But the problem is, the modem is locked by Time Warner.

    Here is how to get around it.

    Bridging the UBee DDW3611 Without Permission

    1. Factory reset to enable a regular user ‘user’
    2. Logon to the modem at a hidden URL http://192.168.0.1/TlModeChange.asp as user/password = user/user. The ‘user’ user is a non admin user, but once you get the URL and go, you can update the modem. The last 8digit of MAC -> Username, c0nf1gur3m3 -> Password didn’t work for me.
    3. Change it from NAT to Bridge mode
    4. Disable unstable Wifi if you want
    5. Reboot the cable modem
    6. The management URL will be changed to http://192.168.100.1/TlModeChange.asp in the Bridge mode

    When you forget SQL server password

    When you don’t remember MSFT SQL server’s sa’s password, here is how to reset it without uninstall/installing it.
    link

    Visual Studio Installer installed SQLExpress onto my box, but I was not in sa. Here is how to run SQL serer (or express) in console mode and add yourself into sa.

    sqlservr.exe -m -c -sSQLExpress
    sqlcmd.exe -S.\SQLEXPRESS -E
    sp_addsrvrolemember @loginame=[yourdomain\yourid], @rolename='sysadmin'
    

    C++ 11 summary video by Embarcadero Japan

    Watched C++11 1/2 and C++11 2/2, and tried some of them. It was quick & nice summary.

    void Hoge::LambdaTest()
    {
        int v1=42;
        auto f1 = [v1] { return v1; };  // capture value
        auto f2 = [&v1] { return v1; }; // capture reference
        auto f3 = [=] { return v1; };   // capture all values
        
        v1 = 99;
        auto f1result = f1();
        auto f2result = f2();
        
        cout << "f1: " << f1result << endl;
        cout << "f2: " << f2result << endl;
        cout << "f3: " << f3() << endl;
    }
    
    void Hoge::PointerTest(){
        auto sp1 = make_shared<int>(42);    //shared_ptr<int> sp1(new int(42));
        auto sp2 = sp1;
    
        if (sp1) { cout << "sp1: " << *sp1 << endl; }
        if (sp2) { cout << "sp2: " << *sp2 << endl; }
        
        unique_ptr<string> up1(new string("hoge"));
        // unique_ptr<string> up2 = up1;
        unique_ptr<string> up2(up1.release());
        
        if (up1) { cout << "up1: " << *up1 << endl; } // up1 alread released
        if (up2) { cout << "up2: " << *up2 << endl; }
    }
    
    
    void Hoge::InitTest(){
        //int *p1 = new int[10];      // new only
        //int *p2 = new int[10]();    // new + clear
        
        const int elements = 10;
        string* p3 = new string[elements]{"ab", "cd", "ef"};
        int* p4 = new int[elements]{1,2};
        
        cout << "string:" << endl;
        for (int i=0; i<elements; i++){
            cout << i << "[string] :" << p3[i] << endl;
            cout << i << "[int] :" << p4[i] << endl;
                }
    }
    
    void Hoge::FuncTest(){
        function<int(int, int)> f = [](int i, int j){ return i*j;};
        cout << f(1,2) << endl;
        cout << f(3,4) << endl;
    }
    
    void Hoge::TupleTest(){
        tuple<string, double> book1("B1 123-456", 42.0);    // constructor
        tuple<string, double> book2{"B2 123-456", 42.1};    // initializer
        auto book3 = make_tuple("B3 123-456", 42.2);        // returns <const char*, double>
        auto book4 = make_tuple(string("B4 123-456"), 42.3);// returns <string, double>
        
        cout << "book1: " << get<0>(book1) << ", " << get<1>(book1) << endl;
        cout << "book2: " << get<0>(book2) << ", " << get<1>(book2) << endl;
        cout << "book3: " << get<0>(book3) << ", " << get<1>(book3) << endl;
        cout << "book4: " << get<0>(book4) << ", " << get<1>(book4) << endl;
    }
    
    void Hoge::RandomTest(){
        // unsigned int
        default_random_engine e1((unsigned int)time(0));  // unsigned int
        for(int i=0; i<10; i++) {
            cout << "e1:" << e1() << endl;
        }
        
        uniform_int_distribution<unsigned long> u(0, 9);
        for(int i=0; i<10; i++) {
            cout << "u(e1):" << u(e1) << endl;
        }
        
        // 64bit
        mt19937_64 e2(time(0));  // unsigned long long (64bit)
        for(int i=0; i<10; i++) {
            cout << "e2:" << e2() << endl;
        }
    }
    
    1 3 4 5 6 7 8