【Android小知识】 getprogname()函数的由来

本文详细介绍了在Android开发中如何查找并理解getprogname()函数的声明、定义及其来源。通过构造错误环境,分析编译选项,发现该函数声明于bionic/libc/include/stdlib.h,并在bionic/libc/upstream-openbsd/lib/libc/gen/getprogname.c中实现。在进程启动时,getprogname()返回的程序名由全局变量__progname存储,该变量在libc_init_common.cpp中初始化为argv[0]。
摘要由CSDN通过智能技术生成

在android开发中,我们会经常用到 getprogname()函数去debug。
这个函数太好用了,以至于我们经常会根据这个函数返回的应用进场名来区分不同的进程。
那么那个函数实现的位置在哪呢?

首先,查找声明的地方

我们我需要知道这个函数声明的地方,需要预编译代码,后去函数声明的头文件。
预编译我们需要知道预编译选项,
但是android正常编译时不会打印预编译选项,只会打印结果,
只有报错的时候,才会有一大串长长的编译选项,
故我们需要构造报错环境
以 frameworks/av/media/libmedia/MediaProfiles.cpp 为例
我们在MediaProfiles.cpp加上一些非法字符,为了表示我们是专业的,我们
我增加了一句话 +#error “error”
@@ -29,7 +29,7 @@
#include

#include “media/AVMediaExtensions.h”

+#error “error”
namespace android {

constexpr char const * const MediaProfiles::xmlFiles[];
编译这个文件时,便会有如下报错,
[ 55% 251/451] //frameworks/av/media/libmedia:libmedia clang++ MediaProfiles.cpp
FAILED: out/soong/.intermediates/frameworks/av/media/libmedia/libmedia/android_arm64_armv8-a_kryo300_shared_core/obj/frameworks/av/media/libmedia/MediaProfiles.o
PWD=/proc/self/cwd prebuilts/misc/linux-x86/ccache/ccache prebuilts/clang/host/linux-x86/clang-4053586/bin/clang++ -c -Iframeworks/av/media/libmedia/aidl -Iframeworks/av/media/libmedia/include -Iframeworks/av/media/libavextensions -Iframeworks/av/media/libmedia -fno-exceptions -Wno-multichar -fno-strict-aliasing -fstack-protector-strong -ffunction-sections -fdata-sections -funwind-tables -Wa,–noexecstack -Werror=format-security -D_FORTIFY_SOURCE=2 -fno-short-enums -no-canonical-prefixes -Werror=pointer-to-int-cast -Werror=int-to-pointer-cast -Werror=implicit-function-declaration -DNDEBUG -O2 -g -Wstrict-aliasing=2 -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -DNDEBUG -UDEBUG -fdebug-prefix-map=/proc/self/cwd= -D__compiler_offsetof=__builtin_offsetof -Werror=int-conversion -Wno-reserved-id-macro -Wno-format-pedantic -Wno-unused-command-line-argument -fcolor-diagnostics -Wno-expansion-to-defined -fdebug-prefix-map=$PWD/= -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Werror=date-time -nostdlibinc -Iout/soong/.intermediates/frameworks/av/media/libmedia/libmedia/android_arm64_armv8-a_kryo300_shared_core/gen/aidl -Iframeworks/av/media/libmedia/include -Iframeworks/av/media/libstagefright/include -Iframeworks/native/headers/media_plugin -Iframeworks/native/headers/media_plugin/media/openmax -Isystem/core/libcutils/include -Isystem/core/libutils/include -Isystem/core/libbacktrace/include -Isystem/core/liblog/include -Isystem/core/libsystem/include -Iframeworks/av/media/libstagefright/foundation/include -Ibionic/libc/malloc_debug -Ibionic/libc/async_safe/include -Isystem/core/demangle/include -Isystem/libhidl/transport/token/1.0/utils/include -Iout/soong/.intermediates/frameworks/native/libs/binder/libbinder/android_arm64_armv8-a_kryo300_static_core/gen/aidl -Iframeworks/native/libs/binder/include -Isystem/core/base/include -Iout/soong/.intermediates/frameworks/native/libs/binder/libbinder/android_arm64_armv8-a_kryo300_shared_core/gen/aidl -Isystem/libhidl/base/include -Isystem/libhidl/transport/include -Iout/soong/.intermediates/system/libhidl/transport/manager/1.0/android.hidl.manager@1.0_genc++_headers/gen -Iout/soong/.intermediates/system/libhidl/transport/manager/1.1/android.hidl.manager@1.1_genc++_headers/gen -Iout/soong/.intermediates/system/libhidl/transport/base/1.0/android.hidl.base@1.0_genc++_headers/gen -Isystem/libhwbinder/include -Iout/soong/.intermediates/hardware/interfaces/graphics/common/1.0/android.hardware.graphics.common@1.0_genc++_headers/gen -Iout/soong/.intermediates/hardware/interfaces/media/1.0/android.hardware.media@1.0_genc++_headers/gen -Iout/soong/.intermediates/hardware/interfaces/graphics/bufferqueue/1.0/android.hardware.graphics.bufferqueue@1.0_genc++_headers/gen -Iout/soong/.intermediates/hardware/interfaces/media/omx/1.0/android.hardware.media.omx@1.0_genc++_headers/gen -Iframeworks/native/libs/ui/include -Iframeworks/native/libs/nativebase/include -Ihardware/libhardware/include -Isystem/media/audio/include -Iframeworks/native/libs/arect/include -Iframeworks/native/libs/math/include -Iexternal/sonivox/arm-wt-22k/include -Iexternal/icu/icu4c/source/common -Iexternal/icu/icu4c/source/i18n -Iexter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值