Skip to content

Commit

Permalink
BSLS_REVIEW uses plink symbols GUID where available (with size fix) (…
Browse files Browse the repository at this point in the history
…#4768)
  • Loading branch information
jfevold-bbg authored and GitHub Enterprise committed May 29, 2024
1 parent 9e77574 commit b535eb1
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 0 deletions.
83 changes: 83 additions & 0 deletions groups/bsl/bsls/bsls_stackaddressutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ BSLS_IDENT("$Id$ $CSID$")

#include <bsls_bsltestutil.h>
#include <bsls_platform.h>
#include <bsls_stackaddressutil_plinktimestamp.h>
#include <bsls_types.h>
#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -266,6 +267,78 @@ uintptr_t getStackOffset()
} // close unnamed namespace


namespace {

const char *getPwhatVar(const char *const tag)
// Return the 'pwhat' variable with the specified 'tag'. The behavior is
// undefined unless 'tag' is a non-empty null-terminated string. Return
// '0' if the 'plink_timestamp___' global variable does not contain the
// 'tag' or is not well formed.
{
// This is a modified version of 'sysutil_pwhat_getvar', adjusted to not
// use additional static data and to be more resilient if
// 'plink_timestamp___' is not formed as expected.

assert(0 != tag);

size_t taglength = strlen(tag);
assert(taglength > 0);

// Assume that 'plink_timestamp___' is a '0'-terminated list of
// '0'-terminated strings. Each string should roughly match the regular
// expression '[^]]*] (?<tag>[A-Z]+) *: (?<value>.*)'.

for (int i = 0; 0 != plink_timestamp___[i]; ++i) {

// search through '[^]]*]'
const char *p = strchr(plink_timestamp___[i], ']');
if (0 == p) {
continue;
}
++p;

// make sure the next character is ' '
if (' ' != *p) {
continue;
}
++p;

// see if the desired 'tag' is next
if (0 != strncmp(p,tag,taglength)) {
continue;
}
p += taglength;

// identify if ' *:' is next and advance past the ':'
switch (*p) {
case ' ': {
p = strchr(p,':');
} break;
case ':': {
} break;
default: {
// this is an invalid line or a tag that has the requested tag as a
// prefix.
continue;
} break;
}
++p;

// make sure the next character is ' ' (again)
if (' ' != *p) {
continue;
}
++p;

// the remaining part of this string is the value for the requested
// 'tag'
return p; // RETURN
}

return 0;
}

} // close unnamed namespace

