diff --git a/tools/libxl/libxl_pci.c b/tools/libxl/libxl_pci.c
index 6f8f49c..3b707f3 100644
--- a/tools/libxl/libxl_pci.c
+++ b/tools/libxl/libxl_pci.c
@@ -86,7 +86,7 @@ int libxl__create_pci_backend(libxl__gc *gc, uint32_t domid,
     front = flexarray_make(gc, 16, 1);
     back = flexarray_make(gc, 16, 1);
 
-    LOG(DEBUG, "Creating pci backend");
+    LOGD(DEBUG, domid, "Creating pci backend");
 
     /* add pci device */
     libxl__device_from_pcidev(gc, domid, pcidev, &device);
@@ -141,7 +141,7 @@ static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d
 
     back = flexarray_make(gc, 16, 1);
 
-    LOG(DEBUG, "Adding new pci device to xenstore");
+    LOGD(DEBUG, domid, "Adding new pci device to xenstore");
     num = atoi(num_devs);
     libxl_create_pci_backend_device(gc, back, num, pcidev);
     flexarray_append_pair(back, "num_devs", GCSPRINTF("%d", num + 1));
@@ -207,7 +207,7 @@ static int libxl__device_pci_remove_xenstore(libxl__gc *gc, uint32_t domid, libx
 
     if (domtype == LIBXL_DOMAIN_TYPE_PV) {
         if (libxl__wait_for_backend(gc, be_path, GCSPRINTF("%d", XenbusStateConnected)) < 0) {
-            LOG(DEBUG, "pci backend at %s is not ready", be_path);
+            LOGD(DEBUG, domid, "pci backend at %s is not ready", be_path);
             return ERROR_FAIL;
         }
     }
@@ -222,7 +222,7 @@ static int libxl__device_pci_remove_xenstore(libxl__gc *gc, uint32_t domid, libx
         }
     }
     if (i == num) {
-        LOG(ERROR, "Couldn't find the device on xenstore");
+        LOGD(ERROR, domid, "Couldn't find the device on xenstore");
         return ERROR_INVAL;
     }
 
@@ -236,7 +236,7 @@ retry_transaction:
 
     if (domtype == LIBXL_DOMAIN_TYPE_PV) {
         if (libxl__wait_for_backend(gc, be_path, GCSPRINTF("%d", XenbusStateConnected)) < 0) {
-            LOG(DEBUG, "pci backend at %s is not ready", be_path);
+            LOGD(DEBUG, domid, "pci backend at %s is not ready", be_path);
             return ERROR_FAIL;
         }
     }
@@ -981,9 +981,9 @@ static int qemu_pci_add_xenstore(libxl__gc *gc, uint32_t domid,
     vdevfn = libxl__xs_read(gc, XBT_NULL, path);
     path = DEVICE_MODEL_XS_PATH(gc, dm_domid, domid, "/state");
     if ( rc < 0 )
-        LOG(ERROR, "qemu refused to add device: %s", vdevfn);
+        LOGD(ERROR, domid, "qemu refused to add device: %s", vdevfn);
     else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 ) {
-        LOG(ERROR, "wrong format for the vdevfn: '%s'", vdevfn);
+        LOGD(ERROR, domid, "wrong format for the vdevfn: '%s'", vdevfn);
         rc = -1;
     }
     xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
@@ -1000,6 +1000,8 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
     unsigned long long start, end, flags, size;
     int irq, i, rc, hvm = 0;
     uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED;
+    uint32_t domainid = domid;
+    bool isstubdom = !libxl_is_stubdom(ctx, domid, &domainid);
 
     if (type == LIBXL_DOMAIN_TYPE_INVALID)
         return ERROR_FAIL;
@@ -1031,7 +1033,7 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
     irq = 0;
 
     if (f == NULL) {
-        LOGE(ERROR, "Couldn't open %s", sysfs_path);
+        LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path);
         return ERROR_FAIL;
     }
     for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
