Skip to content

Commit 5314433

Browse files
pan-applepull[bot]
authored andcommitted
Add logs to debug Darwin CI failures (#11005)
* Debug failures in Darwin CI * Formalize PR by adding ChipLogByteSpan
1 parent b174eba commit 5314433

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/lib/support/logging/CHIPLogging.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <lib/core/CHIPCore.h>
2929
#include <lib/support/CodeUtils.h>
3030
#include <lib/support/DLLUtil.h>
31+
#include <lib/support/Span.h>
3132

3233
#include <stdarg.h>
3334
#include <stdio.h>
@@ -142,6 +143,37 @@ DLL_EXPORT void Log(uint8_t module, uint8_t category, const char * msg, ...)
142143
va_end(v);
143144
}
144145

146+
DLL_EXPORT void LogByteSpan(uint8_t module, uint8_t category, const chip::ByteSpan & span)
147+
{
148+
// Maximum number of characters needed to print 8 byte buffer including formatting (0x)
149+
// 8 bytes * (2 nibbles per byte + 4 character for ", 0x") + null termination.
150+
// Rounding up to 50 bytes.
151+
char output[50];
152+
size_t offset = 0;
153+
for (unsigned int i = 0; i < span.size(); i++)
154+
{
155+
if (i % 8 == 0 && offset != 0)
156+
{
157+
Log(module, category, "%s", output);
158+
offset = 0;
159+
}
160+
int result = snprintf(&output[offset], sizeof(output) - offset, "0x%02x, ", (unsigned char) span.data()[i]);
161+
if (result > 0)
162+
{
163+
offset += static_cast<size_t>(result);
164+
}
165+
else
166+
{
167+
Log(module, chip::Logging::kLogCategory_Error, "Failed to print ByteSpan buffer");
168+
return;
169+
}
170+
}
171+
if (offset != 0)
172+
{
173+
Log(module, category, "%s", output);
174+
}
175+
}
176+
145177
void LogV(uint8_t module, uint8_t category, const char * msg, va_list args)
146178
{
147179
if (!IsCategoryEnabled(category))

src/lib/support/logging/CHIPLogging.h

+16
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
*/
6767

6868
namespace chip {
69+
70+
template <class T>
71+
class Span;
72+
using ByteSpan = Span<const uint8_t>;
73+
6974
namespace Logging {
7075

7176
using LogRedirectCallback_t = void (*)(const char * module, uint8_t category, const char * msg, va_list args);
@@ -88,6 +93,8 @@ void SetLogRedirectCallback(LogRedirectCallback_t callback);
8893
void LogV(uint8_t module, uint8_t category, const char * msg, va_list args);
8994
void Log(uint8_t module, uint8_t category, const char * msg, ...) ENFORCE_FORMAT(3, 4);
9095

96+
void LogByteSpan(uint8_t module, uint8_t category, const ByteSpan & span);
97+
9198
uint8_t GetLogFilter();
9299
void SetLogFilter(uint8_t category);
93100

@@ -158,6 +165,15 @@ void SetLogFilter(uint8_t category);
158165
#define ChipLogDetail(MOD, MSG, ...) ((void) 0)
159166
#endif
160167

168+
#if CHIP_DETAIL_LOGGING
169+
#ifndef ChipLogByteSpan
170+
#define ChipLogByteSpan(MOD, DATA) \
171+
chip::Logging::LogByteSpan(chip::Logging::kLogModule_##MOD, chip::Logging::kLogCategory_Detail, DATA)
172+
#endif
173+
#else
174+
#define ChipLogByteSpan(MOD, DATA) ((void) 0)
175+
#endif
176+
161177
#if CHIP_ERROR_LOGGING || CHIP_PROGRESS_LOGGING || CHIP_DETAIL_LOGGING
162178
#define _CHIP_USE_LOGGING 1
163179
#else

src/transport/FabricTable.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ CHIP_ERROR FabricInfo::GetCompressedId(FabricId fabricId, NodeId nodeId, PeerId
187187
uint8_t compressedFabricIdBuf[sizeof(uint64_t)];
188188
MutableByteSpan compressedFabricIdSpan(compressedFabricIdBuf);
189189
P256PublicKey rootPubkey(GetRootPubkey());
190+
ChipLogDetail(Inet, "Generating compressed fabric ID using uncompressed fabric ID 0x" ChipLogFormatX64 " and root pubkey",
191+
ChipLogValueX64(fabricId));
192+
ChipLogByteSpan(Inet, ByteSpan(rootPubkey.ConstBytes(), rootPubkey.Length()));
190193
ReturnErrorOnFailure(GenerateCompressedFabricId(rootPubkey, fabricId, compressedFabricIdSpan));
194+
ChipLogDetail(Inet, "Generated compressed fabric ID");
195+
ChipLogByteSpan(Inet, compressedFabricIdSpan);
191196

192197
// Decode compressed fabric ID accounting for endianness, as GenerateCompressedFabricId()
193198
// returns a binary buffer and is agnostic of usage of the output as an integer type.
@@ -325,12 +330,19 @@ CHIP_ERROR FabricInfo::GenerateDestinationID(const ByteSpan & ipk, const ByteSpa
325330

326331
Encoding::LittleEndian::BufferWriter bbuf(destinationMessage, sizeof(destinationMessage));
327332

333+
ChipLogDetail(Inet,
334+
"Generating DestinationID. Fabric ID 0x" ChipLogFormatX64 ", Dest node ID 0x" ChipLogFormatX64 ", Random data",
335+
ChipLogValueX64(mFabricId), ChipLogValueX64(destNodeId));
336+
ChipLogByteSpan(Inet, random);
337+
328338
bbuf.Put(random.data(), random.size());
329339
// TODO: In the current implementation this check is required because in some cases the
330340
// GenerateDestinationID() is called before mRootCert is initialized and GetRootPubkey() returns
331341
// empty Span.
332342
if (!rootPubkeySpan.empty())
333343
{
344+
ChipLogDetail(Inet, "Root pubkey");
345+
ChipLogByteSpan(Inet, rootPubkeySpan);
334346
bbuf.Put(rootPubkeySpan.data(), rootPubkeySpan.size());
335347
}
336348
bbuf.Put64(mFabricId);
@@ -339,8 +351,13 @@ CHIP_ERROR FabricInfo::GenerateDestinationID(const ByteSpan & ipk, const ByteSpa
339351
size_t written = 0;
340352
VerifyOrReturnError(bbuf.Fit(written), CHIP_ERROR_BUFFER_TOO_SMALL);
341353

354+
ChipLogDetail(Inet, "IPK");
355+
ChipLogByteSpan(Inet, ipk);
356+
342357
CHIP_ERROR err =
343358
hmac.HMAC_SHA256(ipk.data(), ipk.size(), destinationMessage, written, destinationId.data(), destinationId.size());
359+
ChipLogDetail(Inet, "Generated DestinationID output");
360+
ChipLogByteSpan(Inet, destinationId);
344361
return err;
345362
}
346363

0 commit comments

Comments
 (0)