From 86080b8353d4ca0169e107cdc6c34f9a2e70b0ad Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Mon, 23 Sep 2019 15:41:18 -0700 Subject: libbinder: add getDebugPid So that the PID of a service/binder can easily be looked up (for parity w/ HIDL). Bug: 141187318 Test: use w/ dumpsys Change-Id: I29bf9acfe0cd1029d7aa2bb466b97347d46f2947 --- libs/binder/Binder.cpp | 33 +++++++++++++++++++++++++++++++++ libs/binder/include/binder/Binder.h | 2 ++ libs/binder/include/binder/IBinder.h | 6 ++++++ 3 files changed, 41 insertions(+) (limited to 'libs') diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index 693045e7f3..34b6ea5385 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp @@ -99,6 +99,32 @@ status_t IBinder::getExtension(sp* out) { return reply.readNullableStrongBinder(out); } +status_t IBinder::getDebugPid(pid_t* out) { + BBinder* local = this->localBinder(); + if (local != nullptr) { + *out = local->getDebugPid(); + return OK; + } + + BpBinder* proxy = this->remoteBinder(); + LOG_ALWAYS_FATAL_IF(proxy == nullptr); + + Parcel data; + Parcel reply; + status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply); + if (status != OK) return status; + + int32_t pid; + status = reply.readInt32(&pid); + if (status != OK) return status; + + if (pid < 0 || pid > std::numeric_limits::max()) { + return BAD_VALUE; + } + *out = pid; + return OK; +} + // --------------------------------------------------------------------------- class BBinder::Extras @@ -152,6 +178,9 @@ status_t BBinder::transact( case EXTENSION_TRANSACTION: err = reply->writeStrongBinder(getExtension()); break; + case DEBUG_PID_TRANSACTION: + err = reply->writeInt32(getDebugPid()); + break; default: err = onTransact(code, data, reply, flags); break; @@ -250,6 +279,10 @@ sp BBinder::getExtension() { return e->mExtension; } +pid_t BBinder::getDebugPid() { + return getpid(); +} + void BBinder::setExtension(const sp& extension) { Extras* e = getOrCreateExtras(); e->mExtension = extension; diff --git a/libs/binder/include/binder/Binder.h b/libs/binder/include/binder/Binder.h index 1095c7f28c..5673d5a871 100644 --- a/libs/binder/include/binder/Binder.h +++ b/libs/binder/include/binder/Binder.h @@ -68,6 +68,8 @@ public: // This must be called before the object is sent to another process. Not thread safe. void setExtension(const sp& extension); + pid_t getDebugPid(); + protected: virtual ~BBinder(); diff --git a/libs/binder/include/binder/IBinder.h b/libs/binder/include/binder/IBinder.h index 027e088be6..b12723446d 100644 --- a/libs/binder/include/binder/IBinder.h +++ b/libs/binder/include/binder/IBinder.h @@ -59,6 +59,7 @@ public: INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'), SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'), EXTENSION_TRANSACTION = B_PACK_CHARS('_', 'E', 'X', 'T'), + DEBUG_PID_TRANSACTION = B_PACK_CHARS('_', 'P', 'I', 'D'), // Corresponds to TF_ONE_WAY -- an asynchronous call. FLAG_ONEWAY = 0x00000001 @@ -129,6 +130,11 @@ public: */ status_t getExtension(sp* out); + /** + * Dump PID for a binder, for debugging. + */ + status_t getDebugPid(pid_t* outPid); + // NOLINTNEXTLINE(google-default-arguments) virtual status_t transact( uint32_t code, const Parcel& data, -- cgit v1.2.3-59-g8ed1b