Chitika

Tuesday, June 25, 2013

GDB debugging

Following are the useful commands to debug:

debug with program arguments:

    gdb --args prog arg1 arg2 arg3 

Debugging an application with complex configuration files setup in different directories:

    compile the program with -g switch, and install it
    debug the installed program as follows:
    sudo gdb --args prog arg1 arg2 arg3

add break point to function:

    b func_foo

add break point to line in file:

    b file:line_num

step into:

    s

step over:

    n

continue program execution:

    c

start/restart program:

    r

print variable:

    p var
    x/s str_var

print 10 bytes in hex

    x/10x str_var

list program statements being in debug
  
    l

get call stack: (backtrace)
 
    bt

get value of some variable in function call in back in call stack at row 2:

    f 2
    p var

get all break points (info break):

    i b

delete a break point 3:

    d 3

watch a variable for change:

    wa var

get threads list (info threads):

    i th

change to different thread context (e.g. thread 2):

    t 2

attaching to an already running process for debugging (first get process id using ps -ef):

    at process_id

detaching to debugging process:

    det

debugging save state (checkpoint, only for single threaded application):

   checkpoint

   info checkpoint

restore program state to the checkpoint (e.g. 2):

   restart 2

GDB hex dump

Create file in home folder, named '.gdbinit', and put following contents in it:

define xxd
dump binary memory dump.bin $arg0 $arg0+$arg1
shell xxd dump.bin
end


during debugging execute following command to get hex dump of any memory area, by specifying starting address (or name of variable) and length:

(gdb) xxd var 112
0000000: aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000010:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000020:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000030:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000040:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000050:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000060:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................



Ref:

http://stackoverflow.com/questions/9233095/memory-dump-formatted-like-xxd-from-gdb
http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_chapter/gdb_18.html#SEC195

Thursday, May 16, 2013

Sets Difference algorithm

Lets assume you have two sets as follows:

example sets:

Set A

1
2
3
4

Set B

3
4
5
6

now we want to get difference of these sets:

(assume a cleared (0) flag "added" for each element of set B)

algorithm:

1. for each element E in set B, iterate over elements in set A
    i. if value of element E is found in set A, remove that entry from A
   ii. if value of element E is not found in set A, set (1) "added" flag of element E
4. Remaining elements in set A, represents set difference A-B
5. elements in set B, having flag "added" set (1), represents set difference B-A
6. elements in set B, having flag "added" cleared(0), represents set intersections
7. All remaining elements in set A and value of all elements in set B represents union of set A and B

result:

Set A

1
2

Set B

3
4
5 -> added
6 -> added

A-B = 1,2
B-A = 5,6

A intersection B = 3,4
A union B = 1,2,3,4,5,6

optimizations:

if set A is sorted, iterated over elements of set A, until element value in set A is less than element E of set B
similarly, further optimizations are possible when both sets are sorted.

Monday, May 13, 2013

perl cpan install module


> perl -MCPAN -e shell
> install module_name

e.g.:
> install Net::DNS::Check

Monday, May 6, 2013

update perl modules to latest versions

Ubuntu environment:

Execute:

> sudo apt-get install cpanminus

> sudo cpanm App::cpanoutdated

> sudo cpan-outdated -p | cpanm

Update single module from CPAN: download, build and install

cpanm Net::DNS
cpanm LWP

Wednesday, February 13, 2013

linux enable sftp

To enable sftp on linux (having openssh-server installed), one have to uncomment/open following line in /etc/ssh/sshd_config file, and restart sshd server (this line is already enabled in all the distributions by default, means sftp is already working):

Subsystem       sftp    /usr/lib/ssh/sftp-server


To connect to linux sftp, use sftp user@server from command line and use normal ftp (put/get/mput/mget) commands to retrieve and send files to server.

Tuesday, February 12, 2013

C# Cross thread GUI update issue

Following is the pattern to handle C# cross thread GUI update issue (ref: stackoverflow)


delegate void SetTextCallback(string text);

private void SetText(string text)
{
  // InvokeRequired required compares the thread ID of the
  // calling thread to the thread ID of the creating thread.
  // If these threads are different, it returns true.
  if (this.textBox1.InvokeRequired)
  { 
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object[] { text });
  }
  else
  {
    this.textBox1.Text = text;
  }
}

Monday, February 11, 2013

debug c++ activeX control of different project

Sometimes solutions are big, consisting of different project. When we have to debug these projects we get the notification that: Breakpoint will not currently be hit. No symbol loaded for this document.