namespace BloombergLP {

Expand Down Expand Up @@ -605,6 +678,16 @@ void StackAddressUtil::formatCheapStack(char *output,
return; // RETURN
}

const char *guid = getPwhatVar("GUID");
if (0 != guid && 0 != *guid) {
printed = snprintf(out, rem, "-g %s ", guid);
out += printed;
rem -= printed;
if (printed < 0 || rem <= 0) {
return; // RETURN
}
}

#if defined(BSLS_PLATFORM_OS_AIX)
// On AIX cheapstack expects the stack addresses to be offset based on
// where the text segment was actually loaded - see DRQS 19990260, DRQS
Expand Down
40 changes: 40 additions & 0 deletions groups/bsl/bsls/bsls_stackaddressutil_plinktimestamp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// bsls_stackaddressutil_plinktimestamp.cpp -*-C++-*-
#include <bsls_stackaddressutil_plinktimestamp.h>

#include <bsls_ident.h>
BSLS_IDENT("$Id$ $CSID$")

#include <bsls_platform.h>

// BDE_VERIFY pragma: -AQb01
// BDE_VERIFY pragma: -TR04

// plink_timestamp___ :
const char *plink_timestamp___[6]={0};

// Note that the size of plink_timestamp___ must match the other weak linker
// symbol in sysutil_pwhat.c, which is 6.

#if defined(BSLS_PLATFORM_OS_AIX) || \
defined(BSLS_PLATFORM_CMP_GNU) || \
defined(BSLS_PLATFORM_CMP_SUN)

#pragma weak plink_timestamp___

#endif

// ----------------------------------------------------------------------------
// Copyright 2024 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------
42 changes: 42 additions & 0 deletions groups/bsl/bsls/bsls_stackaddressutil_plinktimestamp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// bsls_stackaddressutil_plinktimestamp.h -*-C++-*-
#ifndef INCLUDED_BSLS_STACKADDRESSUTIL_PLINKTIMESTAMP
#define INCLUDED_BSLS_STACKADDRESSUTIL_PLINKTIMESTAMP

#include <bsls_ident.h>
BSLS_IDENT("$Id: $")

//@PURPOSE: Declare and weakly define a 'plink_timestamp___' global variable.
//
//@DESCRIPTION: This component provides a weak definition of the Bloomberg
// standard 'plink_timestamp__' variable, which is normally generated by any
// program that is built with the Bloomberg 'plink' build system. This array
// of null-terminated strings, ending in a '0', normally contains data useful
// in identifying when and how a particular task was built.
//
// The definition provided by this component is an array containing a single
// '0' value with 'weak' linkage, which should cause it to be only linked into
// an application in build systems that don't generate a definition for this
// variable themselves. Note that "dummy" definitions of this symbol
// occasional use an array of empty strings, but that that would crash
// 'sysutil_pwhat_getvar' if it ended up actually linking together and being
// called.

extern "C" const char *plink_timestamp___[];

#endif

// ----------------------------------------------------------------------------
// Copyright 2024 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------
138 changes: 138 additions & 0 deletions groups/bsl/bsls/bsls_stackaddressutil_plinktimestamp.t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// bsls_stackaddressutil_plinktimestamp.t.cpp -*-C++-*-

#include <bsls_stackaddressutil_plinktimestamp.h>

#include <bsls_bsltestutil.h>
#include <cstdio> // 'fprintf'
#include <cstdlib> // 'atoi'

using namespace BloombergLP;
using namespace std;

//=============================================================================
// TEST PLAN
//-----------------------------------------------------------------------------
// Overview
// --------
// This component will verify that the 'plink_timestamp___' defined by this
// component is an array containing a single 0 value. Note that this is only
// testing the fallback case of a task linked without "pwhat strings." Tests
// for the interesting case have build dependencies which cannot be satisfied
// in this repo.
//-----------------------------------------------------------------------------
// [1] plink_timestamp___
//-----------------------------------------------------------------------------

// ============================================================================
// STANDARD BSL ASSERT TEST FUNCTION
// ----------------------------------------------------------------------------

namespace {

int testStatus = 0;

void aSsErT(bool condition, const char *message, int line)
{
if (condition) {
printf("Error " __FILE__ "(%d): %s (failed)\n", line, message);

if (0 <= testStatus && testStatus <= 100) {
++testStatus;
}
}
}

} // close unnamed namespace

// ============================================================================
// STANDARD BSL TEST DRIVER MACRO ABBREVIATIONS
// ----------------------------------------------------------------------------

#define ASSERT BSLS_BSLTESTUTIL_ASSERT
#define ASSERTV BSLS_BSLTESTUTIL_ASSERTV

#define LOOP_ASSERT BSLS_BSLTESTUTIL_LOOP_ASSERT
#define LOOP0_ASSERT BSLS_BSLTESTUTIL_LOOP0_ASSERT
#define LOOP1_ASSERT BSLS_BSLTESTUTIL_LOOP1_ASSERT
#define LOOP2_ASSERT BSLS_BSLTESTUTIL_LOOP2_ASSERT
#define LOOP3_ASSERT BSLS_BSLTESTUTIL_LOOP3_ASSERT
#define LOOP4_ASSERT BSLS_BSLTESTUTIL_LOOP4_ASSERT
#define LOOP5_ASSERT BSLS_BSLTESTUTIL_LOOP5_ASSERT
#define LOOP6_ASSERT BSLS_BSLTESTUTIL_LOOP6_ASSERT

#define Q BSLS_BSLTESTUTIL_Q // Quote identifier literally.
#define P BSLS_BSLTESTUTIL_P // Print identifier and value.
#define P_ BSLS_BSLTESTUTIL_P_ // P(X) without '\n'.
#define T_ BSLS_BSLTESTUTIL_T_ // Print a tab (w/o newline).
#define L_ BSLS_BSLTESTUTIL_L_ // current Line number

//=============================================================================
// GLOBAL CONSTANTS FOR TESTING
//-----------------------------------------------------------------------------

bool globalVerbose = false;
bool globalVeryVerbose = false;
bool globalVeryVeryVerbose = false;

int main(int argc, char *argv[])
{
int test = argc > 1 ? atoi(argv[1]) : 0;
int verbose = argc > 2;
int veryVerbose = argc > 3;
int veryVeryVerbose = argc > 4;

globalVerbose = verbose;
globalVeryVerbose = veryVerbose;
globalVeryVeryVerbose = veryVeryVerbose;

printf( "TEST %s CASE %d\n", __FILE__, test);

switch (test) { case 0: // zero is always the leading case
case 1: {
// --------------------------------------------------------------------
// PLINK_TIMESTAMP CONTENTS
//
// Concerns:
//: 1 The global variable 'plink_timestamp___' should start with a '0'
//: value.
//
// Plan:
//: 1 Check the value of 'plink_timestamp___'.
//
// Testing:
// plink_timestamp___
// --------------------------------------------------------------------

if (verbose) printf( "\nPLINK_TIMESTAMP CONTENTS"
"\n========================\n" );

ASSERT(0 == plink_timestamp___[0]);
} break;
default: {
fprintf( stderr, "WARNING: CASE `%d` NOT FOUND.\n" , test);
testStatus = -1;
}
}

if (testStatus > 0) {
fprintf( stderr, "Error, non-zero test status = %d.\n", testStatus );
}

return testStatus;
}

// ----------------------------------------------------------------------------
// Copyright 2024 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------
1 change: 1 addition & 0 deletions groups/bsl/bsls/package/bsls.mem
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ bsls_review
bsls_review_macroreset
bsls_spinlock
bsls_stackaddressutil
bsls_stackaddressutil_plinktimestamp
bsls_stopwatch
bsls_systemclocktype
bsls_systemtime
Expand Down

0 comments on commit b535eb1

Please sign in to comment.