Monday, March 1, 2010

Some useful commands when using GDB

some useful gdb commands and usage I didn't know about:

1. run gdb with -tui parameter gives gdb a native gui to show your source code. you may browse the source code using page up/down, arrow keys, using list to indicate where in the code you wish to show

2. "file" command specify which executable you wish to debug with when in gdb console
3. "break" has shortcut 'b', break points can be enabled and disabled using "enable" and "disable" command. To remove a breakpoint, use "clear" with the line number or use "delete" with the break point ID
4. "info" command, with shortcut 'i', can be used to list out all breakpoints, watched variables,
5. "run" command has shortcut 'r'
6. there are several ways to tell gdb show variable values. one is "display" command, which causes the gdb to report you the variable value so long as it is in scope. you can remove a variable on display using "undisplay" command. "print" and "printf" command can also be used to display a variable's value, print takes a variable name as parameter, printf takes a string with paramters, just like c printf function. "watch" command display the value of a variable only when it has been modified. "rwatch" detects if a variable is being read, "awatch" detects both when variable is being read or written to
7. there are many commands for stepping, "next", "stepi", "step", and also "finish" which brings to the end of a function, "advance" command advance execution to some line or function, just like a temporary breakpoint
8. to trace the stack, use the "backtrace" command to show the call stack
9. you can modify the value of a variable during execution using "set variable" command, or "set ( = )", which is set with parenthesis.
10. you can also attach gdb to a currently running program using the "attach " command
11. when a program exited with segmentation fault, the program will produce a core dump. you may access that core dump using gdb with this command: "gdb -c core "
12. when in gui mode, you may access window information using "info win". you can switch focus of window in console using "fs " or "fs next" to focus on next window in the window info list. you may also set the height of a window using "winheight" command
13. there are several layouts for windows, here is some example:
"layout src"
"layout asm"
"layout split"
"layout reg"
"tui reg general"
"tui reg float"
"tui reg system"
"tui reg next"
14. you may switch your disassembly flavor using the "set disassembly-flavor" to either intel or att

(taken from Beej's GDB guide)

Tuesday, December 8, 2009

accessing history of commands in bash

1. the most common is using !! , CTRL+P or !-1 to access the previous command typed. use !$ to retrieve all parameters for the previous command
2. CTRL+R on terminal allows you to search for a command with a keyword, you can do the same using !
3. there are several global variables controlling the history file, its size, name, allowance for duplications etc.
HISTSIZE - number of history lines
HISTFILESIZE - actual file size limit of the history file
HISTFILE - edit the actual history file used
HISTCONTROL - controls whether history allows duplicates, if not, how to eliminate them. option ignoredups erase consecutive repeating commands, erasedups erase all commands that arn't unique, ignorespace option sets all commands started with a space to be not shown in history
4. you can use an positive index to specify which history command you want to execute from the top of the history file, or negative index if from the button of the history file. history -c cleans up the history of your shell

Friday, November 20, 2009

More Xargs

just learned the coolest xargs trick today. normally xargs can only represent one input symbol using the -I, but you can make it multiply by combining xargs with "sh -c". example:

ls --format=single-column | xargs -n1 sh -c 'g++ -g $1 -o ${1%.cpp}' -

takes all files from the current directory, each line contains 1 file name, pass it to xargs, which executes sh -c 'g++ -g $1 -o ${1%.cpp}' - where the file name will be used as the $1 variable for the shell, - is used as placeholder for $0, and ${1%.cpp} shrinks the file of .cpp string at the end if it contains it. sh is used to execute an command if -c is used

This way we can access multiple variables with xargs, each can be placed in different locations of the command we want to execute.

Wednesday, November 18, 2009

xargs xargs xargs.. ??

I was learning about how to use xargs today.. quite an interesting unix tool. What it lets you do is to trigger a command on each input simple you received from input stream stdin. for example:

ls -l | awk '/.*/ { print $8}' | xargs -n1 -I{} gcc -o {}.o -g {}

long list the current directory with detail, take the last column with awk which contains a list of names, take each name and execute "gcc -o .o -g on them

xargs can print the command that is being generated using the -t option, or -p option for you to confirm each command that will be executed on the file. The -I{} indicates a special symbol {} is going to be used for the location where the symbol name should be put into instead of the default at the end of the command. the -n indicate how many symbols you will be using for each command, -l indicates how many lines of symbols you will be using for each command

xargs called without any command will act the same as an echo command.

It is essentially an easy replacement for for loops in a shell script

Saturday, November 14, 2009

Shell Scripting

For the past few weeks, I was working in broadcom, developing a shell script for their automated nightly build emails. It's been a struggle, and here is what I learned about shell scripts, and its limit:

Things I learned:
1. "echo" command will always echo out a line, use -n to prevent that, -e to let echo interpret the \ characters, which is turned off by default
2. "read" is the command used to read values from user, or from a file. but read automatically interpret the \ values, which is the opposite of echo. you need to use -r to disable that functionality
3. some general review of "sed":
1) you can give multiple sed expression replacements using the -e before every replacement string
2) sed only reads from a file and produce output to int stdout, don't know if there is a way to work around it
3) the format for the regular expression: '[general purpose] / [search pattern] / [replacement pattern] / [output options and search options]'
4) general purpose can be s for substitude, d for delete and such
5) use () in search pattern, those represent a single unit if you wish to output those patterns in the braket using \1 to \9 in the replacement pattern
6) . * ( ) ^ $ are all special parsing characters in search pattern, you need to use \ to indicate it is not otherwise
7) you can use & in the replacement pattern to indicate all the stuff that found to match your search pattern
8) in output options, g means global, which means sed does not quit until all matches are found, p means print the replacement pattern, w means write the replacement to a file indicated
9) sed has an option of -n, which means does not produce any output, normally, sed will put all characters not matched into the stdout, -n prevent that, using p in the output options is the exception to the no output rule, which prints the replacement pattern only into the stdout
10) eval is bad, it only evaluates the immediate expression in the next symbol, and each symbol is terminated by space, not \n
11) IFS is a variable indicating the end of a line or a group of symbols representing a command, which is useful for parsing the symbols with a for loop
12) be careful of if command condition spacing, $var=value is not parsable while $var = value is!
13) if command options: -a = and, -o = or, -z means string is empty, -f means file exists, -d means directory exists,
14) functions in shell script are weird, first of all it does not indicate that it takes any variable, but they do and they can take as many as they want. $# tells how many variables are passed to a shell function. $1 to $n indicate each parameters being passed to a shell script. the function act just like a shell command, no brakets needed to put parameters in it