To debug such project, following are the steps:

  1. Clean solution
  2. Rebuild solution
  3. Run the executable project (by setting it as startup project)
  4. When the said activeX just launched by the executable, go to debug and attach to the executable containing the activeX
  5. now, easily debug the activeX project.

Thursday, February 7, 2013

JNI NDK for android developers (and casual c programmer)

  1. Download NDK
  2. extract NDK on c drive (no space in path)
  3. create jni folder in eclipse android project, put c file(s), make file and libraries (.a/.o) in this folder
  4. c file should be as follows (lowlevelfunctions.c), note signature of function:
#include <string.h>
#include <jni.h>
#include "thirdpartylib.h"


jstring Java_com_example_LowLevelFunctions_getInfo(JNIEnv* env, jobject javaThis) {

return (*env)->NewStringUTF(env, "hello world");
}

/************************************
*** and make file as follows (Android.mk): ***
************************************/


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := lowlevelfunctions
LOCAL_MODULE_TAGS := lowlevelfunctions
LOCAL_SRC_FILES := lowlevelfunctions.c
LOCAL_ARM_MODE := arm

LOCAL_LDFLAGS = $(LOCAL_PATH)/thirdpartylib.a
#LOCAL_STATIC_LIBRARIES := thirdpartylib

include $(BUILD_SHARED_LIBRARY)

5.  on cmd, navigate to jni folder
6. on the jni folder, execute ndk-build executable directly from NDK root path
7. liblowlevelfunctions.so will be created to the parent folder's lib folder
8. to include it in the java, note the function name in c file, it should be added in the class LowLevelFunctions of com.example package, named getInfo, as follows:

package com.example;

class LowLevelFunctions {

static {
System.loadLibrary("lowlevelfunctions"); // as defined in make file LOCAL_MODULE
}

public static native String getInfo();

}

9. If you want to create executable, include int main() function in c file and include $(BUILD_EXECUTABLE) in last line of make file.




Wednesday, February 6, 2013

Editing dot net resource .resx file

.Net Resource Editors are third party programs/freewares, which could be used to edit resource files (having extension .resx).

Following is similar utility:

http://fishcodelib.com/files/ResourceNet2.zip

C# get fully qualified domain name

Use following code snippet to get fully qualified domain name of current machine:



string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn = "";

if (!hostName.Contains(domainName))
    fqdn = hostName + "." + domainName;
else
    fqdn = hostName;

C# Auto detect default IP Address


Use following code snippet to automatically detect the default IP address:



  IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                    if (localIPs != null &&
                        localIPs.Length > 0)
                    {
                        tbServerName.Text = (from localIP in localIPs
                                             where localIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
                                             select localIP.ToString()).FirstOrDefault();
                    }
                    else
                    {
                        tbServerName.Text = "";
                    }

Thursday, February 23, 2012

javascript send message to child window from parent window

Parent Window

function ProcessMsgParent(msg) {
.
.
.
}


// open child window
var win = window.open(...);

// send message to child window
win.ProcessMsgChild(msg_to_child);

Child Window

function ProcessMsgChild(msg) {
.
.
.

}

// send message to parent window
window.opener.ProcessMsgParent(msg);

Monday, February 6, 2012

C# Printing



private PrintDocument doc = null;

.
.
.
 doc = new PrintDocument();
 PrintController printController = new StandardPrintController();
 doc.PrintController = printController;
 doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
.
.
.
doc.Print();
.
.
.
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
            try
            {
                  Graphics gfxObj = e.Graphics;
                   // for upside down printing
                  graphics.RotateTransform(180.0F);
                   // graphics operations
                  .

                  .
                  .
            }
            catch
            {
            }
            finally
            {
            }
}

Thursday, February 2, 2012

VC++ DLL

DLL:

Create new Win32DLL project, say dllProj.

In header file define:

#ifdef PROJ_EXPORTS
#define PROJ_API __declspec(dllexport)
#else
#define PROJ_API __declspec(dllimport)
#endif

Add functions in header files with PROJ_API prefix and definitions in source file with PROJ_API prefix.

PROJ_API  int foo();

Usage:

Create new Win32Console project.

define:

Top define:
extern int foo();

In function, call:
a=foo();

right click project, click Reference..., Common Properties, Frameworks and References, Add New Reference, Select dllProj project.

compile and run :)

Dotnet Usage:

Using command like linux "strings", read the function signature in the dll, which would be like ?foo@@YAHPAD@Z.

In c#, declare as:

[DllImport("dllProj.dll", CharSet = CharSet.Ansi, EntryPoint = "?foo@@YAHPAD@Z")]
private static extern int foo(string port);

Now, use it in the class as normal static function.

Monday, January 30, 2012

Perl get all HTML Ids

#!/usr/bin/perl

while (<STDIN>) {

        $p = $_;

        while ($p) {
                if($p =~ m/id=\s*\"(\w+)\"/g) {

                        print $1."\n";

                } else { last; }

                $p = $';
        }
}

Usage:

cat file.html | ./getIds.pl

Thursday, January 26, 2012

MFC Database

Connection:

CDatabase dbase;
dbase.OpenEx("DSN=ecx;UID=sa;PWD=wavetec;");
...
dbase.Close();

Retrieve:

CRecordset rs(&dbase);
rs.Open(CRecordset::dynaset, _T("select id,c1, c2, c3 from tbl")); // preserve same order
                                                                                                       //while reading c1, c2,c3

     CDBVariant varValue;

        while( !rs.IsEOF( ) )
        {
           rec.GetFieldValue(_T("id"), varValue, SQL_C_SLONG);
          id = varValue.m_lVal;

          rec.GetFieldValue(_T("c1"), varValue, SQL_C_CHAR);
          val = varValue.m_pstringA->GetString();
          ...
          
           rs.MoveNext( );
        }

        rs.Close();

Update/Delete:

        CString strQl = "update tble set ts=getdate() where c1='";
        strQl += c1
        strQl += "'";

        dbase.ExecuteSQL(strQl);

Wednesday, January 25, 2012

c++ Win UDP Communication

Init:

    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

SOCKET sockRecv,
        sockSend;


Send:

        sockSend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(srcPort);
        addr.sin_addr.s_addr = INADDR_ANY;

        bind(sockSend, (sockaddr *) &addr, sizeof(addr));

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(dstPort);
    addr.sin_addr.s_addr = inet_addr(this->ipAddress.c_str());

    lastMsgLen = SerialLength;
    memcpy(lastMsg, Buffer, lastMsgLen);
    int ret = sendto(sockSend, (char *)Buffer, SerialLength, 0, (SOCKADDR *) &addr, sizeof(addr));


Receive:



        sockRecv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(this->port+1);
        addr.sin_addr.s_addr = INADDR_ANY;

        bind(sockRecv, (sockaddr *) &addr, sizeof(addr));

       struct sockaddr_in raddr;
       int fromLen = sizeof(raddr);

       fd_set rd;
        FD_ZERO(&rd);
        FD_SET(sockRecv, &rd);
        timeval tv;
        tv.tv_sec = 2;
        tv.tv_usec = 0;

        if(select(0, &rd, NULL, NULL, &tv) > 0) {

            if(FD_ISSET(sockRecv, &rd)) {
                int recv = recvfrom(sockRecv, (char *) &pktRecv, sizeof(pktRecv), 0, (sockaddr *)&raddr, &fromLen);
                readStatus = 1;
                break;
            } else {
                ResentLastMsg();
                readStatus = 0;
            }

        } else {
            ResentLastMsg();
            readStatus = 0;
        }

Shutdown:

    WSACleanup();

Tuesday, January 17, 2012

ListView detail view image in column

Set OwnerDraw=true;
Override DrawSubItem and DrawHeader
DrawHeader implement as  e.DrawDefault=true

DrawSubItem implementation as:

            if (e.ColumnIndex == 1)
            {
                e.DrawDefault = false;
                e.DrawBackground();
                int index = e.Item.ImageIndex;
                Image img = imgLstWallHBState.Images[index];
                e.Graphics.DrawImage(img, e.Bounds.Location);
            }
            else
            {
                e.DrawDefault = true;
            }

Wednesday, December 7, 2011

Shift characters left in string

Recently I have to received data from serial port, but data was asynchronous, so I have to match the sentinal value in the string. I read a buffer then I keep reading one character at a time and shifting string left one character and searching string to find the sentinal value, following is the function to shift characters left:


char *shiftLeft(char *str, int count) {

        char *savPtr=str;
        int len = strlen(str);

        while(*str) {

                if(str+count <= savPtr + len) {
                        *str = *(str+count);
                        str++;
                } else {
                        *str = 0;
                }
        }

        return savPtr;
}