@@ -1042,10 +1044,10 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
             if (flags & PCI_BAR_IO) {
                 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
                 if (rc < 0) {
-                    LOGE(ERROR,
-                         "Error: xc_domain_ioport_permission error 0x%llx/0x%llx",
-                         start,
-                         size);
+                    LOGED(ERROR, domainid,
+                          "Error: xc_domain_ioport_permission error 0x%llx/0x%llx",
+                          start,
+                          size);
                     fclose(f);
                     return ERROR_FAIL;
                 }
@@ -1053,10 +1055,10 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
                 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
                                                 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
                 if (rc < 0) {
-                    LOGE(ERROR,
-                         "Error: xc_domain_iomem_permission error 0x%llx/0x%llx",
-                         start,
-                         size);
+                    LOGED(ERROR, domainid,
+                          "Error: xc_domain_iomem_permission error 0x%llx/0x%llx",
+                          start,
+                          size);
                     fclose(f);
                     return ERROR_FAIL;
                 }
@@ -1068,19 +1070,19 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
                                 pcidev->bus, pcidev->dev, pcidev->func);
     f = fopen(sysfs_path, "r");
     if (f == NULL) {
-        LOGE(ERROR, "Couldn't open %s", sysfs_path);
+        LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path);
         goto out;
     }
     if ((fscanf(f, "%u", &irq) == 1) && irq) {
         rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
         if (rc < 0) {
-            LOGE(ERROR, "Error: xc_physdev_map_pirq irq=%d", irq);
+            LOGED(ERROR, domainid, "Error: xc_physdev_map_pirq irq=%d", irq);
             fclose(f);
             return ERROR_FAIL;
         }
         rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
         if (rc < 0) {
-            LOGE(ERROR, "Error: xc_domain_irq_permission irq=%d", irq);
+            LOGED(ERROR, domainid, "Error: xc_domain_irq_permission irq=%d", irq);
             fclose(f);
             return ERROR_FAIL;
         }
@@ -1091,22 +1093,22 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
     if (pcidev->permissive) {
         if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/permissive",
                              pcidev) < 0 ) {
-            LOG(ERROR, "Setting permissive for device");
+            LOGD(ERROR, domainid, "Setting permissive for device");
             return ERROR_FAIL;
         }
     }
 
 out:
-    if (!libxl_is_stubdom(ctx, domid, NULL)) {
+    if (!isstubdom) {
         if (pcidev->rdm_policy == LIBXL_RDM_RESERVE_POLICY_STRICT) {
             flag &= ~XEN_DOMCTL_DEV_RDM_RELAXED;
         } else if (pcidev->rdm_policy != LIBXL_RDM_RESERVE_POLICY_RELAXED) {
-            LOGE(ERROR, "unknown rdm check flag.");
+            LOGED(ERROR, domainid, "unknown rdm check flag.");
             return ERROR_FAIL;
         }
         rc = xc_assign_device(ctx->xch, domid, pcidev_encode_bdf(pcidev), flag);
         if (rc < 0 && (hvm || errno != ENOSYS)) {
-            LOGE(ERROR, "xc_assign_device failed");
+            LOGED(ERROR, domainid, "xc_assign_device failed");
             return ERROR_FAIL;
         }
     }
@@ -1130,27 +1132,27 @@ static int libxl__device_pci_reset(libxl__gc *gc, unsigned int domain, unsigned
         char *buf = GCSPRINTF(PCI_BDF, domain, bus, dev, func);
         rc = write(fd, buf, strlen(buf));
         if (rc < 0)
-            LOG(ERROR, "write to %s returned %d", reset, rc);
+            LOGD(ERROR, domain, "write to %s returned %d", reset, rc);
         close(fd);
         return rc < 0 ? rc : 0;
     }
     if (errno != ENOENT)
-        LOGE(ERROR, "Failed to access pciback path %s", reset);
+        LOGED(ERROR, domain, "Failed to access pciback path %s", reset);
     reset = GCSPRINTF("%s/"PCI_BDF"/reset", SYSFS_PCI_DEV, domain, bus, dev, func);
     fd = open(reset, O_WRONLY);
     if (fd >= 0) {
         rc = write(fd, "1", 1);
         if (rc < 0)
-            LOGE(ERROR, "write to %s returned %d", reset, rc);
+            LOGED(ERROR, domain, "write to %s returned %d", reset, rc);
         close(fd);
         return rc < 0 ? rc : 0;
     }
     if (errno == ENOENT) {
-        LOG(ERROR,
-            "The kernel doesn't support reset from sysfs for PCI device "PCI_BDF,
-            domain, bus, dev, func);
+        LOGD(ERROR, domain,
+             "The kernel doesn't support reset from sysfs for PCI device "PCI_BDF,
+             domain, bus, dev, func);
     } else {
-        LOGE(ERROR, "Failed to access reset path %s", reset);
+        LOGED(ERROR, domain, "Failed to access reset path %s", reset);
     }
     return -1;
 }