Limitation:
right now I am still stuck in trying to do this: having two variables, one holds the name of the other, and you want to use echo and eval to show the value held by the other variable. the problem right now is in eval, it does not parse anything after the second space, which if the value in the second variable has space, those other parts will be lost..

I wonder how to fix this

Something to try out:
I wonder how the function environments in shell script functions are generated...

Thursday, August 27, 2009

tricks with bash

some micelaneous stuff learned when working in Safe Software:

in bash, you can use !! to indicate the last command typed in shell. This is useful when you want to debug the previous command you have just typed.

&> directive can pass only the standard in to a file and still have both standard out and err into the stdin of the terminal

>& does the reverse, passing std err to file

$ returns the value of the variable, $(command) returns the result of the command

^cat^less will scrach out the cat and replace the string with less, and will run less on the file. this is useful for editing a long string of commands

$@ from shell scripts returns the value of the shell parameter

if you are gdb-ing a project with multiple files, you can use "break : to specify which file at which line to break

ack-grep is a more powerful version of the grep program

scp is the secure copy over network program! useful

g++ compiler is more strict than windows nmake compiler, and will put each library being compiled into the same namespace, whereas in nmake it puts each library in its own separate namespace

Saturday, July 11, 2009

More Linux useful commands

whenever you entered a command from command line and you want to do something else at the same time, you can pause the current process using ctrl+z

you can send a process to the background of the terminal by using the bg . and you set a background process into the foreground using the fg

Tuesday, May 26, 2009

GDB quick note

si - step assembly instruction
s - step, or step in
n - step by line
run run the debug with arguments supplied
break and delete manages break points, it is also possible to break by condition

Wednesday, March 25, 2009

Yet another simple intro to LInux Kernel and history of X11 window manager

Linux kernel core is loaded into the directory /boot/vmlinuz-KERNEL-VERSION
additional kernel modules are loaded into /lib/modules/KERNEL-VERSION

all kernel modules can be viewed by using "lsmod" command
kernel modules can be added and removed using "modprobe", you may not remove a module if it is currently in use.
modules have parameters that you can pass to it when it is used. "modinfo" help to view to parameters. the information can also be viewed if you have the kernel source coded loaded into /usr/src
/proc/sys is a directory that contains all kernel core policies. one example is "echo 1 > /proc/sys/net/ipv4/ip_forward" modifies the ip forwarding policy to 1, thus your computer now acts as a gateway, which is useful for many man-in-the-middle attacks for stealing private information.
since linux kernel is open source, you can also modify the kernel policies freely. performance tuning is useful for running programs, such as database. oracle and IBM both provide kernel tuning page to run their database faster on linux. "sysctl -a" is a command that returns all policy documentations for the kernel.

it is very important to know that performance tuning have so many parameters, it is best you know what you're doing before changing the values.

X11 is a graphical transport layer protocol used to abstract linux window managers. this makes it possible for linux to adopt to having many different kinds of window manager without stuck with one came with the distribution. the most pro window manager for linux users is FVWM. It made it possible for linux user themselves to design their own window manager, you can make it look like anything you want, which is also welcomed by most hackers who want to have their own unique desktop style.

Saturday, March 21, 2009

New Laptop, New Linux




finally linux has been completely restored. someone mentioned that my linux desktop is pretty, so here is some pictures of my linux desktop. enjoy.

Friday, December 12, 2008

Inama Nushif

Unfortunately, my entire Linux continuum is going to shut down for a while, as i have just lost my laptop to a backpack snacher yesterday.

As I am sitting here, thinking about my unfortunate event. I don't really know what to feel like. I do blame myself, but to how long can you hold your alert senses? you can't be invincible forever, no one can. everyone is bound to have openings at some point. Not having my new laptop feels a bit like giving up smoking for me. i just needed that speed that my old Mac can't really provide by now.

and i'm also confused about what i really want at this point, to say no to "smoking", or to get myself a new laptop, in which i could also get stolen. and i don't really know what brand to choose either, and my reason of having a new laptop is being challenged too, why do i really need a new one?

... i think i'm just gona lay low, and ponder for sometime about this... i need some time... to reborn

Saturday, November 15, 2008

Java Hack: creating multiple classes within one file

I know there are plenty of people out there that don't really like java, and also plenty that do. Those that do says Java is easier to program, but the most good point is that java compilers/interpreters must follow a very strict rule of implementation, such that java code can run on any machine without a problem. now that sounds good, but Java is also a very strict programming langauge, and one the things that I have been having some problem with is they don't allow you to create multiple classes within one file, thinking that by having each file a separate class, everything looks more clear. While that is true, it also become a pain in the ass when you're dealing with a huge program with thousands of classes, which implies thousands of files, and each single one of them are listed in your Package Explorer, and some of them are so small you wonder why they have to be listed as a single file at all. so i figured out a trick to convert multiple classes in java into one file, breaking that rule.

the idea of putting multiple classes inside one is to convert a java class into what C++ refers to as a namespace, a live package that has both static and instance of itself, and putting all the other classes you want to conceal into the file as private member classes of the "namespace". This is the key idea of how to break from the one file one class rule. here is the procedure:

1. copy/create a private class inside your "namespace" class
2. write a public class generator function for each private class that is in the namespace class. the generator function will take all the arguments for the constructor and pass it to the private class constructor. it returns the instance once it is generated
3. create a public singleton instance of the namespace class. note that static instance does not work here.

and wala! now whenever you want to create a new class that is declared in the namespace class, just call the singleton instance and its generator function. and my friends, you have just turned a java class into a C++ namespace

Friday, September 26, 2008

5 searching commands in linux

1. find

2. locate
-uses a database of file location instead of searching the real location, the database only updated everyday

3. whereis [-bsm]
- returns the location for a specific command line command, -b gives the location of the binary, -s gives the location of the source, and -m gives the location of the man pages

4. which
- returns all the location for the specific command. it searches the PATH env variable for locating the binary. great if there are multiple versions of a command in PATH.
- it returns the full path for each version of the command

5. type [-a]
- returns the type of command this console is using for the default, could be a shell built in, a UNIX command, a linux command etc. -a gives this property for all commands that are with the same name

Sunday, September 14, 2008

Cloud Computing Digest

Cloud Computing is a fairly new internet service structure. it provide ways to virtually map physical storage, internet access together with a web based application for a remote user. The advantage of cloud computing is the reduction in the amount of server and energy needed to run a large scale service.
the idea of cloud computing resides in thinking of whole bunch of servers running for different services as a cloud, instead of independently running machines. It virtuallizes all physical machines into several layers: infrastructure service layer, platform service layer, and software service layer.

infrastructure service layer manages web throughput and storage space for remote server, giving each remote client a fixed network power and storage space without having all those service being tied down to one service machine. it also provide ways to run a operating platform on top of the infrastructure layer, limiting the storage and service to specific platforms that uses it.

the platform service layer provide users a selection of applications they could use. an example of platform service is the google application engine

the software service layer contains web applications the user may use, one example would be Google Apps.

information digested from here

Monday, September 8, 2008

some more UNIX tips

1. auto-complete features in all shells:

bash: TAB or double TAB
cash: escape
korn: escape \ or double escape (depends on EDITOR var setting, either vi or emacs)

2. accessing previous command arguments from shell
$! access the last argument of the last command entry
!:1 or another number can access the last command argument at the position starting from 1

