Check gBufMgr in _tbm_bo_valid()
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index 702b709..0785ba1 100644 (file)
@@ -40,10 +40,16 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 int bDebug;
 #endif
 
+#ifdef TRACE
+int bTrace;
+#endif
+
 #ifdef HAVE_DLOG
 int bDlog;
 #endif
 
+int b_dump_queue;
+
 #define PREFIX_LIB    "libtbm_"
 #define SUFFIX_LIB    ".so"
 #define DEFAULT_LIB   PREFIX_LIB"default"SUFFIX_LIB
@@ -77,6 +83,63 @@ _tbm_set_last_result(tbm_error_e err)
        tbm_last_error = err;
 }
 
+char * tbm_flag_to_str(int f)
+{
+       static char str[255];
+       int c = 0;
+       if (f == TBM_BO_DEFAULT)
+                snprintf(str, 255, "DEFAULT\n");
+       else {
+               if (f & TBM_BO_SCANOUT)
+                       c = snprintf(&str[c], 255, "SCANOUT,");
+               if (f & TBM_BO_NONCACHABLE)
+                       c = snprintf(&str[c], 255, "NONCACHABLE,");
+               if (f & TBM_BO_WC)
+                       c = snprintf(&str[c], 255, "WC");
+       }
+       return str;
+}
+
+/* LCOV_EXCL_START */
+static int last_chk_bo_cnt = 0;
+static void
+_tbm_util_check_bo_cnt(tbm_bufmgr bufmgr)
+{
+       if (bufmgr->bo_cnt >= 500 && ((bufmgr->bo_cnt % 20) == 0)) {
+               if (bufmgr->bo_cnt > last_chk_bo_cnt) {
+                       TBM_DEBUG("============TBM BO CNT DEBUG: bo_cnt=%d\n", bufmgr->bo_cnt);
+                       tbm_bufmgr_debug_show(bufmgr);
+                       last_chk_bo_cnt = bufmgr->bo_cnt;
+               }
+       }
+}
+
+static int
+_tbm_util_get_max_surface_size(int * w, int * h)
+{
+       int count = 0;
+       tbm_surface_h surface = NULL, tmp = NULL;
+       tbm_surface_info_s info;
+
+       *w = 0;
+       *h = 0;
+
+       if (gBufMgr == NULL)
+               return count;
+
+       if (!LIST_IS_EMPTY(&gBufMgr->surf_list)) {
+               LIST_FOR_EACH_ENTRY_SAFE(surface, tmp, &gBufMgr->surf_list, item_link) {
+                       if (tbm_surface_get_info(surface, &info) == TBM_SURFACE_ERROR_NONE) {
+                               count++;
+                               if (*w < info.width) *w = info.width;
+                               if (*h < info.height) *h = info.height;
+                       }
+               }
+       }
+
+       return count;
+}
+
 static void
 _tbm_util_get_appname_brief(char *brief)
 {
@@ -128,6 +191,7 @@ _tbm_util_get_appname_from_pid(long pid, char *str)
 
        snprintf(str, sizeof(cmdline), "%s", cmdline);
 }
+/* LCOV_EXCL_STOP */
 
 tbm_user_data
 *user_data_lookup(struct list_head *user_data_list, unsigned long key)
@@ -180,11 +244,10 @@ _bo_lock(tbm_bo bo, int device, int opt)
        tbm_bufmgr bufmgr = bo->bufmgr;
        int ret = 0;
 
-       if (bufmgr->backend->bo_lock) {
+       if (bufmgr->backend->bo_lock)
                ret = bufmgr->backend->bo_lock(bo, device, opt);
-       } else {
+       else
                ret = 1;
-       }
 
        return ret;
 }
@@ -194,9 +257,8 @@ _bo_unlock(tbm_bo bo)
 {
        tbm_bufmgr bufmgr = bo->bufmgr;
 
-       if (bufmgr->backend->bo_unlock) {
+       if (bufmgr->backend->bo_unlock)
                bufmgr->backend->bo_unlock(bo);
-       }
 }
 
 static int