@@ -1202,11 +1204,11 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
     if (libxl__domain_type(gc, domid) == LIBXL_DOMAIN_TYPE_HVM) {
         rc = xc_test_assign_device(ctx->xch, domid, pcidev_encode_bdf(pcidev));
         if (rc) {
-            LOG(ERROR,
-                "PCI device %04x:%02x:%02x.%u %s?",
-                pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func,
-                errno == ENOSYS ? "cannot be assigned - no IOMMU"
-                : "already assigned to a different guest");
+            LOGD(ERROR, domid,
+                 "PCI device %04x:%02x:%02x.%u %s?",
+                 pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func,
+                 errno == ENOSYS ? "cannot be assigned - no IOMMU"
+                 : "already assigned to a different guest");
             goto out;
         }
     }
@@ -1221,21 +1223,21 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
     }
 
     if (!libxl_pcidev_assignable(ctx, pcidev)) {
-        LOG(ERROR, "PCI device %x:%x:%x.%x is not assignable",
-            pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
+        LOGD(ERROR, domid, "PCI device %x:%x:%x.%x is not assignable",
+             pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
         rc = ERROR_FAIL;
         goto out;
     }
 
     rc = get_all_assigned_devices(gc, &assigned, &num_assigned);
     if ( rc ) {
-        LOG(ERROR,
-            "cannot determine if device is assigned, refusing to continue");
+        LOGD(ERROR, domid,
+             "cannot determine if device is assigned, refusing to continue");
         goto out;
     }
     if ( is_pcidev_in_array(assigned, num_assigned, pcidev->domain,
                      pcidev->bus, pcidev->dev, pcidev->func) ) {
-        LOG(ERROR, "PCI device already attached to a domain");
+        LOGD(ERROR, domid, "PCI device already attached to a domain");
         rc = ERROR_FAIL;
         goto out;
     }
@@ -1255,7 +1257,7 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
 
     if ( pcidev->vfunc_mask == LIBXL_PCI_FUNC_ALL ) {
         if ( !(pcidev->vdevfn >> 3) ) {
-            LOG(ERROR, "Must specify a v-slot for multi-function devices");
+            LOGD(ERROR, domid, "Must specify a v-slot for multi-function devices");
             rc = ERROR_INVAL;
             goto out;
         }
@@ -1301,7 +1303,7 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
     for (i = 0; i < d_config->num_pcidevs; i++) {
         rc = libxl__device_pci_add(gc, domid, &d_config->pcidevs[i], 1);
         if (rc < 0) {
-            LOG(ERROR, "libxl_device_pci_add failed: %d", rc);
+            LOGD(ERROR, domid, "libxl_device_pci_add failed: %d", rc);
             goto out;
         }
     }
@@ -1310,7 +1312,7 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
-            LOG(ERROR, "libxl_create_pci_backend failed: %d", rc);
+            LOGD(ERROR, domid, "libxl_create_pci_backend failed: %d", rc);
             goto out;
         }
     }
@@ -1342,7 +1344,7 @@ static int qemu_pci_remove_xenstore(libxl__gc *gc, uint32_t domid,
         libxl__qemu_traditional_cmd(gc, domid, "pci-rem");
         if (libxl__wait_for_device_model_deprecated(gc, domid, "pci-removed",
                                          NULL, NULL, NULL) < 0) {
-            LOG(ERROR, "Device Model didn't respond in time");
+            LOGD(ERROR, domid, "Device Model didn't respond in time");
             /* This depends on guest operating system acknowledging the
              * SCI, if it doesn't respond in time then we may wish to
              * force the removal.
@@ -1367,6 +1369,9 @@ static int do_pci_remove(libxl__gc *gc, uint32_t domid,
     libxl_domain_type type = libxl__domain_type(gc, domid);
     int hvm = 0, rc, num;
     int stubdomid = 0;
+    uint32_t domainid = domid;
+    bool isstubdom = !libxl_is_stubdom(ctx, domid, &domainid);
+
 
     assigned = libxl_device_pci_list(ctx, domid, &num);
     if ( assigned == NULL )
@@ -1375,7 +1380,7 @@ static int do_pci_remove(libxl__gc *gc, uint32_t domid,
     rc = ERROR_INVAL;
     if ( !is_pcidev_in_array(assigned, num, pcidev->domain,
                       pcidev->bus, pcidev->dev, pcidev->func) ) {
-        LOG(ERROR, "PCI device not attached to this domain");
+        LOGD(ERROR, domainid, "PCI device not attached to this domain");
         goto out_fail;
     }
 
@@ -1412,7 +1417,7 @@ static int do_pci_remove(libxl__gc *gc, uint32_t domid,
         int i;
 
         if (f == NULL) {
-            LOGE(ERROR, "Couldn't open %s", sysfs_path);
+            LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path);
             goto skip1;
         }
         for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
@@ -1423,18 +1428,18 @@ static int do_pci_remove(libxl__gc *gc, uint32_t domid,
                 if (flags & PCI_BAR_IO) {
                     rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
                     if (rc < 0)
-                        LOGE(ERROR,
-                             "xc_domain_ioport_permission error 0x%x/0x%x",
-                             start,
-                             size);
+                        LOGED(ERROR, domainid,
+                              "xc_domain_ioport_permission error 0x%x/0x%x",
+                              start,
+                              size);
                 } else {
                     rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
                                                     (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
                     if (rc < 0)
-                        LOGE(ERROR,
-                             "xc_domain_iomem_permission error 0x%x/0x%x",
-                             start,
-                             size);
+                        LOGED(ERROR, domainid,
+                              "xc_domain_iomem_permission error 0x%x/0x%x",
+                              start,
+                              size);
                 }
             }
         }
@@ -1444,17 +1449,17 @@ skip1:
                                pcidev->bus, pcidev->dev, pcidev->func);
         f = fopen(sysfs_path, "r");
         if (f == NULL) {
-            LOGE(ERROR, "Couldn't open %s", sysfs_path);
+            LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path);
             goto out;
         }
         if ((fscanf(f, "%u", &irq) == 1) && irq) {
             rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
             if (rc < 0) {
-                LOGE(ERROR, "xc_physdev_unmap_pirq irq=%d", irq);
+                LOGED(ERROR, domainid, "xc_physdev_unmap_pirq irq=%d", irq);
             }
             rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
             if (rc < 0) {
-                LOGE(ERROR, "xc_domain_irq_permission irq=%d", irq);
+                LOGED(ERROR, domainid, "xc_domain_irq_permission irq=%d", irq);
             }
         }
         fclose(f);
@@ -1465,10 +1470,10 @@ out:
         libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
     }
 
-    if (!libxl_is_stubdom(ctx, domid, NULL)) {
+    if (!isstubdom) {
         rc = xc_deassign_device(ctx->xch, domid, pcidev_encode_bdf(pcidev));
         if (rc < 0 && (hvm || errno != ENOSYS))
-            LOGE(ERROR, "xc_deassign_device failed");
+            LOGED(ERROR, domainid, "xc_deassign_device failed");
     }
 
     stubdomid = libxl_get_stubdom_id(ctx, domid);
@@ -1655,17 +1660,17 @@ int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,
 
         FILE *f = fopen(pci_device_class_path, "r");
         if (!f) {
-            LOGE(ERROR,
-                 "pci device "PCI_BDF" does not have class attribute",
-                 pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
+            LOGED(ERROR, domid,
+                  "pci device "PCI_BDF" does not have class attribute",
+                  pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
             continue;
         }
         read_items = fscanf(f, "0x%lx\n", &pci_device_class);
         fclose(f);
         if (read_items != 1) {
-            LOGE(ERROR,
-                 "cannot read class of pci device "PCI_BDF,
-                 pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
+            LOGED(ERROR, domid,
+                  "cannot read class of pci device "PCI_BDF,
+                  pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
             continue;
         }
         if (pci_device_class != 0x030000) /* VGA class */
@@ -1675,20 +1680,20 @@ int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,
         ret = xc_domain_iomem_permission(CTX->xch, stubdom_domid,
                                          vga_iomem_start, 0x20, 1);
         if (ret < 0) {
-            LOGE(ERROR,
-                 "failed to give stubdom%d access to iomem range "
-                 "%"PRIx64"-%"PRIx64" for VGA passthru",
-                 stubdom_domid,
-                 vga_iomem_start, (vga_iomem_start + 0x20 - 1));
+            LOGED(ERROR, domid,
+                  "failed to give stubdom%d access to iomem range "
+                  "%"PRIx64"-%"PRIx64" for VGA passthru",
+                  stubdom_domid,
+                  vga_iomem_start, (vga_iomem_start + 0x20 - 1));
             return ret;
         }
         ret = xc_domain_iomem_permission(CTX->xch, domid,
                                          vga_iomem_start, 0x20, 1);
         if (ret < 0) {
-            LOGE(ERROR,
-                 "failed to give dom%d access to iomem range "
-                 "%"PRIx64"-%"PRIx64" for VGA passthru",
-                 domid, vga_iomem_start, (vga_iomem_start + 0x20 - 1));
+            LOGED(ERROR, domid,
+                  "failed to give dom%d access to iomem range "
+                  "%"PRIx64"-%"PRIx64" for VGA passthru",
+                  domid, vga_iomem_start, (vga_iomem_start + 0x20 - 1));
             return ret;
         }
         break;