3. pushd and popd creates a stack of file locations that can be accessed without using cd command. it can also manipulate the stack, rotating its order with +i or -i where i is the rotation number to rotate, + puts the frontmost to the back, - puts the backmost to the front

4. curl command can be used to retrieve web information, use -s to ignore processing output, use -o to download any file from the internet

5. name matching special characters:
^ -> matching anything to the starting of line as in ^A
? -> matching anything at the end of the line A?
[] -> matching any character within the bracket, use - for indicating range
[^]-> matching any character except for those within the bracket
. -> match a single character of any value except for EOF
* -> match 0 or more preceding characters to expression
\{x,y\} -> match to x to y occurence of the preceding
\{x\} -> match to exactly x occurence of the preceding
\{x,\} -> match to x or more occurence of the preceding

6. some awk example (more examples)

$ cat text
testing the awk command
$ awk '{ i = length($0); print i }' text
23
$ awk '{ i = index($0,”ing”); print i}' text
5
$ awk 'BEGIN { i = 1 } { n = split($0,a," "); while (i <= n) {print a[i]; i++;} }' text

(summarized from here)

Wednesday, September 3, 2008

Google Chrome, so much faster and so much more simple to use

it looks like google's new browser: Chrome is so 5 times faster than firefox and other browsers in general. it is also very small and very simple to use.

you may download it here

Thursday, August 28, 2008

another good reminder that lots of fun google search techniques are here

1. "define:" gives you the meaning of all words you write afterwards
2. "time" gives you the time of location
3. google search can be used as calculator with equation and end with '='
4. currency conversion by in
5. get the map of a city by map
6. get related search results using the "related:" keyword
7. use + and - in search term to plus a meaning or minus a meaning to the search term
8. type in a 3 digit and get the area where the 3 digit is used as phone number initial
9. quote search terms to get the exact result you want

Monday, August 25, 2008

in case i forgot it again, here is the link to wxWidgets installation guide for VC++2008

LINK

good linux habits

taken from IBM Linux help site

1. mkdir -p option allows multiple directory in different depth to be created all at once

e.g. mkdir -p good/{fun,happy/photos}
will create a directory good, which contains 2 directories fun and happy, and in happy a new directory photos

2. tar xvf -C unarchive without having to move the tar to the destination directory, giving the user an option to specify where the directory the tar will be unarchived

e.g. tar xvf /temp/a newarc.tar.gz

3. the && and || operator in command line are more advaned replacements for ;, which is the command separator in console. && checks if previous command have executed and returned 0, and only if it returned 0 the second command runs. || checks if the previous command returned non-zero, and only execute second command if non-zero return exit has been returned

e.g. cd /temp/a && mkdir b
e.g. cd /temp/a || mkdir -p /temp/a

4. It is generally a good idea to enclose variable calls in double quotation marks, unless you have a good reason not to. Similarly, if you are directly following a variable name with alphanumeric text, be sure also to enclose the variable name in curly braces ({}) to distinguish it from the surrounding text.

5. the escape sequence \ makes commands more clear

6. it's good habit to group commands using () in subshell or {} in current shell. this way all commands inside () or {} have their outputs grouped together for further use. make sure that there is a space between commands and {}

7. xargs is a powerful output format tool that can do many types of filtering

e.g. ls -l | xargs
combines all listed files into one line

e.g. ls | xargs file
lists all files and its file type

a caution that xargs can cause error when reading '_', which if placed in a single line cause xargs to ignore anything afterwards, xargs -e turns off the end of file string feature

8. grep -c does the same thing as grep | wc -l and is faster. but grep -c only count lines containing matching patterns. to count all matching patterns,even if a line contain more than 1, use grep -o | wc -l

grep -o cannot be used with -c at the same time

9. use awk instead of grep when possible. awk captures the line if word matches the key at the right index

e.g. ls -l | awk '$6 = "Dec"'
captures all lines with 6th word = Dec

10. grep doesn't have to work with cat because grep can take file names as arguments

e.g. time grep and tmp/a/longfile.txt
does the same as time cat tmp/a/longfile.txt | grep and

Wednesday, August 20, 2008

Turn Gmail into a to do list with Superstar

you can turn gmail into a to do list. first turn on the new google lab gmail feature: Super star, and turn on 3 stars: red, orange, green. 2) enter

l:^ss_sr OR l:^ss_so OR l:^ss_sg

into the search field and any emails with red, orange and green stars will be selected, and the selection is updated every search

keep the search command page and enter a quick link on the left side for this, call it "to do list"

---------------

on the other hand, VMWare has released a linux virtual machine Fushion 2.0 for Mac. should check it out