@@ -295,6 +357,11 @@ _tbm_bo_is_valid(tbm_bo bo)
        if (bo == NULL)
                return 0;
 
+       if (gBufMgr == NULL) {
+               TBM_LOG_E("error tbm_bufmgr was deinited\n");
+               return 0;
+       }
+
        if (!LIST_IS_EMPTY(&gBufMgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &gBufMgr->bo_list, item_link) {
                        if (old_data == bo)
@@ -343,10 +410,13 @@ _tbm_bo_unref(tbm_bo bo)
                LIST_DEL(&bo->item_link);
                free(bo);
                bo = NULL;
+
+               bufmgr->bo_cnt--;
        }
 
 }
 
+/* LCOV_EXCL_START */
 static int
 _check_version(TBMModuleVersionInfo *data)
 {
@@ -479,6 +549,7 @@ _tbm_load_module(tbm_bufmgr bufmgr, int fd)
 
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
 tbm_bufmgr
 tbm_bufmgr_init(int fd)
@@ -487,13 +558,14 @@ tbm_bufmgr_init(int fd)
 
        pthread_mutex_lock(&gLock);
 
+       /* LCOV_EXCL_START */
 #ifdef HAVE_DLOG
        env = getenv("TBM_DLOG");
        if (env) {
                bDlog = atoi(env);
                TBM_LOG_D("TBM_DLOG=%s\n", env);
        } else {
-               bDlog = 0;
+               bDlog = 1;
        }
 #endif
 
@@ -507,12 +579,21 @@ tbm_bufmgr_init(int fd)
        }
 #endif
 