Thursday, November 3, 2011

ASP file upload

Use following HTML for file upload:

  <form name="frmSend" method="POST" enctype="multipart/form-data" accept-charset="utf-8">
            Bulk Upload: <input name="bulkfile" type="file" />
            <input type="submit" value="Upload" / id=submit1 name=submit1>
</form>

Obtain zip file from http://www.freeaspupload.net/freeaspupload/download.asp. Extract it and place freeASPUpload.asp in the directory containing scripts.

I had to comment out /disable line 122

Open the script to have upload functionality and add following line:

'At start of script


<%@ Language=VBScript %>
<%

    Response.Expires = -1
    Server.ScriptTimeout = 600
    ' All communication must be in UTF-8, including the response back from the request
    Session.CodePage  = 65001

.
.
.
%>

'then

<!-- #include file="freeaspupload.asp" -->

Dim uploadsDirVar
 uploadsDirVar = Session("UploadPath")

            if Request.ServerVariables("REQUEST_METHOD") = "POST" then
           
                Dim Upload, origName, savName, fullName
               
                Set Upload = New FreeASPUpload
                Upload.SaveOne uploadsDirVar,0,origName,savName

ASP/VBS reading excel file

Following is the code to read excel file, inspired from http://forums.aspfree.com/code-bank-54/classic-asp-read-excel-file-into-recordset-141448.html

Dim objConnXls, objRSXls, strSQLXls

Set objConnXls = Server.CreateObject("ADODB.Connection")
objConnXls.Open "DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=NO; "& _
"Excel 8.0; DBQ=" & fullName & "; "
           
strSQLXls = "select * from [data]"

set objRSXls = objConnXls.Execute(strSQLXls)

'Fields can be processed as follows:

Dim username, userpassword, firstname, lastname, gender, profession_id, email, dob, country
                   
 username = objRSXls.Fields(0).value
 userpassword = objRSXls.Fields(1).value
 firstname = objRSXls.Fields(2).value
 lastname = objRSXls.Fields(3).value
 gender = objRSXls.Fields(4).value
 profession_id = objRSXls.Fields(5).value
 email = objRSXls.Fields(6).value
 dob = objRSXls.Fields(7).value
 country = objRSXls.Fields(8).value

objRSXls.Close

C++: CGI GET Request

GET request is easy to process, as Query String is passed in environment variable.
Follow is the code to process GET Request:

void ProcessQueryString(map<string,string>& query) {
    char *queryStr = getenv("QUERY_STRING");

    char *ptr=NULL;
    char *savPtr;
    ptr=strtok_r(queryStr, "&=", &savPtr);
    while (ptr != NULL) {
        string key(ptr);
        ptr = strtok_r(NULL, "&=", &savPtr);
        if(ptr!=NULL && strlen(ptr) > 0) {
            string value(ptr);
            query[key]=value;
        }
        ptr=strtok_r(NULL, "&=", &savPtr);

    }
}

C++: CGI POST Data

Following is the code to process HTTP-Post request, it stores the result in map<string, string>, idea is taken from http://www.openroad.org/cgihelp/cgi.html. To decode code from HTTP encoding to is taken from http://www.cs.tut.fi/~jkorpela/forms/cgic.html.


void ProcessPostQueryString(map<string,string>& query) {


        //FILE *fp = fopen("post.txt", "w+");
        char tmp[5*1024];

        char *ptmp = getenv("CONTENT_LENGTH");

        if(ptmp == NULL || strlen(ptmp) < 1) {
                return;
        }


        int contentLength = atoi(getenv("CONTENT_LENGTH"));
        char queryStr[5*1024];

        //sprintf(tmp, "content length[%d]\n", contentLength);
        //fputs(tmp, fp);
        //fflush(fp);

        if(contentLength > sizeof(queryStr)) {

                throw "only 5K of Post query string supported";
        }

        //fputs("post data: ", fp);
        int length=contentLength;
        //while(length > 0)
        {
                int nread = fread(queryStr, length, 1, stdin);
                //fputs(queryStr, fp);

                length -= nread;
        }
        queryStr[contentLength] = 0;

        //fputs("\n", fp);
        //fflush(fp);

        char *ptrSentence=NULL,
                 *ptrWord=NULL;
        char *savPtr1, *savPtr2;
        ptrSentence=strtok_r(queryStr, "&", &savPtr1);
        while (ptrSentence != NULL) {


                //cout << '[' << ptrSentence << ']' << endl;
                ptrWord = strtok_r(ptrSentence, "=", &savPtr2);
                if(ptrWord == NULL && strlen(ptrWord) < 1)  continue;
                string key(ptrWord);
                ptrWord = strtok_r(NULL, "=", &savPtr2);
                if(ptrWord !=NULL && strlen(ptrWord) > 0) {

                        int len=strlen(ptrWord);
                        HTMLDecode(ptrWord, ptrWord+len, tmp);
                        tmp[len]=0;

                        string value(tmp);
                        query[key]=value;

                        sprintf(tmp, "(%s)=(%s)\n", key.c_str(), value.c_str());
                        //fputs(tmp, fp);
                        //fflush(fp);
                }

                ptrSentence=strtok_r(NULL, "&", &savPtr1);
                if(ptrSentence==NULL)  break;

        }

        //fclose(fp);
}

