android system in various parts of the process to obtain the name of the method

Loading function DriverEntry the driver is to run in the System process. EPROCESS can get the address of the kernel structure of the System process by PsGetCurrentProcess, then began to look for "System" string from the address. Once found, is offset EPROCESS process name is stored. Get the process name, the process calls after the drive when you can get the current process name in the offset after the offset EPROCESS structure directly. code show as below:

DWORD GetProcessNameOffset()
{
    PEPROCESS curproc;
    DWORD procNameOffset;
    curproc = PsGetCurrentProcess();
    for(int i=0; i< 4096; i++)
    {
        if( !strncmp( "System", (PCHAR) curproc + i, strlen("System") ))
        {
           procNameOffset = i;
            return procNameOffset;
        }
    }
    return 0;
}

 

BOOL GetProcessName( PCHAR theName )
{
    PEPROCESS       curproc;
    char            *nameptr;
    ULONG           i;
    KIRQL           oldirql;

    if( gProcessNameOffset )
    {
        curproc = PsGetCurrentProcess();
        nameptr   = (PCHAR) curproc + ProcNameOffset;
        strncpy( theName, nameptr, NT_PROCNAMELEN );
        theName[NT_PROCNAMELEN] = 0; /**//* NULL at end */
        return TRUE;
    }
    return FALSE;
}

Android HAL layer, it can be used:

Android.mk 添加 
LOCAL_SHARED_LIBRARIES:= \ 
   libbinder

HAL层中.c文件中:
#include <errno.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <sys/types.h>

#include <hardware/lights.h>
#include <hardware/hardware.h>
#include <hardware/sunxi_display2.h>

#include <cutils/list.h>
#include <stdlib.h>
#include <sys/cdefs.h>
#include <cutils/properties.h>

#include <android/log.h>
#include <binder/IPCThreadState.h>
int fd = 0;
#define GET_CALLING_PID    (android::IPCThreadState::self()->getCallingPid())
void getCallingProcessName(char *name) 
{ 
   char proc_node[128]; 

   if (name == 0) 
   { 
       LOGE("error in params"); 
       return; 
   } 

   memset(proc_node, 0, sizeof(proc_node)); 
   sprintf(proc_node, "/proc/%d/cmdline", GET_CALLING_PID); 
   int fp = ::open(proc_node, O_RDONLY); 
   if (fp > 0) 
   { 
       memset(name, 0, 128); 
       ::read(fp, name, 128); 
       ::close(fp); 
       fp = 0; 
       LOGD("Calling process is: %s", name); 
   } 
   else 
   { 
       LOGE("Obtain calling process failed"); 
   } 
} 
char mProcessName[128]; 
   getCallingProcessName(mProcessName); 
   LOGD("openCameraDev calling_process = %s", mProcessName);

After the corresponding process can be seen in particular in logcat which processes the call, if the above procedure into light.cpp HAL, the use logcat -s lights to see what process is called.

Guess you like

Origin blog.csdn.net/jinron10/article/details/93843572