+#ifdef TRACE
+       env = getenv("TBM_TRACE");
+       if (env) {
+               bTrace = atoi(env);
+               TBM_LOG_D("TBM_TRACE=%s\n", env);
+       } else {
+               bTrace = 0;
+       }
+#endif
+       /* LCOV_EXCL_STOP */
+
        /* initialize buffer manager */
        if (gBufMgr) {
                gBufMgr->ref_count++;
-
-               DBG("bufmgr:%p ref: fd=%d, ref_count:%d\n",
-                   gBufMgr, gBufMgr->fd, gBufMgr->ref_count);
+               TBM_TRACE("reuse  tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, gBufMgr->fd);
                pthread_mutex_unlock(&gLock);
                return gBufMgr;
        }
@@ -523,6 +604,7 @@ tbm_bufmgr_init(int fd)
        gBufMgr = calloc(1, sizeof(struct _tbm_bufmgr));
        if (!gBufMgr) {
                _tbm_set_last_result(TBM_BO_ERROR_HEAP_ALLOC_FAILED);
+               TBM_TRACE("error: fail to alloc bufmgr fd(%d)\n", fd);
                pthread_mutex_unlock(&gLock);
                return NULL;
        }
@@ -531,13 +613,14 @@ tbm_bufmgr_init(int fd)
 
        /* load bufmgr priv from env */
        if (!_tbm_load_module(gBufMgr, gBufMgr->fd)) {
+               /* LCOV_EXCL_START */
                _tbm_set_last_result(TBM_BO_ERROR_LOAD_MODULE_FAILED);
                TBM_LOG_E("error : Fail to load bufmgr backend\n");
-
                free(gBufMgr);
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        /* log for tbm backend_flag */
@@ -550,6 +633,7 @@ tbm_bufmgr_init(int fd)
            gBufMgr, gBufMgr->ref_count);
 
        if (pthread_mutex_init(&gBufMgr->lock, NULL) != 0) {
+               /* LCOV_EXCL_START */
                _tbm_set_last_result(TBM_BO_ERROR_THREAD_INIT_FAILED);
                gBufMgr->backend->bufmgr_deinit(gBufMgr->backend->priv);
                tbm_backend_free(gBufMgr->backend);
@@ -558,6 +642,7 @@ tbm_bufmgr_init(int fd)
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        /* setup the lock_type */
@@ -574,12 +659,17 @@ tbm_bufmgr_init(int fd)
        DBG("BUFMGR_LOCK_TYPE=%s\n",
            env ? env : "default:once");
 
+       TBM_TRACE("create tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, fd);
+
        /* intialize bo_list */
        LIST_INITHEAD(&gBufMgr->bo_list);
 
        /* intialize surf_list */
        LIST_INITHEAD(&gBufMgr->surf_list);
 
+       /* intialize debug_key_list */
+       LIST_INITHEAD(&gBufMgr->debug_key_list);
+
        pthread_mutex_unlock(&gLock);
        return gBufMgr;
 }
@@ -597,10 +687,15 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        pthread_mutex_lock(&gLock);
 
+       if (!gBufMgr) {
+               TBM_LOG_E("gBufmgr already destroy: bufmgr:%p\n", bufmgr);
+               pthread_mutex_unlock(&gLock);
+               return;
+       }
+
        bufmgr->ref_count--;
        if (bufmgr->ref_count > 0) {
-               DBG("tizen bufmgr destroy: bufmgr:%p, ref_count:%d\n",
-                       bufmgr, bufmgr->ref_count);
+               TBM_TRACE("reduce a ref_count(%d) of tbm_bufmgr(%p)\n", bufmgr->ref_count, bufmgr);
                pthread_mutex_unlock(&gLock);
                return;
        }
@@ -632,8 +727,7 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        pthread_mutex_destroy(&bufmgr->lock);
 
-       DBG("tizen bufmgr destroy: bufmgr:%p\n",
-           bufmgr);
+       TBM_TRACE("destroy tbm_bufmgr(%p)\n", bufmgr);
 
        dlclose(bufmgr->module_data);
 
@@ -659,6 +753,8 @@ tbm_bo_size(tbm_bo bo)
 
        size = bufmgr->backend->bo_size(bo);
 
+       TBM_TRACE("bo(%p) size(%d)\n", bo, size);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return size;
@@ -675,6 +771,8 @@ tbm_bo_ref(tbm_bo bo)
 
        _tbm_bo_ref(bo);
 
+       TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return bo;
@@ -689,6 +787,8 @@ tbm_bo_unref(tbm_bo bo)
 
        pthread_mutex_lock(&bufmgr->lock);
 
+       TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt - 1);
+
        _tbm_bo_unref(bo);
 
        pthread_mutex_unlock(&bufmgr->lock);
@@ -704,16 +804,21 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 
        bo = calloc(1, sizeof(struct _tbm_bo));
        if (!bo) {
+               TBM_TRACE("error: fail to create of tbm_bo size(%d) flag(%s)\n", size, tbm_flag_to_str(flags));
                _tbm_set_last_result(TBM_BO_ERROR_HEAP_ALLOC_FAILED);
                return NULL;
        }
 
+       _tbm_util_check_bo_cnt(bufmgr);
+       bufmgr->bo_cnt++;
+
        bo->bufmgr = bufmgr;
 
        pthread_mutex_lock(&bufmgr->lock);
 
        bo_priv = bufmgr->backend->bo_alloc(bo, size, flags);
        if (!bo_priv) {
+               TBM_TRACE("error: fail to create of tbm_bo size(%d) flag(%s)\n", size, tbm_flag_to_str(flags));
                _tbm_set_last_result(TBM_BO_ERROR_BO_ALLOC_FAILED);
                free(bo);
                pthread_mutex_unlock(&bufmgr->lock);
@@ -724,6 +829,8 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
        bo->flags = flags;
        bo->priv = bo_priv;
 
+       TBM_TRACE("bo(%p) size(%d) refcnt(%d), flag(%s)\n", bo, size, bo->ref_cnt, tbm_flag_to_str(bo->flags));
+
        LIST_INITHEAD(&bo->user_data_list);
 
        LIST_ADD(&bo->item_link, &bufmgr->bo_list);
@@ -743,18 +850,24 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
        tbm_bo tmp = NULL;
        void *bo_priv = NULL;
 
+       _tbm_util_check_bo_cnt(bufmgr);
+
        pthread_mutex_lock(&bufmgr->lock);
 
        bo = calloc(1, sizeof(struct _tbm_bo));
        if (!bo) {
+               TBM_TRACE("error: fail to import of tbm_bo by key(%d)\n", key);
                pthread_mutex_unlock(&bufmgr->lock);
                return NULL;
        }
 
+       bufmgr->bo_cnt++;
+
        bo->bufmgr = bufmgr;
 
        bo_priv = bufmgr->backend->bo_import(bo, key);
        if (!bo_priv) {
+               TBM_TRACE("error: fail to import of tbm_bo by key(%d)\n", key);
                _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FAILED);
                free(bo);
                pthread_mutex_unlock(&bufmgr->lock);
@@ -764,9 +877,8 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
                        if (bo2->priv == bo_priv) {
-                               DBG("find bo(%p, ref:%d key:%d) in list\n",
-                                   bo2, bo2->ref_cnt, key);
-
+                               TBM_TRACE("find bo(%p) ref(%d) key(%d) flag(%s) in list\n",
+                                                       bo2, bo2->ref_cnt, key, tbm_flag_to_str(bo2->flags));
                                bo2->ref_cnt++;
                                free(bo);
                                pthread_mutex_unlock(&bufmgr->lock);
@@ -783,6 +895,9 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
        else
                bo->flags = TBM_BO_DEFAULT;
 
+       TBM_TRACE("import new bo(%p) ref(%d) key(%d) flag(%s) in list\n",
+                         bo, bo->ref_cnt, key, tbm_flag_to_str(bo->flags));
+
        LIST_INITHEAD(&bo->user_data_list);
 
        LIST_ADD(&bo->item_link, &bufmgr->bo_list);
@@ -802,18 +917,24 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        tbm_bo tmp = NULL;
        void *bo_priv = NULL;
 
+       _tbm_util_check_bo_cnt(bufmgr);
+
        pthread_mutex_lock(&bufmgr->lock);
 
        bo = calloc(1, sizeof(struct _tbm_bo));
        if (!bo) {
+               TBM_TRACE("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
                pthread_mutex_unlock(&bufmgr->lock);
                return NULL;
        }
 
+       bufmgr->bo_cnt++;
+
        bo->bufmgr = bufmgr;
 
        bo_priv = bufmgr->backend->bo_import_fd(bo, fd);
        if (!bo_priv) {
+               TBM_TRACE("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
                _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FD_FAILED);
                free(bo);
                pthread_mutex_unlock(&bufmgr->lock);
@@ -823,9 +944,8 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
                        if (bo2->priv == bo_priv) {
-                               DBG("find bo(%p, ref:%d, fd:%d) in list\n",
-                                   bo2, bo2->ref_cnt, fd);
-
+                               TBM_TRACE("find bo(%p) ref(%d) fd(%d) flag(%s) in list\n",
+                                                       bo2, bo2->ref_cnt, fd, tbm_flag_to_str(bo2->flags));
                                bo2->ref_cnt++;
                                free(bo);
                                pthread_mutex_unlock(&bufmgr->lock);
@@ -842,6 +962,9 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        else
                bo->flags = TBM_BO_DEFAULT;
 
+       TBM_TRACE("import bo(%p) ref(%d) fd(%d) flag(%s)in list\n",
+                               bo, bo->ref_cnt, fd, tbm_flag_to_str(bo->flags));
+
        LIST_INITHEAD(&bo->user_data_list);
 
        LIST_ADD(&bo->item_link, &bufmgr->bo_list);
@@ -862,12 +985,17 @@ tbm_bo_export(tbm_bo bo)
        bufmgr = bo->bufmgr;
 
        pthread_mutex_lock(&bufmgr->lock);
+
        ret = bufmgr->backend->bo_export(bo);
        if (!ret) {
                _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FAILED);
+               TBM_TRACE("error: bo(%p) tbm_key(%d)\n", bo, ret);
                pthread_mutex_unlock(&bufmgr->lock);
                return ret;
        }
+
+       TBM_TRACE("bo(%p) tbm_key(%d)\n", bo, ret);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return ret;
@@ -884,12 +1012,17 @@ tbm_bo_export_fd(tbm_bo bo)
        bufmgr = bo->bufmgr;
 
        pthread_mutex_lock(&bufmgr->lock);
+
        ret = bufmgr->backend->bo_export_fd(bo);
        if (ret < 0) {
                _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FD_FAILED);
+               TBM_TRACE("error: bo(%p) tbm_fd(%d)\n", bo, ret);
                pthread_mutex_unlock(&bufmgr->lock);
                return ret;
        }
+
+       TBM_TRACE("bo(%p) tbm_fd(%d)\n", bo, ret);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return ret;
@@ -906,12 +1039,17 @@ tbm_bo_get_handle(tbm_bo bo, int device)
        bufmgr = bo->bufmgr;
 
        pthread_mutex_lock(&bufmgr->lock);
+
        bo_handle = bufmgr->backend->bo_get_handle(bo, device);
        if (bo_handle.ptr == NULL) {
                _tbm_set_last_result(TBM_BO_ERROR_GET_HANDLE_FAILED);
+               TBM_TRACE("error: bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
                pthread_mutex_unlock(&bufmgr->lock);
                return (tbm_bo_handle) NULL;
        }
+
+       TBM_TRACE("bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return bo_handle;
@@ -931,8 +1069,7 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
 
        if (!_tbm_bo_lock(bo, device, opt)) {
                _tbm_set_last_result(TBM_BO_ERROR_LOCK_FAILED);
-               TBM_LOG_E("error fail to lock bo:%p)\n",
-                       bo);
+               TBM_TRACE("error: fail to lock bo:%p)\n", bo);
                pthread_mutex_unlock(&bufmgr->lock);
                return (tbm_bo_handle) NULL;
        }
@@ -940,9 +1077,7 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
        bo_handle = bufmgr->backend->bo_map(bo, device, opt);
        if (bo_handle.ptr == NULL) {
                _tbm_set_last_result(TBM_BO_ERROR_MAP_FAILED);
-               TBM_LOG_E("error fail to map bo:%p\n",
-                       bo);
-
+               TBM_TRACE("error: fail to map bo:%p\n", bo);
                _tbm_bo_unlock(bo);
                pthread_mutex_unlock(&bufmgr->lock);
                return (tbm_bo_handle) NULL;
@@ -951,6 +1086,8 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
        /* increase the map_count */
        bo->map_cnt++;
 
+       TBM_TRACE("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return bo_handle;
@@ -970,7 +1107,7 @@ tbm_bo_unmap(tbm_bo bo)
 
        ret = bufmgr->backend->bo_unmap(bo);
        if (!ret) {
-
+               TBM_TRACE("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
                _tbm_set_last_result(TBM_BO_ERROR_UNMAP_FAILED);
                pthread_mutex_unlock(&bufmgr->lock);
                return ret;
@@ -979,6 +1116,8 @@ tbm_bo_unmap(tbm_bo bo)
        /* decrease the map_count */
        bo->map_cnt--;
 
+       TBM_TRACE("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
+
        _tbm_bo_unlock(bo);
 
        pthread_mutex_unlock(&bufmgr->lock);
@@ -996,12 +1135,17 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
 
        pthread_mutex_lock(&bo1->bufmgr->lock);
 
+       TBM_TRACE("before: bo1(%p) bo2(%p)\n", bo1, bo2);
+
        if (bo1->bufmgr->backend->bo_size(bo1) != bo2->bufmgr->backend->bo_size(bo2)) {
                _tbm_set_last_result(TBM_BO_ERROR_SWAP_FAILED);
-               pthread_mutex_unlock(&bo1->bufmgr->lock);
+               TBM_TRACE("error: bo1(%p) bo2(%p)\n", bo1, bo2);
+       pthread_mutex_unlock(&bo1->bufmgr->lock);
                return 0;
        }
 
+       TBM_TRACE("after: bo1(%p) bo2(%p)\n", bo1, bo2);
+
        temp = bo1->priv;
        bo1->priv = bo2->priv;
        bo2->priv = temp;
@@ -1020,16 +1164,21 @@ tbm_bo_locked(tbm_bo bo)
 
        bufmgr = bo->bufmgr;
 
-       if (bufmgr->lock_type == LOCK_TRY_NEVER)
+       if (bufmgr->lock_type == LOCK_TRY_NEVER) {
+               TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
                return 0;
+       }
 
        pthread_mutex_lock(&bufmgr->lock);
 
+
        if (bo->lock_cnt > 0) {
+               TBM_TRACE("error: bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
                pthread_mutex_unlock(&bufmgr->lock);
                return 1;
        }
 
+       TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
        pthread_mutex_unlock(&bufmgr->lock);
 
        return 0;
@@ -1046,14 +1195,17 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
        /* check if the data according to the key exist if so, return false. */
        data = user_data_lookup(&bo->user_data_list, key);
        if (data) {
-               TBM_LOG_W("waring user data already exist. key:%ld\n",
-                       key);
+               TBM_TRACE("warning: user data already exist key(%ld)\n", key);
                return 0;
        }
 
        data = user_data_create(key, data_free_func);
-       if (!data)
+       if (!data) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                return 0;
+       }
+
+       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, data->data);
 
        LIST_ADD(&data->item_link, &bo->user_data_list);
 
@@ -1067,18 +1219,24 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
 
        tbm_user_data *old_data;
 
-       if (LIST_IS_EMPTY(&bo->user_data_list))
+       if (LIST_IS_EMPTY(&bo->user_data_list)) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                return 0;
+       }
 
        old_data = user_data_lookup(&bo->user_data_list, key);
-       if (!old_data)
+       if (!old_data) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                return 0;
+       }
 
        if (old_data->data && old_data->free_func)
                old_data->free_func(old_data->data);
 
        old_data->data = data;
 
+       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
+
        return 1;
 }
 
@@ -1089,17 +1247,22 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
 
        tbm_user_data *old_data;
 
-       if (!data || LIST_IS_EMPTY(&bo->user_data_list))
+       if (!data || LIST_IS_EMPTY(&bo->user_data_list)) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                return 0;
+       }
 
        old_data = user_data_lookup(&bo->user_data_list, key);
        if (!old_data) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                *data = NULL;
                return 0;
        }
 
        *data = old_data->data;
 
+       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
+
        return 1;
 }
 
@@ -1110,38 +1273,32 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
 
        tbm_user_data *old_data = (void *)0;
 
-       if (LIST_IS_EMPTY(&bo->user_data_list))
+       if (LIST_IS_EMPTY(&bo->user_data_list)) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                return 0;
+       }
 
        old_data = user_data_lookup(&bo->user_data_list, key);
-       if (!old_data)
+       if (!old_data) {
+               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
                return 0;
+       }
+
+       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
 
        user_data_delete(old_data);
 
        return 1;
 }
 
-tbm_error_e
-tbm_get_last_error(void)
-{
-       return tbm_last_error;
-}
-
 unsigned int
 tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
 {
        TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0);
 
-       unsigned int capability = TBM_BUFMGR_CAPABILITY_NONE;
+       TBM_TRACE("tbm_bufmgr(%p) capability(%d)\n", bufmgr, bufmgr->capabilities);
 
-       if (bufmgr->backend->bo_import && bufmgr->backend->bo_export)
-               capability |= TBM_BUFMGR_CAPABILITY_SHARE_KEY;
-
-       if (bufmgr->backend->bo_import_fd && bufmgr->backend->bo_export_fd)
-               capability |= TBM_BUFMGR_CAPABILITY_SHARE_FD;
-
-       return capability;
+       return bufmgr->capabilities;
 }
 
 int
@@ -1149,9 +1306,18 @@ tbm_bo_get_flags(tbm_bo bo)
 {
        TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
+       TBM_TRACE("bo(%p)\n", bo);
+
        return bo->flags;
 }
 
+/* LCOV_EXCL_START */
+tbm_error_e
+tbm_get_last_error(void)
+{
+       return tbm_last_error;
+}
+
 void
 tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
 {
@@ -1164,6 +1330,9 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
        int i;
        char app_name[255] = {0,};
        unsigned int pid = 0;
+       char title[255] = {0,};
+       char data[255] = {0,};
+       tbm_surface_debug_data *debug_old_data = NULL, *debug_tmp = NULL;
 
        pthread_mutex_lock(&gLock);
 
@@ -1173,11 +1342,20 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
        TBM_DEBUG("============TBM DEBUG: %s(%d)===========================\n",
                  app_name, getpid());
        memset(app_name, 0x0, 255 * sizeof(char));
+       snprintf(title, 255, "%s", "no  surface     refcnt  width  height  bpp  size    n_b  n_p  flags  format    app_name       ");
+       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
+               LIST_FOR_EACH_ENTRY_SAFE(debug_old_data, debug_tmp, &bufmgr->debug_key_list, item_link) {
+                       strncat(title, "  ", 2);
+                       strncat(title, debug_old_data->key, strlen(debug_old_data->key) + 1);
+               }
+       }
 
        TBM_DEBUG("[tbm_surface information]\n");
-       TBM_DEBUG("no  surface              refcnt  width  height  bpp  size      num_bos num_planes flags format              app_name\n");
+       TBM_DEBUG("%s\n", title);
        /* show the tbm_surface information in surf_list */
        if (!LIST_IS_EMPTY(&bufmgr->surf_list)) {
+               char *value = NULL;
+
                LIST_FOR_EACH_ENTRY_SAFE(surf, tmp_surf, &bufmgr->surf_list, item_link) {
                        pid = _tbm_surface_internal_get_debug_pid(surf);
                        if (!pid) {
@@ -1188,7 +1366,7 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
                        _tbm_util_get_appname_from_pid(pid, app_name);
                        _tbm_util_get_appname_brief(app_name);
 
-                       TBM_DEBUG("%-4d%-23p%-6d%-7d%-8d%-5d%-12d%-10d%-9d%-4d%-20s%s\n",
+                       snprintf(data, 255, "%-2d  %-9p    %-4d  %-5d  %-6d  %-3d  %-6d   %-2d   %-2d    %-3d  %-8s  %-15s",
                                  ++surf_cnt,
                                  surf,
                                  surf->refcnt,
@@ -1199,9 +1377,22 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
                                  surf->num_bos,
                                  surf->num_planes,
                                  surf->flags,
-                                 _tbm_surface_internal_format_to_str(surf->info.format),
+                                 _tbm_surface_internal_format_to_str(surf->info.format) + 11,
                                  app_name);
 
+                       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
+                               LIST_FOR_EACH_ENTRY_SAFE(debug_old_data, debug_tmp, &bufmgr->debug_key_list, item_link) {
+                                       strncat(data, "  ", 2);
+
+                                       value = _tbm_surface_internal_get_debug_data(surf, debug_old_data->key);
+                                       if (value)
+                                               strncat(data, value, strlen(value) + 1);
+                                       else
+                                               strncat(data, "none", strlen("none") + 1);
+                               }
+                       }
+                       TBM_DEBUG("%s\n", data);
+
                        for (i = 0; i < surf->num_bos; i++) {
                                TBM_DEBUG(" bo:%-12p  %-26d%-10d\n",
                                          surf->bos[i],
@@ -1217,12 +1408,12 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
        TBM_DEBUG("\n");
 
        TBM_DEBUG("[tbm_bo information]\n");
-       TBM_DEBUG("no  bo                   refcnt  size     lock_cnt map_cnt flags surface\n");
+       TBM_DEBUG("no  bo          refcnt  size    lock_cnt  map_cnt  flags  surface\n");
 
        /* show the tbm_bo information in bo_list */
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(bo, tmp_bo, &bufmgr->bo_list, item_link) {
-                       TBM_DEBUG("%-4d%-11p   %-6d%-12d%-9d%-9d%-4d%-11p\n",
+                       TBM_DEBUG("%-4d%-11p   %-4d  %-6d     %-5d     %-4d    %-3d  %-11p\n",
                                  ++bo_cnt,
                                  bo,
                                  bo->ref_cnt,
@@ -1246,11 +1437,76 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
 void
 tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
 {
+#ifdef TRACE
        TBM_LOG_D("bufmgr=%p onoff=%d\n", bufmgr, onoff);
-       TBM_LOG_D("Not implemented yet.\n");
+       bTrace = onoff;
+#endif
+}
+
+int
+tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
+{
+       TBM_LOG_D("path=%s count=%d onoff=%d\n", path, count, onoff);
+
+       if (onoff == 1) {
+
+               TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+
+               int w = 0, h = 0;
+               if (_tbm_util_get_max_surface_size(&w, &h) == 0) {
+                       TBM_LOG_I("No tbm_surface.\n");
+                       return 0;
+               }
+
+               tbm_surface_internal_dump_start(path, w, h, count);
+               b_dump_queue = 1;
+
+       } else if (onoff == 0) {
+
+               tbm_surface_internal_dump_end();
+               b_dump_queue = 0;
+
+       } else {
+               return 0;
+       }
+
+       return 1;
+}
+
+int
+tbm_bufmgr_debug_dump_all(char *path)
+{
+       TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+
+       TBM_LOG_D("path=%s\n", path);
+       int w = 0, h = 0, count = 0;
+       tbm_surface_h surface = NULL, tmp = NULL;
+
+       count = _tbm_util_get_max_surface_size(&w, &h);
+       if (count == 0) {
+               TBM_LOG_I("No tbm_surface.\n");
+               return 1;
+       }
+
+       tbm_surface_internal_dump_start(path, w, h, count);
+
+       LIST_FOR_EACH_ENTRY_SAFE(surface, tmp, &gBufMgr->surf_list, item_link) {
+               tbm_surface_internal_dump_buffer(surface, "dump_all");
+       }
+
+       tbm_surface_internal_dump_end();
+
+       return 1;
+
 }
 
 /* internal function */
+tbm_bufmgr
+_tbm_bufmgr_get_bufmgr(void)
+{
+       return gBufMgr;
+}
+
 int
 _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface)
 {
@@ -1271,18 +1527,22 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay)
        pthread_mutex_lock(&bufmgr->lock);
 
        if (!bufmgr->backend->bufmgr_bind_native_display) {
+               TBM_TRACE("error: tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
                pthread_mutex_unlock(&bufmgr->lock);
                return 1;
        }
 
        ret = bufmgr->backend->bufmgr_bind_native_display(bufmgr, NativeDisplay);
        if (!ret) {
+               TBM_TRACE("error: tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
                pthread_mutex_unlock(&bufmgr->lock);
                return 0;
        }
 
+       TBM_TRACE("tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
+
        pthread_mutex_unlock(&bufmgr->lock);
 
        return 1;
 }
-
+/* LCOV_EXCL_STOP */