Following is the code to decode HTML encoded string:

int HTMLDecode(char *src, char *last, char *dest)
{
    int sz=0;
 for(; src != last; src++, dest++, sz++)
   if(*src == '+')
     *dest = ' ';
   else if(*src == '%') {
     int code;
     sscanf(src+1, "%2x", &code) != 1;
     *dest = code;
     src +=2; }
   else
     *dest = *src;

 *dest=0;
 return sz;
}

Wednesday, October 26, 2011

C++: Load configurations


Header:
#include <cstdio>
#include <cstring> 
#include <map>
#include <fstream>
#include <algorithm>

using namespace std;

void LoadConfig(const char *fileName);
string GetConf(string key);
int GetIntConf(string key);

Definition:

char *strtrim(char *&str) {

        int len = strlen(str);
        char *ptr = str+len-1;
        while((*ptr == ' ' ||
                        *ptr == '\t' ||
                        *ptr == '\r' ||
                        *ptr == '\n') &&
                        str <= ptr) {
                ptr--;
        }
        *(ptr+1) = 0;

        ptr=str;
        while((*ptr == ' ' ||
                        *ptr == '\t' ||
                        *ptr == '\r' ||
                        *ptr == '\n') &&
                        ptr < str+len-1 &&
                        *ptr != 0) {
                ptr++;
        }

        str = ptr;
        return ptr;
}

char *strlower(char *str) {
        char *p=str;

        while(*str != 0) {

                if(*str >= 'A' &&
                                *str <= 'Z') {
                        *str -= 'A';
                        *str += 'a';
                }

                str++;
        }

        return p;
}

map<string, string> configuration;
void LoadConfig(const char* fileName) {

        char ltmp[1024];

        sprintf(ltmp, "Loading config file (%s)", fileName);
        log(ltmp);

        ifstream infile(fileName);

                        while(!infile.eof()) {

                                char tmp[2048];
                                infile.getline(tmp, sizeof(tmp));

                                if(tmp[0] == '#') {
                                        sprintf(ltmp, "skipping commnet (%s)", tmp);
                                        log(ltmp);
                                        continue;
                                }

                                char *ptmp = tmp;
                                strtrim(ptmp);
                                if(strlen(ptmp) <= 0) continue;
#define SEP "="

                                char key[1024];
                                char value[1024];
                                char *pstr=strtok(tmp, SEP);
                                strcpy(key,strlower(pstr));
                                string strKey = key;
                                pstr=strtok(NULL, SEP);
                                strcpy(value, strtrim(pstr));
                                string strValue = value;

                                sprintf(ltmp, "(%s) = (%s)", key, value);
                                log(ltmp);

                                configuration[strKey]=strValue;

                        }

                infile.close();
                configLoad = true;

                if(GetConf("DebugEnabled") == "true") {
                        debugEnabled = true;
                }
}

string GetConf(string key) {

        char tmp[1024];

        std::transform(key.begin(), key.end(), key.begin(), ::tolower);
        if(configuration.find(key) == configuration.end())
        {
                sprintf(tmp, "Key (%s) not found in configuration", key.c_str());
                log(tmp);

                return "";
        }
        return configuration[key];
}

int GetIntConf(string key) {

        char tmp[1024];

        std::transform(key.begin(), key.end(), key.begin(), ::tolower);
        if(configuration.find(key) == configuration.end())
        {
                sprintf(tmp, "Key (%s) not found in configuration", key.c_str());
                log(tmp);

                return 0;
        }
        return atoi(configuration[key].c_str());
}

for log, please see previous post minimal logging.