=利用bcc来查看arduino的编译原理
arduino 单片机环境只需要用户填写2个函数setup() 和loop(),而C语言里习惯的main()在哪里呢? 可以用万能的bcc工具(https://github.com/iovisor/bcc)来跟踪下到底调用了哪些命令,我测试了一下,使用arduino UNO的单板,编译自带的 01.Basics
的BLINK例子,来看看到底底层是怎么执行的,结果如下:
[nsiy@nsiy-pc tools]$ sudo ./execsnoop --max-args 60
[sudo] nsiy 的密码:
PCOMM PID PPID RET ARGS
arduino-builder 3349 1402 0 /usr/share/arduino/arduino-builder -dump-prefs -logger=machine -hardware /usr/share/arduino/hardware -tools /usr/share/arduino/tools-builder -libraries /home/nsiy/Arduino/libraries -fqbn=archlinux-arduino:avr:uno -vid-pid=0000_0000 -ide-version=10809 -build-path /tmp/arduino_build_103047 -warnings=null -prefs=build.warn_data_percentage=75 /usr/share/arduino/examples/01.Basics/Blink/Blink.ino
arduino-builder 3358 1402 0 /usr/share/arduino/arduino-builder -compile -logger=machine -hardware /usr/share/arduino/hardware -tools /usr/share/arduino/tools-builder -libraries /home/nsiy/Arduino/libraries -fqbn=archlinux-arduino:avr:uno -vid-pid=0000_0000 -ide-version=10809 -build-path /tmp/arduino_build_103047 -warnings=null -prefs=build.warn_data_percentage=75 /usr/share/arduino/examples/01.Basics/Blink/Blink.ino
avr-g++ 3368 3358 0 /usr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -fno-devirtualize -fno-use-cxa-atexit -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I/usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard /tmp/arduino_build_103047/sketch/Blink.ino.cpp -o /dev/null
cc1plus 3369 3368 0 /usr/lib/gcc/avr/9.2.0/cc1plus -E -quiet -CC -I /usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I /usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard -imultilib avr5 -D__AVR_ATmega328P__ -D__AVR_DEVICE_NAME__=atmega328p -D F_CPU=16000000L -D ARDUINO=10809 -D ARDUINO_AVR_UNO -D ARDUINO_ARCH_AVR /tmp/arduino_build_103047/sketch/Blink.ino.cpp -o /dev/null -mn-flash=1 -mno-skip-bug -mmcu=avr5 -std=gnu++11 -Wno-error=narrowing -w -w -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -fno-devirtualize -fno-use-cxa-atexit -g -fworking-directory -Os -mn-flash=1 -mno-skip-bug -fno-rtti -fno-enforce-eh-specs -fno-exceptions
avr-g++ 3370 3358 0 /usr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -fno-devirtualize -fno-use-cxa-atexit -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I/usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard /tmp/arduino_build_103047/sketch/Blink.ino.cpp -o /tmp/arduino_build_103047/preproc/ctags_target_for_gcc_minus_e.cpp
cc1plus 3371 3370 0 /usr/lib/gcc/avr/9.2.0/cc1plus -E -quiet -CC -I /usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I /usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard -imultilib avr5 -D__AVR_ATmega328P__ -D__AVR_DEVICE_NAME__=atmega328p -D F_CPU=16000000L -D ARDUINO=10809 -D ARDUINO_AVR_UNO -D ARDUINO_ARCH_AVR /tmp/arduino_build_103047/sketch/Blink.ino.cpp -o /tmp/arduino_build_103047/preproc/ctags_target_for_gcc_minus_e.cpp -mn-flash=1 -mno-skip-bug -mmcu=avr5 -std=gnu++11 -Wno-error=narrowing -w -w -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -fno-devirtualize -fno-use-cxa-atexit -g -fworking-directory -Os -mn-flash=1 -mno-skip-bug -fno-rtti -fno-enforce-eh-specs -fno-exceptions
arduino-ctags 3373 3358 0 /usr/bin/arduino-ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /tmp/arduino_build_103047/preproc/ctags_target_for_gcc_minus_e.cpp
avr-g++ 3374 3358 0 /usr/bin/avr-g++ -c -g -Os -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -fno-devirtualize -fno-use-cxa-atexit -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I/usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard /tmp/arduino_build_103047/sketch/Blink.ino.cpp -o /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o
cc1plus 3375 3374 0 /usr/lib/gcc/avr/9.2.0/cc1plus -quiet -I /usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I /usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard -imultilib avr5 -MMD /tmp/arduino_build_103047/sketch/Blink.ino.cpp.d -MQ /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o -D__AVR_ATmega328P__ -D__AVR_DEVICE_NAME__=atmega328p -D F_CPU=16000000L -D ARDUINO=10809 -D ARDUINO_AVR_UNO -D ARDUINO_ARCH_AVR /tmp/arduino_build_103047/sketch/Blink.ino.cpp -mn-flash=1 -mno-skip-bug -quiet -dumpbase Blink.ino.cpp -mmcu=avr5 -auxbase-strip /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o -g -Os -Wno-error=narrowing -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -fno-devirtualize -fno-use-cxa-atexit -mn-flash=1 -mno-skip-bug -fno-rtti -fno-enforce-eh-specs -fno-exceptions -o /tmp/ccYvZ3wm.s
avr-as 3376 3374 0 /usr/bin/avr-as -I /usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I /usr/share/arduino/hardware/archlinux-arduino/avr/variants/standard -mmcu=avr5 -mgcc-isr -mno-skip-bug -o /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o /tmp/ccYvZ3wm.s
avr-gcc 3379 3358 0 /usr/bin/avr-gcc -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o /tmp/arduino_build_103047/Blink.ino.elf /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o /tmp/arduino_build_103047/core/core.a -L/tmp/arduino_build_103047 -lm
collect2 3380 3379 0 /usr/lib/gcc/avr/9.2.0/collect2 -plugin /usr/lib/gcc/avr/9.2.0/liblto_plugin.so -plugin-opt=/usr/lib/gcc/avr/9.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccIiAhnH.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lm -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-latmega328p -flto -mavr5 -Tdata 0x800100 -o /tmp/arduino_build_103047/Blink.ino.elf /usr/lib/gcc/avr/9.2.0/../../../../avr/lib/avr5/crtatmega328p.o -L/tmp/arduino_build_103047 -L/usr/lib/gcc/avr/9.2.0/avr5 -L/usr/lib/gcc/avr/9.2.0/../../../../avr/lib/avr5 -L/usr/lib/gcc/avr/9.2.0 -L/usr/lib/gcc/avr/9.2.0/../../../../avr/lib --gc-sections /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o /tmp/arduino_build_103047/core/core.a -lm --start-group -lgcc -lm -lc -latmega328p --end-group
avr-ld 3381 3380 0 /usr/bin/avr-ld -plugin /usr/lib/gcc/avr/9.2.0/liblto_plugin.so -plugin-opt=/usr/lib/gcc/avr/9.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccIiAhnH.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lm -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-latmega328p -mavr5 -Tdata 0x800100 -o /tmp/arduino_build_103047/Blink.ino.elf /usr/lib/gcc/avr/9.2.0/../../../../avr/lib/avr5/crtatmega328p.o -L/tmp/arduino_build_103047 -L/usr/lib/gcc/avr/9.2.0/avr5 -L/usr/lib/gcc/avr/9.2.0/../../../../avr/lib/avr5 -L/usr/lib/gcc/avr/9.2.0 -L/usr/lib/gcc/avr/9.2.0/../../../../avr/lib --gc-sections /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o /tmp/arduino_build_103047/core/core.a -lm --start-group -lgcc -lm -lc -latmega328p --end-group
lto-wrapper 3382 3381 0 /usr/lib/gcc/avr/9.2.0/lto-wrapper @/tmp/ccvbjpFP
avr-gcc 3383 3382 0 /usr/bin/avr-gcc @/tmp/cc43ORIX
lto1 3384 3383 0 /usr/lib/gcc/avr/9.2.0/lto1 -mno-skip-bug -quiet -dumpdir /tmp/arduino_build_103047/ -dumpbase Blink.ino.elf.wpa -mn-flash=1 -mmcu=avr5 -mn-flash=1 -mno-skip-bug -mmcu=avr5 -auxbase Blink.ino.cpp -g -Os -Os -fno-openmp -fno-openacc -fno-pie -fuse-linker-plugin -fltrans-output-list=/tmp/Blink.ino.elf.rovuWX.ltrans.out -fwpa -fresolution=/tmp/ccIiAhnH.res -flinker-output=exec @/tmp/cciZii60
avr-gcc 3385 3382 0 /usr/bin/avr-gcc @/tmp/cc99Gv4T
lto1 3386 3385 0 /usr/lib/gcc/avr/9.2.0/lto1 -mno-skip-bug -quiet -dumpdir /tmp/arduino_build_103047/ -dumpbase Blink.ino.elf.ltrans0 -mn-flash=1 -mmcu=avr5 -mn-flash=1 -mno-skip-bug -mmcu=avr5 -auxbase-strip /tmp/Blink.ino.elf.rovuWX.ltrans0.ltrans.o -g -Os -Os -fno-openmp -fno-openacc -fno-pie -fuse-linker-plugin -fltrans @/tmp/cci83VV7 -o /tmp/cc7irmI7.s
avr-as 3387 3385 0 /usr/bin/avr-as -mmcu=avr5 -mgcc-isr -mno-skip-bug -o /tmp/Blink.ino.elf.rovuWX.ltrans0.ltrans.o /tmp/cc7irmI7.s
avr-objcopy 3388 3358 0 /usr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 /tmp/arduino_build_103047/Blink.ino.elf /tmp/arduino_build_103047/Blink.ino.eep
avr-objcopy 3389 3358 0 /usr/bin/avr-objcopy -O ihex -R .eeprom /tmp/arduino_build_103047/Blink.ino.elf /tmp/arduino_build_103047/Blink.ino.hex
avr-size 3390 3358 0 /usr/bin/avr-size -A /tmp/arduino_build_103047/Blink.ino.elf
可以看到从原始代码文件Blink.ino 被编辑器改成 /tmp/arduino_build_103047/sketch/Blink.ino.cpp,编译成 /tmp/arduino_build_103047/sketch/Blink.ino.cpp.o, 和 /tmp/arduino_build_103047/core/core.a 合并成二进制 /tmp/arduino_build_103047/Blink.ino.elf 。
那么core.a是由什么文件组成的呢,可以看到:
[nsiy@nsiy-pc core]$ ar t ./core.a
CDC.cpp.o
HardwareSerial.cpp.o
HardwareSerial0.cpp.o
HardwareSerial1.cpp.o
HardwareSerial2.cpp.o
HardwareSerial3.cpp.o
IPAddress.cpp.o
PluggableUSB.cpp.o
Print.cpp.o
Stream.cpp.o
Tone.cpp.o
USBCore.cpp.o
WInterrupts.c.o
WMath.cpp.o
WString.cpp.o
abi.cpp.o
hooks.c.o
main.cpp.o
new.cpp.o
wiring.c.o
wiring_analog.c.o
wiring_digital.c.o
wiring_pulse.S.o
wiring_pulse.c.o
wiring_shift.c.o
[nsiy@nsiy-pc core]$
看到有main.cpp.o ,可以找到这个main文件在 /tmp/arduino_build_103047/core/ 下面,其中main.cpp.d 可以看到原始的main文件在 /usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino/main.cpp ,这就是arduino的main的位置。