tech_log: December 2008

Tuesday, December 16, 2008

Suppressing annoying find / -name "something" error messages like "find: `/proc/1/fd': Permission denied"

Have you ever tried looking for some file like this:

drasko@kanta:~$ find / -name "some_file.c"
find: `/.mozilla': Permission denied
find: `/proc/tty/driver': Permission denied
find: `/proc/1/task/1/fd': Permission denied
find: `/proc/1/task/1/fdinfo': Permission denied
find: `/proc/1/fd': Permission denied
find: `/proc/1/fdinfo': Permission denied
find: `/proc/2/task/2/fd': Permission denied
find: `/proc/2/task/2/fdinfo': Permission denied
find: `/proc/2/fd': Permission denied
find: `/proc/2/fdinfo': Permission denied
find: `/proc/3/task/3/fd': Permission denied
find: `/proc/3/task/3/fdinfo': Permission denied
find: `/proc/3/fd': Permission denied

and many lines like this...

Eventually, you loose yourself in this and do not see the eventual hit. You do not see the tree because of the forest.

Now, you will not get out easily just by piping this to grep, like:
drasko@kanta:~$ find / -name "some_file.c" | grep some_file.c
because these "Permission denied" lines do not go to the stdout but to stderr, so they will be displayed anyway.

Solution is to redirect stderr out of find to /dev/null:

drasko@kanta:~$ find / -name "some_file.c" 2> /dev/null
/home/drasko/some_file.c
drasko@kanta:~$


Here you go.

Friday, December 12, 2008

ASM_CMT macro for creating comments in asm produced from C


C code:

//! Assembly comment
#define ASM_CMT(str) asm volatile("@ " str)

int main()
{
   int i = 5;
   int j = 0;

   ASM_CMT("This is a comment!");

   for (i=0; i<10; i++)
      j = i + 1;

   ASM_CMT("This is another comment");

   return 0;
}


Produced asm:

.file    "asm_cmt.c"
    .text
    .align    2
    .global    main
    .type    main, %function
main:
    @ args = 0, pretend = 0, frame = 8
    @ frame_needed = 1, uses_anonymous_args = 0
    mov    ip, sp
    stmfd    sp!, {fp, ip, lr, pc}
    sub    fp, ip, #4
    sub    sp, sp, #8
    mov    r3, #5
    str    r3, [fp, #-20]
    mov    r3, #0
    str    r3, [fp, #-16]
#APP
    @ This is a comment!
    mov    r3, #0
    str    r3, [fp, #-20]
    b    .L2
.L3:
    ldr    r3, [fp, #-20]
    add    r3, r3, #1
    str    r3, [fp, #-16]
    ldr    r3, [fp, #-20]
    add    r3, r3, #1
    str    r3, [fp, #-20]
.L2:
    ldr    r3, [fp, #-20]
    cmp    r3, #9
    ble    .L3
#APP
    @ This is another comment
    mov    r3, #0
    mov    r0, r3
    sub    sp, fp, #12
    ldmfd    sp, {fp, sp, pc}
    .size    main, .-main
    .ident    "GCC: (GNU) 4.2.4"


gcc differences between .s and .S assembly file name suffix


Although the assembler can be invoked directly, assembly files are more typically passed through gcc. The case of the assembly
file’s suffix (.s versus .S) is important. If gcc is invoked with an uppercase assembly file suffix (.S) it is passed first to the
preprocessor whereas a lowercase file suffix (.s) passes goes directly to the assembler.

---



Please note that GCC treats files with the suffixes ".s" and ".S"
differently: the preprocessor is run on files with the ".S" (capital s)
suffix, not on the ones with the ".s" suffix.



---

file.s

Assembler code. Apple's version of GCC runs the preprocessor
on these files as well as those ending in `.S'.



file.S
Assembler code which must be preprocessed.






About gdb target sim

Some mail from some mailing list:

I've built crossgcc for target
arm-linux and gdb for target arm-elf. (couldn't match the two targets
for some reasons but i thought it wouldn't matter. I couldn't find
any difference between them. Is there any difference?)


I tried a simple example for debugging
with gdb target as simulator and gdb ignores the breakpoints as
follows. I'll appreciate any kind of advice. Thanks in advance.


$ arm-linux-gcc -g foo.c -o foo


$ arm-elf-gdb


GNU gdb 4.18


Copyright 1998 Free Software
Foundation, Inc.


GDB is free software, covered by the
GNU General Public License, and you are


welcome to change it and/or distribute
copies of it under certain conditions.


Type "show copying" to see
the conditions.


There is absolutely no warranty for
GDB. Type "show warranty" for details.


This GDB was configured as
"--host=i686-pc-linux-gnu --target=arm-elf".


(gdb) file foo


Reading symbols from foo...done.


(gdb) target sim


Connected to the simulator.


(gdb) load foo


Loading section .interp, size 0x27 vma
0x20000f4


Loading section .note.ABI-tag, size
0x20 vma 0x2000120


Loading section .hash, size 0x24 vma
0x2000140


Loading section .dynsym, size 0x40 vma
0x2000164


Loading section .dynstr, size 0x3c vma
0x20001a4


Loading section .gnu.version, size 0x8
vma 0x20001e0


Loading section .gnu.version_r, size
0x20 vma 0x20001e8


Loading section .rel.plt, size 0x10 vma
0x2000208


Loading section .init, size 0x18 vma
0x2000218


Loading section .plt, size 0x30 vma
0x2000230


Loading section .text, size 0x350 vma
0x2000260


Loading section .fini, size 0x14 vma
0x20005b0


Loading section .rodata, size 0x4 vma
0x20005c4


Loading section .data, size 0xc vma
0x20085c8


Loading section .ctors, size 0x8 vma
0x20085d4


Loading section .dtors, size 0x8 vma
0x20085dc


Loading section .got, size 0x14 vma
0x20085e4


Loading section .dynamic, size 0x88 vma
0x20085f8


Start address 0x2000260


Transfer rate: 11320 bits in <1 sec.


(gdb) list main


1 int main()


2 {


3 int a=27;


4 int b=4;


5 int c;


6 c=a*b;


(gdb)


7 c=a%b;


8 c=a/b;


9 }


(gdb) br 7


Breakpoint 1 at 0x200036c: file foo.c,
line 7.


(gdb) run


Starting program:
/home/totohero/test/foo


Program exited with code 074.


(gdb) quit




From gdb manual:


13.4.10
Simulated CPU target



For some configurations, GDB includes a CPU simulator that you can
use instead of a hardware CPU to debug your programs. Currently,
simulators are available for ARM, D10V, D30V, FR30, H8/300, H8/500,
i960, M32R, MIPS, MN10200, MN10300, PowerPC, SH, Sparc, V850, W65,
and Z8000.


For the Z8000 family, `target sim' simulates either
the Z8002 (the unsegmented variant of the Z8000 architecture) or the
Z8001 (the segmented variant). The simulator recognizes which
architecture is appropriate by inspecting the object code.


target sim args

Debug programs on a
simulated CPU. If the simulator supports setup options, specify them
via args.


After specifying this target, you can debug programs for the
simulated CPU in the same style as programs for your host computer;
use the file command to load a new program image, the
run command to run your program, and so on.


As well as making available all the usual machine registers (see
info reg), the Z8000 simulator provides three additional
items of information as specially named registers:


cycles

Counts clock-ticks in the simulator.

insts

Counts instructions run in the simulator.

time

Execution time in 60ths of a second.


You can refer to these values in GDB expressions with the usual
conventions; for example, `b fputc if $cycles>5000'
sets a conditional breakpoint that suspends only after at least 5000
simulated clock ticks.