簡體   English   中英

自動生成返回碼檢查器的工具

[英]Tool for auto generating return code checker

我正在尋找一個生成子程序的工具,該子程序用於檢查其他子程序的返回碼。

例如, pthread_create可以返回0EAGAINEINVALEPERM代碼。 擁有這樣的檢查器將是很好的:

void pthread_create_check(int retcode) {
    switch (retcode) {
    case 0:
        printf("pthread_create success.\n");
        break;
    case EAGAIN:
        printf("pthread_create EAGAIN error: insufficient resources"
               " to create another thread, or a system-imposed"
               " limit on the number of threads was encountered.\n");
        break;
    case EINVAL:
        printf("pthread_create EINVAL error: invalid settings in"
               " attr.\n");
        break;
    case EPERM:
        printf("pthread_create EPERM error: no permission to set the"
               " scheduling policy and parameters specified in"
               " attr.\n");
        break;
    }
}

並以這種方式使用它:

iret = pthread_create(&thread_desc, 
                      NULL, 
                      thread_function, 
                      (void *) thread_param);
pthread_create_check(iret);

手冊頁中有每個錯誤代碼的說明。 創建這樣的檢查器只不過是復制粘貼錯誤代碼和手冊頁中的說明。 我認為計算機可以比人類做得更好,因為計算機永不疲倦。 另外,對於每個子例程調用我也不願意這樣做。 有自動化工具嗎?

只需創建消息表即可。 它將節省編碼時間和空間。

typedef struct pthread_message {
    int code;
    const char* text;
} pthread_message;

int pthread_check(int retcode, const char* fname, 
    pthread_message* messages)
{
    if(!retcode) /* Makes the common case fast. */
    {
        fprintf(stderr, "%s success.\n", fname);
        return retcode;
    }

    /* Look for a message. */
    for(; messages->code; ++messages)
        if(messages->code == retcode)
        {
            fprintf(stderr, "%s %s\n", fname, message->text);
            return retcode;
        }

    /* Fall back on standard library. If you lack strerror_r, 
       then put a generic message here.
    */
    char buf[256];
    fprintf(stderr, "%s %s\n", fname, strerror_r(retcode, buf, 256));
    return retcode;
);

pthread_message pthread_create_messages[] = {
    { EAGAIN, "EAGAIN error: insufficient resources to create another thread,"
      " or a system-imposed limit on the number of threads was encountered." },
    { EINVAL, "EINVAL error: invalid settings in attr." },
    { EPERM, "EPERM error: no permission to set the scheduling policy and"
      " parameters specified in attr." },
    { 0, 0 } /* End of list. */
};

iret = pthread_check(pthread_create(arg1, arg2, ...), "pthread_create", 
    pthread_create_messages);

沒有什么能阻止您在函數之間共享消息列表,因此您可以編寫任意數量的內容。

如果您瘋了,可以退出通話宏:

#define PTHREAD_CHECK(fname, arglist) \
    (pthread_check(fname arglist, #fname, fname##_messages))

iret = PTHREAD_CHECK(pthread_create, (arg1, arg2, ...));

在這種情況下,共享消息列表意味着您需要為每個附加函數創建一個具有正確名稱的指針,該指針指向第一個函數的列表。 還有很多工作要做。

作為記錄,我只用通用消息(成功消息除外,它們是垃圾郵件)編寫了一個check函數,並在我的C ++包裝器中的pthread處使​​用了該函數。 (不要在十年前對我提起Boost了。)

Valgrind的Helgrind可以檢測到POSIX pthreads API的濫用。

http://valgrind.org/docs/manual/hg-manual.html#hg-manual.api-checks

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM